假设我使用mp3play
模块来播放mp3文件,并使用ttk.Progressbar
来显示播放音乐的数量(持续时间)。有什么代码可以实现它吗?在
我还想要一个定时器来显示音乐播放的持续时间。在
import ttk
import mp3play
self.music = mp3play.load('filename')
self.fr=ttk.Frame()
self.fr.pack(expand=True, fill=BOTH, side=TOP)
self.seek=ttk.Progressbar(self.fr, orient='horizontal', mode='determinate', length=500)
self.seek.place(x=50, y=325)
self.seek.start()
查看
mp3play
模块的代码,mp3play.load()
返回一个AudioClip
对象。此对象具有方法seconds()
和milliseconds()
,它们分别以秒或毫秒为单位提供剪辑的长度。在您可以保存开始播放时的时间,并将其与当前时间和剪辑的总长度进行比较,以确定progressbar的状态。在
mp3play模块uses似乎是Windows winmm库。具体来说,它使用函数^{} 来control多媒体系统。在
实现您想要的效果的一个方法是使用^{} 命令定期检索当前播放位置并按您的喜好显示它。在
从mp3play库修改
AudioClip
类的两个版本似乎是最合适的。在修改mp3play库
类音频剪辑windows.py
首先,在59 of windows.py行上可以看到一个使用status命令的函数
我们可以根据这个例子编写一个新函数,它将得到当前的回放位置:
^{pr2}$此函数将返回一个字符串,该字符串编码表示当前位置(毫秒)的数字。在
在init.py中初始化音频剪辑
下一步是修改12 of __init__py行上的
AudioClip
类,添加以下函数:CLI示例
现在我们可以用一个简单的脚本进行测试:
控制台输出如下所示:
GUI示例
截图:
相关问题 更多 >
编程相关推荐