python - 使用 vlc pip 产生巨大的回声

标签 python tkinter vlc

我尝试拥有一个活跃的时钟并同时播放音乐。我不知道音乐发生了什么。它开始回声。或者这首歌可能会多次播放。我认为这与 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/

相关文章:

android - 为 x86 构建 LibVLC

python - VLC python 绑定(bind)问题 => NameError : name 'u' is not defined

python - 在 python 中将 numpy ndarray 转换为字符串的更智能、更快速的方法

python - 即使文件只有一维,如何使 np.loadtxt 返回多维数组?

python - 使用 Tkinter Text 使用 Tab 键转到下一个字段(而不是缩进)

python - 如何在 Tkinter 中添加图像?

javascript - 在 GWT 中嵌入 VLC 播放器

python - `__contains__` 是否可能返回非 bool 值?

python - 合并列表与交集

python - Python 中的 focus_set() 在第二个窗口上不起作用