3
votes

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()
2
Was the other sound an OGG file? MP3s don't work very well in Pygame.TigerhawkT3

2 Answers

1
votes

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!

0
votes
  1. Pygame seems to only support .ogg and raw .wav

  2. Try this duplicate also : Pygame, sounds don't play

  3. If you're still stuck, based on what I found in the simplest pygame example I know (and used), you may need to add pygame.mixer.init() before loading the sound.