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