告诉我如何在播放期间更改音量而不是中断音量? 也许我需要使用额外的库? 我的代码示例:
import vlc
import time
url = 'http://prem1.rockradio.com:80/bluesrock?749334267be3790147565672'
#define VLC instance
instance = vlc.Instance('--input-repeat=-1', '--fullscreen')
#Define VLC player
player=instance.media_player_new()
#Define VLC media
media=instance.media_new(url)
#Set player media
player.set_media(media)
#Play the media
player.play()
player.audio_set_volume(50)
答案 0 :(得分:2)
我想我有一个答案给你!请注意,此代码是我正在处理的一个更大项目的一部分。此代码的某些部分不会按原样格式化。
import threading
import time
import vlc
from tkinter import *
root = Tk()
root.title("Playlist Player")
root.geometry("700x200")
volume = Scale(root, from_=0, to=100, orient=HORIZONTAL)
volume.set(100)
volume.place(x=150, y=20)
root.mainloop()
player = vlc.MediaPlayer()
media = vlc.Media(dir + playlist + "/" + song)
player.set_media(media)
player.audio_set_volume(100)
player.video_set_scale(0.6)
player.play()
play()
clip = MP3(dir + playlist + "/" + song)
clip_info = clip.info
time.sleep(int(clip_info.length)) # Wait until after the clip has finished
player.stop()
def play():
volumethread = threading.Thread(target=refreshvolume, daemon=True)
volumethread.start()
def refreshvolume():
while True:
changevolume(volume.get())
time.sleep(0.1)
def changevolume(volume):
player.audio_set_volume(volume)
time.sleep(0.1)
所以我在这里所做的几乎是让一个线程无限循环一个命令,每 0.1 秒更改一次音量(我将 sleep 放在那里以试图降低 CPU 使用率)。因此,每隔十分之一秒,就会运行一个命令,将音量更改为任何比例。