3

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()
Song
  • 298
  • 5
  • 20

2 Answers2

0
  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.

Community
  • 1
  • 1
Diane M
  • 1,503
  • 1
  • 12
  • 23
  • 1
    `pygame.mixer.init()` isn't necessary here because the OP's program has `pygame.init()`, which initializes all Pygame modules. – TigerhawkT3 Dec 14 '15 at 02:56
  • Chimp example also does, which is strange but I assumed there is a reason behind it. Its very unlikely to be the reason op has a problem, but worth a try if 1 and 2 fail. – Diane M Dec 14 '15 at 05:57
  • I tried exporting the audio as wav instead of mp3 in the program Audacity, but I still only get the clicking sound. I tried it with some sounds from sound bible that were originalle in wav format, but they didn't work either. For some reason though, one sound will play just fine in my program...? –  Dec 28 '15 at 04:48
  • `pygame.init()` dosn't always work, but you can fix this by using both `pygame.init()` and the `init()` functions of the modules(like `display` or `mixer`) – Uncle Dino Apr 14 '16 at 19:54
0

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!

Song
  • 298
  • 5
  • 20