2017-04-10 94 views

回答

0

首先所有,确保你有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()