pygame is not working

Go To StackoverFlow.com

0

I am trying to play a song with pygame and it is not playing the song.

My code:

import pygame,time
pygame.init()
print "Mixer settings", pygame.mixer.get_init()
print "Mixer channels", pygame.mixer.get_num_channels()
pygame.mixer.music.set_volume(1.0)
pygame.mixer.music.load('C:/1.mp3')
print "Play"
pygame.mixer.music.play(0)
while pygame.mixer.music.get_busy():
   print "Playing", pygame.mixer.music.get_pos()
time.sleep(1)
print "Done"

I am getting output as

Mixer settings (22050, -16, 2)
Mixer channels 8
Play
Done
2012-04-04 23:04
by chom


1

Your code works for me, on Lubuntu 11.10 running Python 2.7.2, with an MP3 I converted from a Youtube clip. Have you checked that the mp3 is not zero length? Have you tried a wav file?

Lacking other explanations, I think it is conceivable that pygame.mixer.music.get_busy() could be returning false if the play(0) call hasn't finished starting its process or thread. This would cause your code to skip the while loop, print "Done", and terminate, deleting the music player object and terminating playback before you got to hear anything. If this is the problem, you could try something like this after play(0) and before print Done:

pygame.mixer.music.set_endevent(pygame.USEREVENT)
finishedPlaying = False

while not finishedPlaying:
    for event in pygame.event.get():
        if event.type == pygame.USEREVENT: 
            finishedPlaying = True
            break # only because we don't care about any other events
    print "Playing", pygame.mixer.music.get_pos() # will print -1 on the last iteration
2012-04-05 01:15
by Iskar Jarak


0

in the comments of pygame.mixer.music.play i found this:

November 18, 2010 7:30pm - Anonymous
Work Exmpl:

pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
sound = pygame.mixer.Sound('Time_to_coffee.wav').play()

also it seems that your sending 0 as the amount of times you want to repeat it thanks Iskar Jarak , -1 is infinity.

http://www.pygame.org/docs/ref/music.html#pygame.mixer.music.play

2012-04-05 00:27
by apple16
Thanks for the solution. I tried this. But, it is still not playing the song - chom 2012-04-05 00:43
"Be aware that MP3 support is limited. On some systems an unsupported format can crash the program, e.g. Debian Linux. Consider using OGG instead. - apple16 2012-04-05 00:49
"also it seems that your sending 0 as the amount of times you want to play it, -1 is infinity." This is incorrect - the argument is the number of times to repeat it. 0 should play the song once - Iskar Jarak 2012-04-05 00:56
Ads