0
我想用pyglet制作一个使用python的mp3播放器,并且我想要显示正在播放的当前歌曲名称。我试图使用SourceInfo类,但我不知道它是如何工作的 pyglet.media.SourceInfo.title
没有显示任何内容。你可以帮我吗? 谢谢在Ubuntu上使用python 2.7和pyglet的MP3播放器
我想用pyglet制作一个使用python的mp3播放器,并且我想要显示正在播放的当前歌曲名称。我试图使用SourceInfo类,但我不知道它是如何工作的 pyglet.media.SourceInfo.title
没有显示任何内容。你可以帮我吗? 谢谢在Ubuntu上使用python 2.7和pyglet的MP3播放器
首先所有,确保你有AvBin安装!
否则,您将无法播放除wav
以外的文件。
请记住,这只是您需要的粗略模拟。
建立它,废弃它..但这是一个开始。
import pyglet
pyglet.options['audio'] = ('pulse', 'alsa')
window = pyglet.window.Window(width=800, height=600)
player=pyglet.media.Player()
# == Queue more tracks,
# the label will update automatically
player.queue(pyglet.media.load('./Downloads/music/Pendulum - Hold Your Color.mp3', streaming=True))
player.volume=0.3
# window is the variable 'window' from earlier.
@window.event
def on_draw():
window.clear()
if player.playing:
label = pyglet.text.Label('{artist} - {song}'.format(**{'artist' : player.source.info.author.decode('UTF-8'),
'song' : player.source.info.title.decode('UTF-8')}),
font_size=36,
x=window.width//2, y=window.height//2, # same here, window on line 6
anchor_x='center', anchor_y='center')
else:
label = pyglet.text.Label('Music paused!', font_size=36, x=window.width//2, y=window.height//2, anchor_x='center', anchor_y='center')
label.draw()
window.flip() # This is overkill but ensures proper rendering at all times.
@window.event
def on_key_press(symbol, modifiers):
# Up and Down controls the volume for instance
if symbol == pyglet.window.key.UP:
player.volume = player.volume+0.1
elif symbol == pyglet.window.key.DOWN:
player.volume = player.volume-0.1
# Any key really will start/pause the playback
elif player.playing:
player.pause()
else:
player.play()
pyglet.app.run()