Why doesn't my PyGame mixer play sounds,?

前端 未结 4 2159
忘了有多久
忘了有多久 2021-01-03 08:57

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 t

相关标签:
4条回答
  • 2021-01-03 09:19

    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()
    
    0 讨论(0)
  • 2021-01-03 09:22

    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.

    0 讨论(0)
  • 2021-01-03 09:24

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

    0 讨论(0)
  • 2021-01-03 09:25

    you just created an object called song.

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

    song.play()

    0 讨论(0)
提交回复
热议问题