Search code examples
pythonpyglet

How do I make the Pyglet media queue continue playing?


I have multiple songs being loaded and queued up in my player, however the player stops after the first song and I have to manually force the next song using next_source() though from the documentation I believe it should be automatic.

I'm running Python 3.7.1 and Pyglet 1.3.2, I have scoured the documentation and tried different methods of queuing the data. I have looked into making sure the file is playing completely, though I'm unsure if playback is ever ending on the first song.

chosenDirectory = ""
cSong = ""
cSongIndex = 0
player = pyglet.media.Player()
songList = []


def chooseDirectory():
    global chosenDirectory
    chosenDirectory = tk.filedialog.askdirectory(initialdir="/")
    lbl.configure(text=chosenDirectory)
    listBox.delete("0", "end")

    def listFiles(dir):
        global songList
        songList = []
        files = os.listdir(dir)
        for x, file in enumerate(files):
            filePath = chosenDirectory + "/" + file
            if not os.path.isdir(filePath):
                fileExt = file.split(".")[1]
                if fileExt == "mp3" or fileExt == "wav":
                    listBox.insert(x, file)
                    songList.append(filePath)

    listFiles(chosenDirectory)


class mediaControls:
    def curSelect(self, info):
        global cSong
        if listBox.curselection() != ():
            selected = listBox.get(listBox.curselection())
            if selected != cSong:
                cSong = selected
                cSongIndex = listBox.curselection()[0]
        self.playList()

    def playSong(self):
        if not player.playing:
            player.play()
        elif player.playing:
            player.pause()

    def playList(self):
        for song in songList:
            music = pyglet.media.load(song)
            print(music.duration)
            player.queue(music)
        self.playSong()

    def playNextSong(self):
        player.next_source()

    def playNewSong(self):
        global cSong
        global cSongIndex
        cSongIndex = cSongIndex + 1
        cSong = listBox.get(cSongIndex)
        self.playSong()

My expected result from this would be it cycling through all of the songs dequeuing them before moving on to the next, and not requiring intervention on my part.


Solution

  • So it turns out I was expecting the queue to handle everything without realizing the pyglet.app.run() command is actually what sets all the events into motion. Without using that (since its blocking) I can just on_eos() to go ahead and call the next_source() function and get the same result.