【问题标题】:Using Python to control system audio i [Voice Control]使用Python控制系统音频i【语音控制】
【发布时间】:2020-12-23 09:44:44
【问题描述】:

我正在编写一个程序来控制我的系统音量增加或减少语音命令。但我不知道应该如何安装所有软件包以及应该安装哪些软件包?我在 pycharm 中使用 python 3.7.0

【问题讨论】:

    标签: python-3.x audio voice-control


    【解决方案1】:

    我不确定,但你可能可以使用 tkinter。我创建了一个复制 GUI 文件,所以只需复制此代码并粘贴到 python 中:

    from tkinter import  *
    
    try:
        root=Tk()
        root.title("file copier")
    
        fname=mystring=StringVar()
    
        root.geometry = Canvas(root, width = 3000, height = 3000)
        label=Label(root,text="enter the file name with file extension", fg="black") .grid(row=0, column=0,sticky=W)
        entry=Entry(root, textvariable=fname) .grid(row=0, column=1)
    
    
    #specifying the function going to be assigned to the button
        def duplicatefunction():
            print(fname.get())
            with open(fname.get(),"rb") as f:
                data = f.read()
            with open("copy file(rename this file and copy info inside).exe","wb") as f:
                f.write(data)
    
        
        button1=Button(text="duplicate file", command=duplicatefunction) .grid(row=0, column=2)
    
        root.mainloop()
    except FileNotFoundError:
        print("no such file found named", entry)
    

    所以如果你看到我在函数中的文件名后键入了 exe 扩展名。尝试在记事本中输入您的所有代码,然后从此文件转换为 exe(粘贴代码后保存文件并运行它),您可以通过更改扩展名来做到这一点,但您也应该复制,无论如何这个文件的代码仅用于您的参考和顺便说一句,复制文件将仅在 pycharm 中,如果您像我一样将它用于 python IDLE,它将出现在文件资源管理器中。通过复制文件来转换记事本文件,然后返回,输入你的音量代码并在最后使用此代码来控制音量:

    fname=StringVar()
    print(fname.get())
            with open(fname.get(),"rb") as f:
                data = f.read()
    

    所以数据应该被读取和工作,我希望它对我有用,在其他项目中。

    【讨论】:

      【解决方案2】:

      我在https://techoverflow.net/2020/04/04/how-to-set-windows-audio-volume-using-python/ 上发现了一些有趣的东西 但这仅适用于 Windows。

      他们使用的包是pycaw。您可以使用
      pip install pycaw 安装它。

      那是网站上的脚本:

      from ctypes import cast, POINTER
      from comtypes import CLSCTX_ALL
      from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
      import math
      # Get default audio device using PyCAW
      devices = AudioUtilities.GetSpeakers()
      interface = devices.Activate(
          IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
      volume = cast(interface, POINTER(IAudioEndpointVolume))
      # Get current volume 
      currentVolumeDb = volume.GetMasterVolumeLevel()
      volume.SetMasterVolumeLevel(currentVolumeDb - 6.0, None)
      # NOTE: -6.0 dB = half volume !
      

      如果我使用

      volume.SetMasterVolumeLevel(-65.25, None)
      

      我的系统音量设置为 0

      volume.SetMasterVolumeLevel(0, None)
      

      我可以把系统音量调到100

      【讨论】:

        猜你喜欢
        • 2011-05-18
        • 1970-01-01
        • 1970-01-01
        • 2012-10-25
        • 2011-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多