+5 votes
176 views
in Help by (242k points)
reopened
How to mute your Windows PC by disconnecting the headphones.

1 Answer

+3 votes
by (1.6m points)
edited
 
Best answer

How to make your Windows computer silent when we disconnect the headphones.

Few things bother me more than when I disconnect the headphones from my Windows computer, the speakers continue playing what I'm playing. This is exacerbated if I am in a public place at that time since I can disturb everyone around me. To avoid this, I usually pause the player before disconnecting my headphones, but I do not always remember to do it and you also have to have disconnections by mistake.

image

You may also be interested:  How to disable automatic hard disk maintenance in Windows 10

By default, Windows 10 does not incorporate any specific function that allows you to automatically mute your computer when you disconnect your headphones. However, you should know that the operating system will do is activate the last sound configuration you had before connecting your headphones..

That is, if for example our computer has sound activated at a volume of 25%, when we connect our headphones, Windows 10 activates a different and specific volume controller for them. However, when we disconnect our headphones automatically, the sound will continue to reproduce the audio through the speakers of your computer at a volume of 25%, which was the level we had set before connecting the headphones.

A trick that I usually do is to deactivate the sound from my computer speakers or set their volume to 0. In this way, when I disconnect my headphones, the sound will not be reproduced in our computer speakers..

Having said this, we want to show you step by step how to prevent the sound reproduced by our Windows 10 computer from sounding from your speakers after disconnecting our headphones:

How to make your Windows computer silent when we disconnect the headphones.

To ensure that when we disconnect our headphones from our computer the speakers continue to reproduce sound, we must make use of a Script which we can create ourselves.

image

To do this you just have to copy the following code and paste it in Notepad:

 [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)] IAudioEndpointVolume interface { // 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)] IMMDevice interface { 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 ())]" } } 

Once you have pasted the code into Notepad, you will have to click on the Files option for the row and then select the Save As option. This will bring up a window where you will have to select the option: All Files in the Type section . Now you can write the desired name to add to the end of it the extension: .PS1

image

Once the file is saved, we will have two options: 1) Run the Script manually by right-clicking on the file and selecting the option: run with PowerShell..

2) Run the Script automatically every time the Windows 10 operating system starts. To do this, we will have to create a .bat file ( Batch Script ). This file will cause the Script to run automatically. If you do not know how to create the Batch Script file, the following link will show the steps to follow: How to create a .bat file to execute commands in PowerShell automatically. (Windows)

Once the Script created is running manually or automatically, you will never have to worry about disconnecting your headphones from your computer again as the sound will automatically mute in the same way as Android or iPhone mobile phones do.


...