0

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.

White Shadow
  • 444
  • 2
  • 10
  • 26
Logan Henry
  • 63
  • 1
  • 10

2 Answers2

2

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"
f.rodrigues
  • 3,499
  • 6
  • 26
  • 62
  • Thanks! I tried your edit and it worked well. From the output I get it seems like the code is running well. However there is still no sound as output! Still refused to work with the mp3 but an ogg conversion did sort of work. The code ran but without sound. however I could tell by the output something was working Could it be the ogg? – Logan Henry Dec 07 '14 at 23:34
  • I can't tell, only with the file I could test myself. I tried several mp3 files and only a few worked, ogg files also have some problems, I'm not sure how to fix. midi files always seems to work. – f.rodrigues Dec 07 '14 at 23:39
  • Have you tried the mixer.Sound class? http://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Sound – f.rodrigues Dec 07 '14 at 23:50
  • This is going to be implemented into a larger game and is supposed to be used as background music. The sound class is going to be used for sound effects. – Logan Henry Dec 08 '14 at 00:35
0

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.

Nwilson
  • 121
  • 9
  • I tried changing to ogg and it made little difference. However f.rodrigues gave me some changes that helped. The mp3 was for sure an issue. – Logan Henry Dec 07 '14 at 23:36