如何在 Windows 上摘下耳機時自動關閉聲音

如何在 Windows 上摘下耳機時自動關閉聲音

當您在播放音樂或觀看電影時不小心拔掉電腦耳機時,聲音將切換為透過電腦的外部揚聲器播放。如果你在辦公環境或擁擠的公共場所工作,就會影響到你周圍的每個人。然而,只需幾個設定步驟即可輕鬆解決所有問題。以下的文章將指導您如何在取下電腦上的耳機時自動關閉聲音。

拔下耳機時關閉聲音的說明

步驟1:

Windows + R組合鍵開啟「執行」對話方塊。然後輸入關鍵字記事本,點選確定開啟。

如何在 Windows 上摘下耳機時自動關閉聲音

第2步:

然後將下面的程式碼貼到記事本中,然後點擊“檔案”並選擇“另存為...”

[cmdletbinding()] Param() #Adding definitions for accessing the Audio API Add-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 care int 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(); // Unused int 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)); } } } '@ -Verbose While($true) { #Clean all events in the current session since its in a infinite loop, to make a fresh start when loop begins Get-Event | Remove-Event -ErrorAction SilentlyContinue #Registering the Event and Waiting for event to be triggered Register-WmiEvent -Class Win32_DeviceChangeEvent Wait-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 API If($EventType -eq 3) { [Audio]::Mute = $true Write-Verbose "Muted [$((Get-Date).tostring())]" } elseif($EventType -eq 2 -and [Audio]::Mute -eq $true) { [Audio]::Mute = $false Write-Verbose "UnMuted [$((Get-Date).tostring())]" } }

如何在 Windows 上摘下耳機時自動關閉聲音

步驟3:

在文件保存介面中,保存類型部分選擇所有文件。然後輸入.ps1 格式的檔名。檔案名稱可以選擇不簽名並立即寫入,但必須採用 PS1 格式。最後點選Save儲存。

我們應該將文件保存在桌面上,以便需要時快速使用。

如何在 Windows 上摘下耳機時自動關閉聲音

步驟4:

右鍵單擊新建立的文件,然後選擇使用PowerShell運行以啟動該文件。該文件將起作用,當我們拔下電腦耳機時將聲音靜音,直到關閉電腦為止

如何在 Windows 上摘下耳機時自動關閉聲音

如果您希望該檔案在您開啟電腦時自動執行,您可以使用任務排程程式。如何使用任務規劃程式自動執行該文件,使用者可以參考下面的文章。

看更多:

祝您成功!


什麼是惡意軟體 HackTool:Win32/Keygen?如何去除?

什麼是惡意軟體 HackTool:Win32/Keygen?如何去除?

您是否掃描過您的設備並發現它感染了 HackTool:Win32/Keygen?在您使用破解或金鑰產生器啟動進階軟體後,Windows Defender 可能會自動警告您有關此惡意軟體的存在。

在 Windows 10 上建立備份和還原點(Restore Point)的說明

在 Windows 10 上建立備份和還原點(Restore Point)的說明

探索如何在Windows 10上建立和管理還原點,這是一項有效的系統保護功能,可以讓您快速還原系統至先前狀態。

在 Windows 10 中開啟裝置管理員的 15 種方法

在 Windows 10 中開啟裝置管理員的 15 種方法

您可以使用以下 15 種方法輕鬆在 Windows 10 中開啟<strong>裝置管理員</strong>,包括使用命令、捷徑和搜尋。

Windows「顯示桌面」的 10 種超快速方法

Windows「顯示桌面」的 10 種超快速方法

很多人不知道如何快速顯示桌面,只好將各個視窗一一最小化。這種方法對於用戶來說非常耗時且令人沮喪。因此,本文將向您介紹Windows中快速顯示桌面的十種超快速方法。

如何使用 Windows Repair 修復 Windows 錯誤

如何使用 Windows Repair 修復 Windows 錯誤

Windows Repair 是一款有效的 Windows 錯誤修復工具,幫助使用者修復與 Internet Explorer、Windows Update 和其他重要程式相關的錯誤。

如何在 Windows 10 中開啟資料夾選項或檔案總管選項

如何在 Windows 10 中開啟資料夾選項或檔案總管選項

您可以使用檔案總管選項來變更檔案和資料夾的工作方式並控制顯示。掌握如何在 Windows 10 中開啟資料夾選項的多種方法。

有關在 Windows 10 上刪除使用者帳戶的 5 種方法的說明

有關在 Windows 10 上刪除使用者帳戶的 5 種方法的說明

刪除不使用的使用者帳戶可以顯著釋放記憶體空間,並讓您的電腦運行得更流暢。了解如何刪除 Windows 10 使用者帳戶的最佳方法。

如何使用Bootsect /nt60將VBC更新為BOOTMGR

如何使用Bootsect /nt60將VBC更新為BOOTMGR

使用 bootsect 命令能夠輕鬆修復磁碟區引導程式碼錯誤,確保系統正常啟動,避免 hal.dll 錯誤及其他啟動問題。

Windows 11 中的存檔應用程式功能是什麼?應該啟用還是停用它?

Windows 11 中的存檔應用程式功能是什麼?應該啟用還是停用它?

存檔應用程式是一項功能,可以自動卸載您很少使用的應用程序,同時保留其關聯的檔案和設定。了解如何有效利用 Windows 11 中的存檔應用程式功能。

如何在 Windows 10 上為 BlueStacks 5 啟用虛擬化 (VT)

如何在 Windows 10 上為 BlueStacks 5 啟用虛擬化 (VT)

要啟用虛擬化,您必須先進入 BIOS 並從 BIOS 設定中啟用虛擬化,這將顯著提升在 BlueStacks 5 上的效能和遊戲體驗。