我正在尝试用pygame播放一首歌而且它没有播放这首歌。
我的代码:
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"
我的输出为
Mixer settings (22050, -16, 2)
Mixer channels 8
Play
Done
答案 0 :(得分:1)
你的代码适用于我,运行Python 2.7.2的Lubuntu 11.10,以及从Youtube剪辑转换的MP3。你检查过mp3不是零长度吗?你试过一个wav文件吗?
缺乏其他解释,我认为如果pygame.mixer.music.get_busy()
调用尚未完成其进程或线程,play(0)
可能会返回false。
这将导致您的代码跳过while循环,打印“完成”,并终止,删除音乐播放器对象并终止播放,然后才能听到任何声音。如果这是问题,您可以在play(0)
之后和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
答案 1 :(得分:0)
在pygame.mixer.music.play的评论中我发现了这个:
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()
似乎你发送0作为你想要重复它的次数感谢Iskar Jarak ,-1是无穷大。
http://www.pygame.org/docs/ref/music.html#pygame.mixer.music.play