简体   繁体   English

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

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

I am new in Python - Pyglet and Stackoverflow. 我是Python新手-Pyglet和Stackoverflow。 I would like know how to show the current playing time and the total duration of an audio in Pyglet. 我想知道如何在Pyglet中显示当前播放时间和音频的总持续时间。 It is clearly given in Pyglet Docs but I don't excactly understood how to use it properly. Pyglet Docs中清楚地给出了它,但是我没有确切地理解如何正确使用它。 So kindly I would like to request for help. 因此,我想寻求帮助。 It would be lot easier by showing an example. 通过显示一个示例会容易得多。 Thanks! 谢谢!

Here is my code.. 这是我的代码。

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

(Sorry For Bad English) (对不起,英语不好)

Short answer is: 简短的答案是:

current_time = player.time

That will give/store the time of the currently playing audio. 这将给出/存储当前播放音频的时间。
What you do with this information is up to you, I assume you want to add it to a label or something. 您可以根据自己的意愿来处理此信息,我想您想将其添加到标签或其他内容中。

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

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

However the long answer is, don't mix your libraries. 但是,长的答案是,不要混用您的库。

Pyglet is great for 2D/3D rendering since you can hook into the GL libraries pretty well. Pyglet非常适合2D / 3D渲染,因为您可以很好地连接到GL库。
What Pyglet does not do great is Audio (even tho it has support for it). Pyglet不能做得很好的是Audio(甚至支持它)。

Tkinter on the other hand does nothing of these things but instead gives you buttons and other "widgets". 另一方面,Tkinter不执行任何操作,而是提供按钮和其他“小部件”。

I would recommend using any other library under how to play music through python with mpg321 for playing audio with tkinter. 我建议在使用mpg321通过python播放音乐的方式下,使用其他任何库来使用tkinter播放音频。

Either The Snack Sound Toolkit or winsound if you're on windows. 如果您在Windows上,则可以使用Snack Sound Toolkit或winsound。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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