我目前正在尝试让python媒体播放器在当前源结束后自动排队并播放随机源。为此,我一直在与玩家事件on_eos进行斗争
import pyglet
import random
import os
#Global Variable Declarations
MusicDir = ""
MusicList = ""
CurrentIndex = 0
MaxSongs = 0
Player = pyglet.media.Player()
Player.eos_action = pyglet.media.Player.EOS_NEXT
@Player.event
def on_eos():
randomSong()
def getMusicDir():
global MusicDir
global MusicList
global MaxSongs
MusicDir = os.path.abspath("C:\music")
MusicList = os.listdir(MusicDir)
MaxSongs = len(MusicList)
def randomSong():
global CurrentIndex
global MusicList
global Player
CurrentIndex = random.randint(0, MaxSongs-1)
Player.queue(pyglet.media.load(MusicDir + "/" + MusicList[CurrentIndex]))
Player.next()
Player.play()
getMusicDir()
CurrentIndex = random.randint(0, MaxSongs-1)
Player.queue(pyglet.media.load(MusicDir + "/" + MusicList[CurrentIndex]))
Player.play()
但是从不调用on_eos()事件。我查看了pyglet文档,并尝试了
@Player.event('on_eos')
甚至定义了一个定义on_eos的玩家的sublcass,一切都没有效果。这是一个错误,on_eos永远不会被派遣,或者我错过了什么?
答案 0 :(得分:-1)
这应该适合你:
Player = pyglet.media.Player()
# our event handling function
def on_eos():
print("on player eos")
Player.push_handlers(on_eos)