internal static void MuteSpotify(bool mute) { ISimpleAudioVolume volume = GetSpotifyVolumeObject(); if (volume == null) { throw new COMException("Volume object creation failed"); } Guid guid = Guid.Empty; volume.SetMute(mute, ref guid); Marshal.ReleaseComObject(volume); }
public static void SetApplicationMute(int pid, bool mute) { ISimpleAudioVolume volume = GetVolumeObject(pid); if (volume == null) { return; } Guid guid = Guid.Empty; volume.SetMute(mute, ref guid); Marshal.ReleaseComObject(volume); }
/// <summary> /// ミュート状態を設定します。 /// </summary> /// <param name="processID">対象のプロセスID。</param> /// <param name="mute">ミュートするなら true。</param> public static void SetApplicationMute(uint processID, bool mute) { ISimpleAudioVolume volume = GetVolumeObject(processID); if (volume == null) { throw new ArgumentException(ErrorMessageNotFound); } Guid guid = Guid.Empty; volume.SetMute(mute, ref guid); Marshal.ReleaseComObject(volume); }
public static void SetApplicationVolume(int pid, float level) { ISimpleAudioVolume volume = GetVolumeObject(pid); if (volume == null) { return; } Guid guid = Guid.Empty; //volume.SetMasterVolume(level / 100, ref guid); volume.SetMute(true, ref guid); Marshal.ReleaseComObject(volume); }
internal static void MuteSpotify(bool mute) { Process[] p = Process.GetProcessesByName(SpotifyProcessName); if (p.Length == 0) { throw new Exception("Spotify process is not running or was not found!"); } int pid = p[0].Id; ISimpleAudioVolume volume = GetVolumeObject(pid); if (volume == null) { throw new COMException("Volume object creation failed"); } Guid guid = Guid.Empty; volume.SetMute(mute, ref guid); Marshal.ReleaseComObject(volume); }
/// <summary> /// Application entry point /// </summary> /// <param name="args">Command line arguments</param> public static void Main(string[] args) { try { // If we were called from a command line then attache to the parent console. // We are running as a Windows Application instead of a Console application but // we still want to be able to write to the console. AttachConsole(-1); if (args != null && args.Length >= 1 && args[0] != null && !args[0].Equals("-?")) { // First command line argument must contain the process name or -? Process selectedProcess = null; ISimpleAudioVolume volumeControl = null; FindMatchingProcess(args[0], out selectedProcess, out volumeControl); if (selectedProcess != null) { Console.WriteLine("Selected Process: " + selectedProcess.ProcessName); // Loop through the rest of the parameters and send the message for (int argIndex = 1; argIndex < args.Length; argIndex++) { switch (args[argIndex]) { case "-pp": { SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_MEDIA_PLAY_PAUSE); Console.WriteLine("Message Sent: Play/Pause"); break; } case "-p": { SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_MEDIA_PLAY); Console.WriteLine("Message Sent: Play"); break; } case "-pa": { SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_MEDIA_PAUSE); Console.WriteLine("Message Sent: Pause"); break; } case "-s": { SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_MEDIA_STOP); Console.WriteLine("Message Sent: Stop"); break; } case "-vm": { if (volumeControl != null) { // Send using the Core Audio API bool muted; // Get the current state volumeControl.GetMute(out muted); // Toggle the state volumeControl.SetMute(!muted, Guid.Empty); } else { SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_VOLUME_MUTE); } Console.WriteLine("Message Sent: Volume Mute"); break; } case "-vu": { if (volumeControl != null) { float currentVolume; // Get the Current Volume Level volumeControl.GetMasterVolume(out currentVolume); // Increment the volume if (currentVolume + VolumeIncrement >= MaxVolume) { volumeControl.SetMasterVolume(MaxVolume, Guid.Empty); } else { volumeControl.SetMasterVolume(currentVolume + VolumeIncrement, Guid.Empty); } } else { SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_VOLUME_UP); } Console.WriteLine("Message Sent: Volume Up"); break; } case "-vd": { if (volumeControl != null) { float currentVolume; // Get the current volume level volumeControl.GetMasterVolume(out currentVolume); // Decrement the volume if (currentVolume - VolumeIncrement <= MinVolume) { volumeControl.SetMasterVolume(MinVolume, Guid.Empty); } else { volumeControl.SetMasterVolume(currentVolume - VolumeIncrement, Guid.Empty); } } else { SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_VOLUME_DOWN); } Console.WriteLine("Message Sent: Volume Down"); break; } case "-mvm": { SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_VOLUME_MUTE); Console.WriteLine("Message Sent: Master Volume Mute"); break; } case "-mvu": { SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_VOLUME_UP); Console.WriteLine("Message Sent: Master Volume Up"); break; } case "-mvd": { SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_VOLUME_DOWN); Console.WriteLine("Message Sent: Master Volume Down"); break; } case "-nt": { SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_MEDIA_NEXTTRACK); Console.WriteLine("Message Sent: Next Track"); break; } case "-pt": { SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_MEDIA_PREVIOUSTRACK); Console.WriteLine("Message Sent: Previous Track"); break; } case "-?": { DisplayUsage(); break; } default: { Console.WriteLine("Ignoring unrecognized command: " + args[argIndex]); break; } } } } else { Console.WriteLine("No process with the name " + args[0] + " is currently running."); } if (volumeControl != null) { Marshal.ReleaseComObject(volumeControl); } } else { DisplayUsage(); } } catch (Exception e) { Console.WriteLine("Exception occurred:\r\n" + e.ToString()); } }
public static void SetMute(ISimpleAudioVolume v, bool mute) { v?.SetMute(mute, Guid.Empty); }
public static void SetApplicationMute(int pid, bool?mute) { if (mute == null) { return; } { IMMDeviceEnumerator deviceEnumerator = null; IAudioSessionEnumerator sessionEnumerator = null; IAudioSessionManager2 mgr = null; IMMDevice speakers = null; try { // get the speakers (1st render + multimedia) device deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator()); deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out speakers); // activate the session manager. we need the enumerator Guid IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID; object o; speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out o); mgr = (IAudioSessionManager2)o; // enumerate sessions for on this device mgr.GetSessionEnumerator(out sessionEnumerator); int count; sessionEnumerator.GetCount(out count); // search for an audio session with the required process-id ISimpleAudioVolume volumeControl = null; for (int i = 0; i < count; ++i) { IAudioSessionControl2 ctl = null; try { sessionEnumerator.GetSession(i, out ctl); // NOTE: we could also use the app name from ctl.GetDisplayName() int cpid; ctl.GetProcessId(out cpid); if (cpid == pid) { Guid guid = Guid.Empty; volumeControl = ctl as ISimpleAudioVolume; volumeControl.SetMute(Convert.ToBoolean(mute), ref guid); } } finally { if (ctl != null) { Marshal.ReleaseComObject(ctl); } } } } finally { if (speakers != null) { Marshal.ReleaseComObject(speakers); } if (mgr != null) { Marshal.ReleaseComObject(mgr); } if (sessionEnumerator != null) { Marshal.ReleaseComObject(sessionEnumerator); } if (deviceEnumerator != null) { Marshal.ReleaseComObject(deviceEnumerator); } } } }
public void SetApplicationMute(ISimpleAudioVolume volume, bool mute) { Guid guid = Guid.Empty; volume.SetMute(mute, ref guid); }
public static void SetApplicationMute(int pid, bool?mute) { if (mute == null) { return; } { IMMDeviceEnumerator deviceEnumerator = null; IAudioSessionEnumerator sessionEnumerator = null; IAudioSessionManager2 mgr = null; IMMDevice speakers = null; try { deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance(); deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out speakers); Guid IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID; speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out object o); mgr = (IAudioSessionManager2)o; mgr.GetSessionEnumerator(out sessionEnumerator); sessionEnumerator.GetCount(out int count); ISimpleAudioVolume volumeControl = null; for (int i = 0; i < count; ++i) { IAudioSessionControl2 ctl = null; try { sessionEnumerator.GetSession(i, out ctl); ctl.GetProcessId(out int cpid); if (cpid == pid) { Guid guid = Guid.Empty; volumeControl = ctl as ISimpleAudioVolume; volumeControl.SetMute(Convert.ToBoolean(mute), ref guid); } } finally { if (ctl != null) { Marshal.ReleaseComObject(ctl); } } } } catch { } finally { if (speakers != null) { Marshal.ReleaseComObject(speakers); } if (mgr != null) { Marshal.ReleaseComObject(mgr); } if (sessionEnumerator != null) { Marshal.ReleaseComObject(sessionEnumerator); } if (deviceEnumerator != null) { Marshal.ReleaseComObject(deviceEnumerator); } } } }
public void SetMute(bool isMuted) { _SimpleAudioVolume.SetMute(isMuted, Guid.NewGuid()); }
public void SetApplicationMute(bool IsMute) { ISimpleAudioVolume simpleAudio = SessionControl2 as ISimpleAudioVolume; simpleAudio.SetMute(IsMute, Guid.Empty); }