简体   繁体   中英

Python Pyglet player on_eos decorator not being called

I'm currently trying to have a python media player automatically queue and play a random source after the current source ends. To this end, I've been wrestling with the player event 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()

However the on_eos() event is never called. I've looked through the pyglet documentation, and tried

@Player.event('on_eos')

and even defining a sublcass of player that defines on_eos, all to no effect. Is this an error with on_eos never being dispatched, or am I missing something?

this should work for you:

Player = pyglet.media.Player()

# our event handling function
def on_eos():
    print("on player eos")

Player.push_handlers(on_eos)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM