What is preventing the sound files from being played in this pygame Mixer set up?

1.3k views Asked by At

I'm trying to write a small bit of code to play music files in the background of a game. The problem I'm coming across is that despite all the code being laid out and phrased properly no sound files will play. I put several print statements in the code and it seems to suggest that the sound file is either not loading or is simply not playing once it loads?

import pygame
def musicPlayer():
    print "Playing music now"
    pygame.init()
    pygame.mixer.music.load('01ANightOfDizzySpells.mp3')
    print "load song1"
    pygame.mixer.music.play(loops=0, start=0.0)
    print "play song1"
    pygame.mixer.music.queue('01HHavok-intro.mp3')
    pygame.mixer.music.queue('02HHavok-main.mp3')
    pygame.mixer.music.queue('02Underclockedunderunderclockedmix.mp3')
    pygame.mixer.music.queue('03ChibiNinja.mp3')
    pygame.mixer.music.queue('04AllofUs.mp3')
    pygame.mixer.music.queue('05ComeandFindMe.mp3')
    pygame.mixer.music.queue('06Searching.mp3')
    pygame.mixer.music.queue('07WeretheResistors.mp3')
    pygame.mixer.music.queue('08Ascending.mp3')
    pygame.mixer.music.queue('09ComeandFindMe-Bmix.mp3')
    pygame.mixer.music.queue('10Arpanauts.mp3')
    pygame.mixer.music.queue('DigitalNative.mp3')
    pygame.mixer.music.set_endevent()
    #musicPlayer()
musicPlayer()

Am I missing something basic? Or could it have to do with my computer not the code?

Edit: this is the output from running the code

 Playing music now
 load song1
 play song1

As you can see it throws no errors.

2

There are 2 answers

1
Nwilson On

I'm not very familiar with pygame and its methods, but my guess is that either it cannot find the file correctly, or it's not able to correctly handle the file that it finds.

I would suggest putting the full path to audio files to see if that helps. That would rule out the issue of it not finding the files properly (although hardcoding this is obviously not a good idea long-term as any changes you make to the organizational structure of your program will likely break those paths).

And according to their documentation, their MP3 support is limited. You might try using .ogg files instead (http://www.pygame.org/docs/ref/music.html) as it could just be an issue with the encoding not being fully supported.

4
f.rodrigues On

I tried your code with a mid file, it works fine, but there's some adjustments:

import pygame

def musicPlayer():
    pygame.init()
    pygame.mixer.music.load('test.mid')
    print "load song1"
    pygame.mixer.music.play()
    print "play song1"
    pygame.mixer.music.queue('test_2.mid')

musicPlayer()
while pygame.mixer.music.get_busy():
    pygame.time.Clock().tick(10)
print "DONE"

if the script ends the play won't happend, for that you need to get_busy() in a while loop.

in the documentation of pygame.music it states: Be aware that MP3 support is limited [...] Consider using OGG instead.


I played a little with the Sound class.

Here's what I came up:

import pygame

def musicPlayer():
    pygame.mixer.init()
    channel = pygame.mixer.Channel(1)
    sound = pygame.mixer.Sound('test.ogg')
    channel.play(sound)

musicPlayer()
while pygame.mixer.get_busy():
    pygame.time.Clock().tick(10)
print "DONE"