0

I am trying to play a wav file in my program by using pygame. However I get no sound despite having no errors pop up. The file is in the same project folder as my .py file (I am using PyCharm, but don't know if that is relevant).

import pygame


pygame.init()


def playsound(filename):
    pygame.mixer.music.load(filename)
    pygame.mixer.music.play()


playsound('my-file.wav')

I expected the sound file to play but it didn't. No errors were displayed.

Astudent
  • 176
  • 1
  • 2
  • 16
  • Yes, https://stackoverflow.com/questions/13649884/play-music-using-pygame-but-no-sound/13653825 answered my problem! Seems like you need pygame.display.set_mode((200,100)) for music to play. – Astudent Oct 09 '19 at 15:23
  • It's working fine, but `pygame.mixer.music.play()` only *begins* the playing of the sound. Then the program immediately exits. The code needs to wait until `pygame.mixer.music.get_busy()` returns `False` to give the sound time to play (or wait some other way). The question-part of the possible-duplicate link from @Rabbid76 has a good example of this waiting mechanism. – Kingsley Oct 09 '19 at 21:29

0 Answers0