Why doesn't my PyGame mixer sound?

My PyGame mixer in version 2.7 will not work with sound. I can get it to work with mixer.music, but not with mixer.sound, with mixer.sound it makes a little ticking noise and then stops. The 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) 

There is no mistake, it just won’t play and gives a little ticking noise. On windows 7-x64 by the way.

+4
source share
4 answers

Pygame usually does not play mp3 files. You can check if the .wav and .ogg files will play first to make sure your code is correct (based on what you inserted, it seems like it is correct). I suggest converting your mp3 sounds to ogg for Pygame.

+6
source

This can be easily resolved because your song file should be downloaded as music, and not as normal sound. Therefore, the following code makes it perfect:

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

You just created an object called a song.

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

song.play ()

0
source

Pygame plays mp3 files. I had the same problem, but I found a solution:

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

So try the following: pygame.mixer.music.load('filename.mp3.mp3')

-one
source

Source: https://habr.com/ru/post/1501367/


All Articles