目录
需求
平时需要频繁更换默认播放设备,虽然可以手动打开控制面板–声音–修改默认播放设备.但是作为程序员需要一切皆自动!
通过查找资料 发现AudioDeviceCmdlets可以实现,特此记录!
第一步:下载AudioDeviceCmdlets.dll
第二步:安装Powershell
如果w10 可以跳过本步骤,直接下面就可以,如果是w7或者安装的Powershell版本标记老旧,需要更新Powershell
或者 运行Powershell命令报错:
无法将“Unblock-File”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查
名称的拼写,如果包括路径,请确保路径正确,然后重试。
也需要更新Powershell
第三步:准备python执行Powdshell脚本
import os
from glob import glob
import subprocess as sp
class PowerShell:
# from scapy
def __init__(self, coding, ):
cmd = [self._where('PowerShell.exe'),
"-NoLogo", "-NonInteractive", # Do not print headers
"-Command", "-"] # Listen commands from stdin
startupinfo = sp.STARTUPINFO()
startupinfo.dwFlags |= sp.STARTF_USESHOWWINDOW
self.popen = sp.Popen(cmd, stdout=sp.PIPE, stdin=sp.PIPE, stderr=sp.STDOUT, startupinfo=startupinfo)
self.coding = coding
def __enter__(self):
return self
def __exit__(self, a, b, c):
self.popen.kill()
def run(self, cmd, timeout=15):
b_cmd = cmd.encode(encoding=self.coding)
try:
b_outs, errs = self.popen.communicate(b_cmd, timeout=timeout)
except sp.TimeoutExpired:
self.popen.kill()
b_outs, errs = self.popen.communicate()
outs = b_outs.decode(encoding=self.coding)
return outs, errs
@staticmethod
def _where(filename, dirs