如何使Pyglet媒体队列继续播放?

时间:2019-06-27 20:16:18

标签: python pyglet

我有多首歌曲正在播放器中加载并排队,但是播放器在第一首歌曲播放后停止播放,尽管我认为文档应该是自动的,但我必须使用next_source()手动强制下一首歌曲。

我正在运行Python 3.7.1和Pyglet 1.3.2,已经对文档进行了搜索,并尝试了不同的数据排队方法。我不确定文件是否会完全播放,尽管我不确定第一首歌曲是否会播放完。

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

我的预期结果是,在循环播放所有歌曲之前,它们会进入队列,然后再继续播放下一个歌曲,

2 个答案:

答案 0 :(得分:1)

我想谈谈global的使用,因为您似乎已经解决了它无法继续播放的问题。通过使状态成为类的一部分,可以消除对global的所有使用。当应用程序运行时,它将创建该类的实例,该实例在程序运行的整个过程中都存在。考虑以下。

它不起作用,因为您没有包含整个脚本。 listBox似乎丢失了,也应该是实例成员。

class mediaControls:
    def __init__(self):
        self.chosenDirectory = ""
        self.cSong = ""
        self.cSongIndex = 0
        self.player = pyglet.media.Player()
        self.songList = []

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

        songList = []
        files = os.listdir(chosenDirectory)
        for x, file in enumerate(files):
            filePath = os.path.join(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)
        self.songList = songList

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

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

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

    def playNextSong(self):
        self.player.next_source()

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

答案 1 :(得分:0)

因此,事实证明我期望队列能够处理所有事情,而没有意识到pyglet.app.run()命令实际上是使所有事件开始的原因。不使用它(因为它被阻塞了),我只需要on_eos()来调用next_source()函数并获得相同的结果即可。