4

My PyGame mixer in 2.7 won't work with the sound option. I can make it work with mixer.music but not with mixer.sound, with mixer.sound it makes a small ticking noise and then stops. Code:

import pygame
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
song = pygame.mixer.Sound("song.mp3")
pygame.mixer.Sound.play(song)

No error, it just won't play and gives a small ticking noise. On windows 7-x64 btw.

user164814
  • 135
  • 2
  • 2
  • 5

4 Answers4

9

Usually, Pygame will not play mp3 files. You could test to see if .wav and .ogg files will play first, to make sure your code is correct (based on what you pasted, it seems to be right). I suggest converting your mp3 sounds to ogg for Pygame.

SimonT
  • 2,219
  • 1
  • 18
  • 32
  • 1
    Interesting. When I use Audacity to convert an MP3 to a Wave, Pygame won't play it. But when I convert MP3 to Ogg Vorbis it works. Any sounds that were Wave to begin with will play, however. Any reason why? – Galen Nare Apr 11 '15 at 13:51
  • @GalenNare I'm not a Pygame guru but my best prediction is that you might have made a .wav with certain codecs or weird settings in Audacity, while the "native" wav files were more ordinary. – SimonT Apr 12 '15 at 18:20
  • This is exactly what I was looking for as an answer. Switched sound format and suddenly it was working. – Tim Holt May 08 '16 at 21:47
1

you just created an object called song.

instead of "pygame.mixer.Sound.play(song)" try this:

song.play()

Pommy
  • 11
  • 1
0

This can easily be solved because your song file should be loaded as music, not as a normal sound. Therefore, the following code makes it work perfectly:

import pygame
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
pygame.mixer.music.load("song.mp3")
pygame.mixer.music.play()
zenofpython
  • 182
  • 1
  • 2
  • 14
  • While a song is better off with the music module, the music module does not have any capabilities for different file types that the mixer module lacks. – trevorKirkby Dec 21 '13 at 02:02
  • In this case, one should use music module for .mp3 files as they will not play on the mixer module. – zenofpython Dec 22 '13 at 21:03
  • 1
    While it is better to use the music module, what I am saying is that even then it won't always support mp3 file format. – trevorKirkby Dec 22 '13 at 21:50
0

Pygame does play mp3 files. I had the same problem but I found out the solution:

if you saved your mp3 file as 'filename.mp3', and you wrote down the .mp3 file extension yourself, then the filename in pygame's pygame.mixer.music.load() function must be written as 'filename.mp3.mp3', because python expects you to add the .mp3. Sometimes the .mp3 is already included in the filename if you manually saved it as that.

Therefore, try this: pygame.mixer.music.load('filename.mp3.mp3')

TheEyesHaveIt
  • 980
  • 5
  • 16
  • 33
  • 3
    _"Hide extensions for known file types"_ should never have been a setting on windows... – Eric Jan 03 '16 at 03:03