我尝试拥有一个活跃的时钟并同时播放音乐。我不知道音乐发生了什么。它开始回声。或者这首歌可能会多次播放。我认为这与 Gui 或 Clock 的刷新有关。如果有人知道出了什么问题,你真的会让我开心。
from tkinter import *
import vlc
import datetime
def Clockupdate(time):
timerefresher.config(text=time)
Timer = "20:30:00"
if time == Timer:
playsound()
def secondrefresher():
newtime = ""
oldtime = datetime.datetime.now()
a = str(oldtime.hour)
b = str(oldtime.minute)
c = str(oldtime.second)
if int(c) < 10:
c = "0"+str(c)
if int(b) < 10:
b = "0"+str(b)
curtime = (a+":"+b+':'+c)
if curtime != newtime:
newtime = curtime
Clockupdate(newtime)
timerefresher.after(200, secondrefresher)
def playsound():
p = vlc.MediaPlayer("file:///Space.mp3")
root.after(200, p.play)
root = Tk()
timerefresher = Label(root, text = "", bg='black', fg='white', font=('times', 100, 'bold'))
secondrefresher()
timerefresher.pack()
root.mainloop()
如果您希望代码运行,您需要将一个名为 space 的 mp3 放在与此脚本相同的目录中。您还需要将计时器变量调整为您想要开始播放音乐的时间。
最佳答案
我进行了一个小测试,将播放歌曲的调用更改为打印语句。看起来你的 p.play 被调用了 5 次。很可能是因为您设置了刻度。
我会改变:
timerefresher.after(200, secondrefresher)
致:
timerefresher.after(900, secondrefresher)
# anything between 801 and 999 should work fine without messing with the clock here.
这应该可以防止多次调用p.play
关于python - 使用 vlc pip 产生巨大的回声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44314768/