Search code examples
pythonaudio-playerpyglet

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?


Solution

  • 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)