如何使用 Pyglet 显示音频的当前时间和持续时间?

How to show the current time and the duration of an audio using Pyglet?

我是 Python - Pyglet 和 Whosebug 的新手。我想知道如何在 Pyglet 中显示当前播放时间和音频的总持续时间。 Pyglet Docs 中清楚地给出了它,但我并不完全理解如何正确使用它。所以,我想请求帮助。 通过展示一个例子会容易得多。谢谢!

这是我的代码..

from tkinter import*
import pyglet

root = Tk()

player = pyglet.media.Player()
song = "er.mp3"
src = pyglet.media.load(song)
player.queue(src)

def play():
    player.play()

def pause():
    player.pause()

button_1 = Button(root,text = "Play", command = play)
button_1.pack()
button_2 = Button(root,text = "Pause", command = pause)
button_2.pack()

root.mainloop()

(抱歉英语不好)

简短的回答是:

current_time = player.time

这将 give/store 当前播放音频的时间。
你如何处理这些信息取决于你,我假设你想将它添加到标签或其他东西。

v = StringVar()
Label(master, textvariable=v).pack()

# Probably in a event driven loop or something.
v.set(player.time)

但是长话短说,不要混合你的库。

Pyglet 非常适合 2D/3D 渲染,因为您可以很好地连接到 GL 库。
Pyglet 做得不好的是音频(即使它支持音频)。

另一方面,Tkinter 不做这些事情,而是给你按钮和其他 "widgets"。

我建议使用 how to play music through python with mpg321 下的任何其他库来使用 tkinter 播放音频。

The Snack Sound Toolkit 或 winsound,如果您使用 windows。