簡體   English   中英

卷 slider 在 Tkinter 與 PyDub

[英]Volume slider in Tkinter with PyDub

通常對於音頻,您可以通過以下方式訪問音量:sound.volume = 10 但是對於 Pydub,可以使用以下方式訪問音量:sound + 10 這個問題是因為我無法准確地“設置”音量,只需調整歌曲當前的音量在。 我想創建一個 Tkinter slider,我可以在聽不見和響亮之間變化。 這是我到目前為止所擁有的:

root = tk.Tk()
root.geometry("500x500")
song1 = AudioSegment.from_file("./song.mp3")

def current_value(event):
    song1 + slider.get()
    #song1.volume = event
    print(event)

slider = ttk.Scale(root, from_=-50, to=50, orient="horizontal",command=current_value)
slider.place(rely=.5, relx=.5)

def play_music(song):
    play(song)

thread1 = threading.Thread(target=play_music, args=(song1,))
thread1.start()

root.mainloop()
#This mostly just lags the audio file

修復:

from pycaw.pycaw import AudioUtilities
class AudioController:
    def __init__(self, process_name):
        self.process_name = process_name
        self.volume = self.process_volume()

    def process_volume(self):
        sessions = AudioUtilities.GetAllSessions()
        for session in sessions:
            interface = session.SimpleAudioVolume
            if session.Process and session.Process.name() == self.process_name:
                #print("Volume:", interface.GetMasterVolume())  # debug
                return interface.GetMasterVolume()

    def set_volume(self, decibels):
        sessions = AudioUtilities.GetAllSessions()
        for session in sessions:
            interface = session.SimpleAudioVolume
            if session.Process and session.Process.name() == self.process_name:
                # only set volume in the range 0.0 to 1.0
                self.volume = min(1.0, max(0.0, decibels))
                interface.SetMasterVolume(self.volume, None)
                #print("Volume set to", self.volume)  # debug

def volume_slider_controller(event):
    audio_controller = AudioController("python.exe") #will need to be punge.exe
    audio_controller.set_volume(float(event))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM