如果在每次拔下耳机时都可以自动使计算机的音频输出静音,那会不会很好?这样可以防止在早晨的几个小时内意外唤醒您的室友,或在公共场合误解您对布兰妮·斯皮尔斯的爱。
当然,您可以根据情况将扬声器和耳机静音使用音量混合器。 (了解有关在Windows 10中自定义声音的更多信息。)但是,老实说,当您可以使用PowerShell自动执行此操作时,无需这样做。
以下是在删除PC时自动静音PC音频的方法。耳机,就像智能手机一样。
要开始使用,请打开记事本。然后,将以下代码粘贴到空白文档中:
[cmdletbinding()]Param()#Adding definitions for accessing the Audio APIAdd-Type -TypeDefinition @'using System.Runtime.InteropServices;[Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]interface IAudioEndpointVolume {// f(), g(), ... are unused COM method slots. Define these if you careint f(); int g(); int h(); int i();int SetMasterVolumeLevelScalar(float fLevel, System.Guid pguidEventContext);int j();int GetMasterVolumeLevelScalar(out float pfLevel);int k(); int l(); int m(); int n();int SetMute([MarshalAs(UnmanagedType.Bool)] bool bMute, System.Guid pguidEventContext);int GetMute(out bool pbMute);}[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]interface IMMDevice {int Activate(ref System.Guid id, int clsCtx, int activationParams, out IAudioEndpointVolume aev);}[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]interface IMMDeviceEnumerator {int f(); // Unusedint GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice endpoint);}[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] class MMDeviceEnumeratorComObject { }public class Audio {static IAudioEndpointVolume Vol() {var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;IMMDevice dev = null;Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(/*eRender*/ 0, /*eMultimedia*/ 1, out dev));IAudioEndpointVolume epv = null;var epvid = typeof(IAudioEndpointVolume).GUID;Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));return epv;}public static float Volume {get {float v = -1; Marshal.ThrowExceptionForHR(Vol().GetMasterVolumeLevelScalar(out v)); return v;}set {Marshal.ThrowExceptionForHR(Vol().SetMasterVolumeLevelScalar(value, System.Guid.Empty));}}public static bool Mute {get { bool mute; Marshal.ThrowExceptionForHR(Vol().GetMute(out mute)); return mute; }set { Marshal.ThrowExceptionForHR(Vol().SetMute(value, System.Guid.Empty)); }}}'@ -VerboseWhile($true){#Clean all events in the current session since its in a infinite loop, to make a fresh start when loop beginsGet-Event | Remove-Event -ErrorAction SilentlyContinue#Registering the Event and Waiting for event to be triggeredRegister-WmiEvent -Class Win32_DeviceChangeEventWait-Event -OutVariable Event |Out-Null$EventType = $Event.sourceargs.newevent | `Sort-Object TIME_CREATED -Descending | `Select-Object EventType -ExpandProperty EventType -First 1#Conditional logic to handle, When to Mute/unMute the machine using Audio APIIf($EventType -eq 3) {[Audio]::Mute = $trueWrite-Verbose "Muted [$((Get-Date).tostring())]"}elseif($EventType -eq 2 -and [Audio]::Mute -eq $true){[Audio]::Mute = $falseWrite-Verbose "UnMuted [$((Get-Date).tostring())]"}}
现在,您需要将文件保存为PS1格式。在查看“保存文件"对话框时,从下拉菜单中选择所有文件,然后将文件命名为 AutoMute.ps1 。文件本身的名称并不重要,因此只需选择一个容易记住的名称即可。
要激活脚本,请右键单击新创建的文件,然后选择运行。该脚本将一直处于活动状态,直到您关闭计算机。
这只是PowerShell可以提高生产力的众多方式之一。
图片来源:peus / Depositphotos
标签: