Why doesn't my PyGame mixer play sounds,?

雨燕双飞 提交于 2019-12-03 20:21:41

问题


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.


回答1:


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.




回答2:


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()



回答3:


you just created an object called song.

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

song.play()




回答4:


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')



来源:https://stackoverflow.com/questions/18706991/why-doesnt-my-pygame-mixer-play-sounds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!