public bool SetVolume(float Value) { if (volume == null) { return(false); } return(volume.SetMasterVolume(Value, Guid.Empty) == 0); }
public static void SetApplicationVolume(string name, float level) { ISimpleAudioVolume volume = GetVolumeObject(name); if (volume == null) return; Guid guid = Guid.Empty; volume.SetMasterVolume(level / 100, ref guid); }
public static void SetVolume(ISimpleAudioVolume v, float level) { if (v == null) { return; } v.SetMasterVolume(level / 100, Guid.Empty); }
public static void SetApplicationVolume(int pid, float level) { ISimpleAudioVolume volumeObject = GetVolumeObject(pid); if (volumeObject != null) { Guid EventContext = Guid.Empty; volumeObject.SetMasterVolume(level / 100f, ref EventContext); Marshal.ReleaseComObject(volumeObject); } }
public static void SetVolume(uint pid, float level) { ISimpleAudioVolume volume = GetAudioVolumeObject(pid); if (volume != null) { Guid guid = Guid.Empty; volume.SetMasterVolume(level / 100, ref guid); Marshal.ReleaseComObject(volume); } }
public static void SetApplicationVolume(int target_process_id, float level) { ISimpleAudioVolume volume = GetVolumeObject(target_process_id); if (volume == null) { return; } Guid guid = Guid.Empty; volume.SetMasterVolume(level / 100, ref guid); }
// Set the volume to level public static void SetApplicationVolume(ISimpleAudioVolume volume, float level) { if (volume == null) { return; } Guid guid = Guid.Empty; volume.SetMasterVolume(level / 100, ref guid); Marshal.ReleaseComObject(volume); }
private static void LowLevelSetApplicationVolume(int pid, float level) { ISimpleAudioVolume volume = GetVolumeObject(pid); if (volume == null) { return; } Guid guid = Guid.Empty; volume.SetMasterVolume(level / 100, ref guid); }
public void SetApplicationVolume(uint pid, int level) { ISimpleAudioVolume volume = GetVolumeObject(pid); if (volume == null) { return; } Guid guid = Guid.Empty; volume.SetMasterVolume((float)level / 100, ref guid); }
public static void SetApplicationVolume(uint name, float volum) { ISimpleAudioVolume volume = GetVolumeObject(name); if (volume == null) { return; } Guid guid = Guid.Empty; volume.SetMasterVolume(volum, ref guid); }
public static void SetApplicationVolume(int pid, float level) { ISimpleAudioVolume volume = VolumeMixer.GetCoreAudioObject <ISimpleAudioVolume>(pid); if (volume == null) { return; } Guid guid = Guid.Empty; volume.SetMasterVolume(level / 100, ref guid); Marshal.ReleaseComObject(volume); }
internal static void SetSpotifyVolume(float level) { ISimpleAudioVolume volume = GetSpotifyVolumeObject(); if (volume == null) { throw new COMException("Volume object creation failed"); } Guid guid = Guid.Empty; volume.SetMasterVolume(level / 100, ref guid); Marshal.ReleaseComObject(volume); }
/// <summary> /// 音量を設定します。 /// </summary> /// <param name="processID">対象のプロセスID。</param> /// <param name="level">音量( 0.0 - 1.0 )。</param> public static void SetApplicationVolume(uint processID, float level) { ISimpleAudioVolume volume = GetVolumeObject(processID); if (volume == null) { throw new ArgumentException(ErrorMessageNotFound); } Guid guid = Guid.Empty; volume.SetMasterVolume(level, ref guid); Marshal.ReleaseComObject(volume); }
public static void SetApplicationVolume(int pid, float level) { ISimpleAudioVolume volume = GetVolumeObject(pid); //Console.WriteLine("vol object pid " + pid + " = "+ volume); if (volume == null) { //ConsoleManager.Show(); Console.WriteLine("processId " + pid + " is not associated with an audio session, Volume not changed!"); return; } Guid guid = Guid.Empty; volume.SetMasterVolume(level / 100, ref guid); Marshal.ReleaseComObject(volume); }
public static void SetApplicationVolume(int procID, float level) { ISimpleAudioVolume volume = GetVolumeObject(procID); if (level < 1) { level *= 100; } if (volume == null) { return; } Guid guid = Guid.Empty; volume.SetMasterVolume(level / 100, guid); }
public static void SetAllApplicationVolumes(float level) { IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator()); IMMDevice speakers; deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out speakers); Guid IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID; object o; speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out o); IAudioSessionManager2 mgr = (IAudioSessionManager2)o; IAudioSessionEnumerator sessionEnumerator; mgr.GetSessionEnumerator(out sessionEnumerator); int count; sessionEnumerator.GetCount(out count); ISimpleAudioVolume volumeControl = null; for (int i = 0; i < count; i++) { IAudioSessionControl ctl; sessionEnumerator.GetSession(i, out ctl); string dn; ctl.GetDisplayName(out dn); volumeControl = ctl as ISimpleAudioVolume; if (volumeControl != null) { Guid guid = Guid.Empty; volumeControl.SetMasterVolume(level / 100, ref guid); } Marshal.ReleaseComObject(ctl); } Marshal.ReleaseComObject(sessionEnumerator); Marshal.ReleaseComObject(mgr); Marshal.ReleaseComObject(speakers); Marshal.ReleaseComObject(deviceEnumerator); }
internal static void SetSpotifyVolume(float level) { 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.SetMasterVolume(level / 100, 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; try { volume.SetMasterVolume(level / 100, ref guid); } finally { if (volume != null) { Marshal.ReleaseComObject(volume); } } }
/// <summary> /// 设置音量 /// </summary> /// <param name="type"></param> /// <param name="processName"></param> /// <param name="volumeLevel"></param> public static void SetVolume(string type, string processName, float volumeLevel, ref float adVolumeLevel) { try { // get the speakers (1st render + multimedia) device IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator()); IMMDevice speakers; 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); IAudioSessionManager2 mgr = (IAudioSessionManager2)o; // enumerate sessions for on this device IAudioSessionEnumerator sessionEnumerator; mgr.GetSessionEnumerator(out sessionEnumerator); int count; sessionEnumerator.GetCount(out count); for (int i = 0; i < count; i++) { IAudioSessionControl ctl; IAudioSessionControl2 ctl2; sessionEnumerator.GetSession(i, out ctl); ctl2 = ctl as IAudioSessionControl2; uint pid = 0; string sout1 = ""; string sout2 = ""; if (ctl2 != null) { ctl2.GetSessionIdentifier(out sout1); ctl2.GetProcessId(out pid); ctl2.GetSessionInstanceIdentifier(out sout2); } if (type == "ad") { if (sout2.ToUpper().Contains(processName.ToUpper())) { ISimpleAudioVolume volumeControl = null; volumeControl = ctl as ISimpleAudioVolume; Guid guid = Guid.Empty; volumeControl.GetMasterVolume(out adVolumeLevel); volumeControl.SetMasterVolume((float)volumeLevel, ref guid); } } else if (type == "voice") { if (sout2.ToUpper().Contains(processName.ToUpper())) { ISimpleAudioVolume volumeControl = null; volumeControl = ctl as ISimpleAudioVolume; Guid guid = Guid.Empty; volumeControl.SetMasterVolume((float)volumeLevel, ref guid); } } if (ctl != null) { Marshal.ReleaseComObject(ctl); } if (ctl2 != null) { Marshal.ReleaseComObject(ctl2); } } if (sessionEnumerator != null) { Marshal.ReleaseComObject(sessionEnumerator); } if (mgr != null) { Marshal.ReleaseComObject(mgr); } if (speakers != null) { Marshal.ReleaseComObject(speakers); } if (deviceEnumerator != null) { Marshal.ReleaseComObject(deviceEnumerator); } } catch (Exception ex) { WriteLog.WriteErrorLogToFile(string.Format("日志定位Controller.cs-SetVolume(string type, string processName, float volumeLevel, ref float adVolumeLevel)判断是否存在进程错误-错误信息:{0} {1}", ex.Message.ToString(), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")), true); throw ex; } }
public static void SetApplicationMute(string procname, bool mute) { IAudioSessionManager2 mgr = null; IAudioSessionEnumerator sessionEnumerator = null; try { ISimpleAudioVolume obj = null; mgr = GetAudioSessionManager(); if (mgr == null) { return; } mgr.GetSessionEnumerator(out sessionEnumerator); int count; sessionEnumerator.GetCount(out count); for (int i = 0; i < count; i++) { IAudioSessionControl ctl; sessionEnumerator.GetSession(i, out ctl); if (ctl == null) { continue; } IAudioSessionControl2 ctl2 = ctl as IAudioSessionControl2; if (ctl2 != null) { try { if (new AudioSession(ctl2).Process.ProcessName.ToLower().Equals(procname)) { obj = ctl2 as ISimpleAudioVolume; if (mute) { //obj.SetMute(mute, Guid.Empty); try { obj.GetMasterVolume(out volume); // Save old volume } catch (Exception e) { } obj.SetMasterVolume(0f, Guid.Empty); } else { obj.SetMasterVolume(volume, Guid.Empty); } } } catch (Exception e) { Console.WriteLine(e); } } } Marshal.ReleaseComObject(sessionEnumerator); Marshal.ReleaseComObject(mgr); } catch (Exception spotify_hire_me) { Console.WriteLine(spotify_hire_me); } finally { Marshal.ReleaseComObject(sessionEnumerator); Marshal.ReleaseComObject(mgr); } return; }
public static void SetApplicationVolume(int?pid, float?volume) { if (pid == null || volume == 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.SetMasterVolume((float)volume, ref guid); } } finally { if (ctl != null) { Marshal.ReleaseComObject(ctl); } } } } catch { Debug.WriteLine("Major Error"); // If it gets to this point something is very wrong } 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 static void SetApplicationVolume(int pid, float?level) { if (level == 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.SetMasterVolume(Convert.ToSingle(level) / 100, 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 SetApplicationVolume(ISimpleAudioVolume volume, float level) { Guid guid = Guid.Empty; volume.SetMasterVolume(level / 100, ref guid); }
/// <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()); } }
/// <summary> /// 获取正在使用音频的进程 /// </summary> /// <param name="processName"></param> /// <param name="volumeLevel"></param> /// <returns></returns> public static IEnumerable <string> EnumerateApplications(string processName, float volumeLevel) { // get the speakers (1st render + multimedia) device IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator()); IMMDevice speakers; 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); IAudioSessionManager2 mgr = (IAudioSessionManager2)o; // enumerate sessions for on this device IAudioSessionEnumerator sessionEnumerator; mgr.GetSessionEnumerator(out sessionEnumerator); int count; sessionEnumerator.GetCount(out count); for (int i = 0; i < count; i++) { IAudioSessionControl ctl; IAudioSessionControl2 ctl2; sessionEnumerator.GetSession(i, out ctl); ctl2 = ctl as IAudioSessionControl2; string dn; uint pid = 0; string sout1 = ""; string sout2 = ""; if (ctl2 != null) { ctl2.GetSessionIdentifier(out sout1); ctl2.GetProcessId(out pid); ctl2.GetSessionInstanceIdentifier(out sout2); } if (sout2.Contains(processName)) { ISimpleAudioVolume volumeControl = null; volumeControl = ctl as ISimpleAudioVolume; Guid guid = Guid.Empty; volumeControl.SetMasterVolume((float)volumeLevel, ref guid); } //ctl.GetDisplayName(out dn); // ctl2.GetProcessId(out pid); //yield return pid.ToString(); yield return(pid.ToString() + ": " + sout1 + " :: " + sout2); if (ctl != null) { Marshal.ReleaseComObject(ctl); } if (ctl2 != null) { Marshal.ReleaseComObject(ctl2); } } if (sessionEnumerator != null) { Marshal.ReleaseComObject(sessionEnumerator); } if (mgr != null) { Marshal.ReleaseComObject(mgr); } if (speakers != null) { Marshal.ReleaseComObject(speakers); } if (deviceEnumerator != null) { Marshal.ReleaseComObject(deviceEnumerator); } }