带有 PyDub 的 Tkinter 中的音量滑块

Volume slider in Tkinter with PyDub

通常对于音频,您可以通过以下方式访问音量: sound.volume = 10 但是对于 Pydub,可以使用以下方式访问该卷: 声音 + 10 这个问题是因为我不能准确 'set' 音量,只需调整歌曲当前的音量即可。我想创建一个 Tkinter 滑块,我可以在听不见和响亮之间变化。这是我目前所拥有的:

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))