Ad

Why Is PyGame Not Playing Sounds

- 1 answer

I am making a game in PyGame and started to add some sounds to it. One sound I added would play right, but another sound I added will only play for milliseconds (I'm assuming, seeing that a short click is all I hear). I tried calling time.sleep() after it, but I am still getting that short click. I made a test program, and the sounds still aren't playing. I'll appreciate any help/ suggestions.

import pygame
pygame.init()

JohnCena = pygame.mixer.Sound('JohnCena.mp3')

def main():
    JohnCena.play(0,0,0)
    raw_input()

main()
Ad

Answer

First of all, mp3's don't work really well in pygame. Your code should work perfectly fine. One issue is you are doing

loops = 0

in the main function.

You should change the file extension to *.ogg (because ogg files work pretty well) and try out this code:

import pygame
pygame.init()
music = pygame.mixer.music.load('file.ogg')
def main():
    pygame.music.play(loops=-1, start=0)
    raw_input()
main()

The only thing you have to change is the

pygame.mixer.music.load('file.ogg')

line. Be careful is you change the file extension to something else, because pygame only loads uncompressed *.wav files and *.ogg files!

Ad
source: stackoverflow.com
Ad