public void VolumeUp() { float volume = 0.0F; iAudioEndpoint.GetMasterVolumeLevelScalar(ref volume); volume = volume < 0.9F ? volume + 0.1F : 1F; Guid nullGuid = Guid.Empty; iAudioEndpoint.SetMasterVolumeLevelScalar(volume, nullGuid); }
public static float SetMasterVolume(float newValue) { // retrieve audio device... IMMDeviceEnumerator useenumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator()); IMMDevice speakers; const int eRender = 0; const int eMultimedia = 1; //retrieve the actual endpoint useenumerator.GetDefaultAudioEndpoint(eRender, ERole.eMultimedia, out speakers); object o; //retrieve the actual interface instance to retrieve the volume information from. speakers.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out o); IAudioEndpointVolume aepv = (IAudioEndpointVolume)o; float result; int hresult = aepv.GetMasterVolumeLevelScalar(out result); aepv.SetMasterVolumeLevelScalar(newValue, new System.Guid()); Marshal.ReleaseComObject(aepv); Marshal.ReleaseComObject(speakers); Marshal.ReleaseComObject(useenumerator); return(result); }
public float StepMasterVolume(float stepAmount) { IAudioEndpointVolume masterVol = null; try { masterVol = GetMasterVolumeObject(); if (masterVol == null) { return(-1); } float stepAmountScaled = stepAmount / 100; masterVol.GetMasterVolumeLevelScalar(out float volumeLevel); float newLevel = volumeLevel + stepAmountScaled; newLevel = Math.Min(1, newLevel); newLevel = Math.Max(0, newLevel); masterVol.SetMasterVolumeLevelScalar(newLevel, Guid.Empty); return(newLevel * 100); } finally { if (masterVol != null) { Marshal.ReleaseComObject(masterVol); } } }
/// <summary> /// Tries to set the volume to a specified level /// </summary> /// <returns>returns true if the keypress was sent, false if there was an error</returns> public bool SetVolume(int Volume) { try { IMMDeviceEnumerator enumerator = MMDeviceEnumeratorFactory.CreateInstance(); IMMDevice device; int eRender = 0; int eMultimedia = 1; enumerator.GetDefaultAudioEndpoint(eRender, eMultimedia, out device); object endpoint = null; device.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out endpoint); IAudioEndpointVolume audio = (IAudioEndpointVolume)endpoint; if (audio.SetMasterVolumeLevelScalar(Volume / 100f, new Guid()) != 0) { throw new Exception(); } return(true); } catch { return(false); } }
/// <summary> /// Increments or decrements the current volume level by the <see cref="stepAmount"/>. /// </summary> /// <param name="stepAmount">Value between -100 and 100 indicating the desired step amount. Use negative numbers to decrease /// the volume and positive numbers to increase it.</param> /// <returns>the new volume level assigned</returns> public static float StepMasterVolume(float stepAmount) { IAudioEndpointVolume masterVol = null; try { masterVol = GetMasterVolumeObject(); if (masterVol == null) { return(-1); } float stepAmountScaled = stepAmount / 100; // Get the level masterVol.GetMasterVolumeLevelScalar(out float volumeLevel); // Calculate the new level float newLevel = volumeLevel + stepAmountScaled; newLevel = Math.Min(1, newLevel); newLevel = Math.Max(0, newLevel); masterVol.SetMasterVolumeLevelScalar(newLevel, Guid.Empty); // Return the new volume level that was set return(newLevel * 100); } finally { if (masterVol != null) { Marshal.ReleaseComObject(masterVol); } } }
public void TestGetVolume() { var devices = AudioUtilities.GetAllActiveSpeakers(); var speakers = AudioUtilities.GetCurrentSpeakers(); Assert.IsNotNull(speakers); string id; Assert.IsTrue(speakers.GetId(out id) == 0); Assert.IsFalse(string.IsNullOrWhiteSpace(id)); IAudioEndpointVolume endpointVolume = AudioUtilities.GetAudioEndpointVolume(speakers); var device = AudioUtilities.CreateDevice(speakers); Assert.IsNotNull(device); float level; endpointVolume.GetMasterVolumeLevelScalar(out level); Assert.AreNotEqual(0, level); endpointVolume.SetMasterVolumeLevelScalar(level * 1.5f, Guid.NewGuid()); Playback.SetDefaultPlaybackDevice(devices.First(d => d.Id != id).Id); }
/// <summary> /// Sets the master volume to a specific level /// </summary> /// <param name="newLevel">Value between 0 and 100 indicating the desired scalar value of the volume</param> public static void SetMasterVolume(float newLevel) { if (newLevel > 100) { newLevel = 100; } if (newLevel < 0) { newLevel = 0; } IAudioEndpointVolume masterVol = null; try { masterVol = GetMasterVolumeObject(); if (masterVol == null) { return; } masterVol.SetMasterVolumeLevelScalar(newLevel / 100, Guid.Empty); } finally { if (masterVol != null) { Marshal.ReleaseComObject(masterVol); } } }
public static int SetMicrophoneMasterVolume(float volume) { if (volume < 0 || volume > 1) { throw new ArgumentOutOfRangeException("Provide volumet between 0 and 1"); } try { IMMDeviceEnumerator deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance(); IMMDevice microphone = null; deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eCommunications, out microphone); if (microphone != null) { object aepv_obj; microphone.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out aepv_obj); IAudioEndpointVolume aepv = (IAudioEndpointVolume)aepv_obj; Guid ZeroGuid = new Guid(); int res = aepv.SetMasterVolumeLevelScalar(volume, ZeroGuid); Console.WriteLine($"Audio microphone level set to {volume}%"); return(res); } } catch (Exception ex) { Console.WriteLine($"**Could not set microphone level** {ex.Message}"); return(ex.HResult); } return(1); // no mic }
public void SetAppAndSystemVolume(float volume, bool muted) { Guid guidEnumetator = typeof(IMMDeviceEnumerator).GUID; Guid guidManager = typeof(IAudioSessionManager).GUID; Guid guidVolume = typeof(IAudioEndpointVolume).GUID; CoCreateInstance(ref MMDeviceEnumerator, null, CLSCTX_ALL, ref guidEnumetator, out IUnknown _enumerater); IMMDeviceEnumerator enumerator = _enumerater as IMMDeviceEnumerator; enumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out IMMDevice endpoint); endpoint.Activate(ref guidManager, CLSCTX_ALL, IntPtr.Zero, out IUnknown _manager); endpoint.Activate(ref guidVolume, CLSCTX_ALL, IntPtr.Zero, out IUnknown _volume); IAudioSessionManager manager = _manager as IAudioSessionManager; manager.GetSimpleAudioVolume(Guid.Empty, false, out ISimpleAudioVolume processvolume); processvolume.GetMasterVolume(out float prevolume); if (prevolume != volume) { processvolume.SetMasterVolume(volume, Guid.Empty); } processvolume.SetMute(muted, Guid.Empty); IAudioEndpointVolume systemvolume = _volume as IAudioEndpointVolume; systemvolume.SetMasterVolumeLevelScalar(muted?0:1, Guid.Empty); systemvolume.SetMute(muted, Guid.Empty); }
public static float StepMasterVolume(float stepAmount) { IAudioEndpointVolume audioEndpointVolume = null; try { audioEndpointVolume = GetMasterVolumeObject(); if (audioEndpointVolume == null) { return(-1f); } float num = stepAmount / 100f; audioEndpointVolume.GetMasterVolumeLevelScalar(out float level); float val = level + num; val = Math.Min(1f, val); val = Math.Max(0f, val); audioEndpointVolume.SetMasterVolumeLevelScalar(val, Guid.Empty); return(val * 100f); } finally { if (audioEndpointVolume != null) { Marshal.ReleaseComObject(audioEndpointVolume); } } }
/// <summary> /// Set Master Volume /// </summary> /// <param name="newValue"></param> public void SetMasterVolumeScalar(double newValue) { if (_audioEndpointVolume == null) { return; } _audioEndpointVolume.SetMasterVolumeLevelScalar((float)newValue, Guid.Empty); }
private void SetVolume(float volume) { IMMDevice speakers = AudioUtilities.GetCurrentSpeakers(); IAudioEndpointVolume currentEndpointVolume = AudioUtilities.GetAudioEndpointVolume(speakers); //as we work with volume as a value between 0-100 we need to convert back to a value between 0-1. //MasterVolumeLevelScalar capped at 100, any value above 100 is treated as 100. currentEndpointVolume.SetMasterVolumeLevelScalar(volume > 0 ? volume / 100 : 0, Guid.NewGuid()); }
public void SetMasterVolumeLevel(Single volumeLevel) { if (volumeLevel < 0.0 || volumeLevel > 1.0) { throw new ArgumentOutOfRangeException(nameof(volumeLevel)); } // set the master volume level var result = _audioEndpointVolume.SetMasterVolumeLevelScalar(volumeLevel, IntPtr.Zero); if (result != WindowsApi.S_OK) { // TODO: consider throwing more granular exceptions here throw new COMException("IAudioEndpointVolume.GetMasterVolumeLevelScalar failed", Marshal.GetExceptionForHR(result)); } }
public static void setVolume(int level) { IMMDeviceEnumerator deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance(); IMMDevice speakers; const int eRender = 0; const int eMultimedia = 1; deviceEnumerator.GetDefaultAudioEndpoint(eRender, eMultimedia, out speakers); object aepv_obj; speakers.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out aepv_obj); IAudioEndpointVolume aepv = (IAudioEndpointVolume)aepv_obj; Guid ZeroGuid = new Guid(); aepv.SetMasterVolumeLevelScalar(level / 100f, ZeroGuid); }
public static void SetMasterVolume(float newLevel) { IAudioEndpointVolume audioEndpointVolume = null; try { audioEndpointVolume = GetMasterVolumeObject(); audioEndpointVolume?.SetMasterVolumeLevelScalar(newLevel / 100f, Guid.Empty); } finally { if (audioEndpointVolume != null) { Marshal.ReleaseComObject(audioEndpointVolume); } } }
//Up default audio device volume (0-100) public static bool AudioVolumeUp(int targetStep, bool inputDevice) { try { IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator) new MMDeviceEnumerator(); IMMDevice.IMMDevice deviceItem = null; if (!inputDevice) { deviceItem = deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia); } else { deviceItem = deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia); } //Get the audio device volume endpoint deviceItem.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out object deviceActivated); IAudioEndpointVolume audioEndPointVolume = (IAudioEndpointVolume)deviceActivated; //Get the audio device volume audioEndPointVolume.GetMasterVolumeLevelScalar(out float volumeLevelCurrentFloat); float volumeLevelFloat = volumeLevelCurrentFloat + (targetStep / 100F); //Check the target volume if (volumeLevelFloat > 1.00) { volumeLevelFloat = 1.00F; } if (volumeLevelFloat < 0.00) { volumeLevelFloat = 0.00F; } //Change the audio device volume audioEndPointVolume.SetMasterVolumeLevelScalar(volumeLevelFloat, Guid.Empty); Debug.WriteLine("Up volume: " + targetStep + "% / " + volumeLevelFloat); return(true); } catch { Debug.WriteLine("Failed to up default audio device volume."); return(false); } }
public static bool SetVolume(int Level) { if (!OK) { return(false); } try { Guid ZeroGuid = new Guid(); aepv.SetMasterVolumeLevelScalar(Level / 100f, ZeroGuid); return(true); } catch (Exception e) { CatchException(e); return(false); } }
public static void SetVol(int Level) { try { IMMDeviceEnumerator deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance(); IMMDevice speakers; const int eRender = 0; const int eMultimedia = 1; deviceEnumerator.GetDefaultAudioEndpoint(eRender, eMultimedia, out speakers); object aepv_obj; speakers.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out aepv_obj); IAudioEndpointVolume aepv = (IAudioEndpointVolume)aepv_obj; Guid ZeroGuid = new Guid(); int res = aepv.SetMasterVolumeLevelScalar(Level / 100f, ZeroGuid); MMDevice defaultDevice = new MMDeviceEnumerator().GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia); } catch { Console.WriteLine("VolumeMixer.SetVol casting issue"); } }
/// <summary> /// 设置系统音量 /// </summary> /// <param name="volume">音量(0-100)</param> /// <returns></returns> public void SetVolume(int volume) { float level = 0.0f; if (volume >= 100) { level = 1f; } else if (volume < 100 && volume >= 0) { level = volume / 100.0f; } else { level = 0f; } //设置主音量 AudioEndpoint.SetMasterVolumeLevelScalar(level, Guid.NewGuid()); }
//Set default audio device volume (0-100) public static bool AudioVolumeSet(int targetVolume, bool inputDevice) { try { IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator) new MMDeviceEnumerator(); IMMDevice.IMMDevice deviceItem = null; if (!inputDevice) { deviceItem = deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia); } else { deviceItem = deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia); } //Get the audio device volume endpoint deviceItem.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out object deviceActivated); IAudioEndpointVolume audioEndPointVolume = (IAudioEndpointVolume)deviceActivated; //Check the target volume if (targetVolume > 100) { targetVolume = 100; } if (targetVolume < 0) { targetVolume = 0; } //Set the audio device volume float volumeLevelFloat = targetVolume / 100F; audioEndPointVolume.SetMasterVolumeLevelScalar(volumeLevelFloat, Guid.Empty); Debug.WriteLine("Set volume: " + targetVolume + "% / " + volumeLevelFloat); return(true); } catch { Debug.WriteLine("Failed to set default audio device volume."); return(false); } }
public static void SetMasterMicVolume(int level) { if (level < 0 || level > 100) { return; } IMMDeviceEnumerator deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance(); IMMDevice speakers; const int eCapture = 1; const int eMultimedia = 1; deviceEnumerator.GetDefaultAudioEndpoint(eCapture, eMultimedia, out speakers); object aepv_obj; speakers.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out aepv_obj); IAudioEndpointVolume aepv = (IAudioEndpointVolume)aepv_obj; Guid ZeroGuid = new Guid(); int res = aepv.SetMasterVolumeLevelScalar(level / 100f, ZeroGuid); }
public void setVolume(int soundPercentage) { IAudioEndpointVolume masterVol = null; try { masterVol = GetMasterVolumeObject(); if (masterVol == null) { return; } masterVol.SetMasterVolumeLevelScalar(soundPercentage / 100.0f, Guid.Empty); } finally { if (masterVol != null) { Marshal.ReleaseComObject(masterVol); } } }
/// <summary> /// Set the volume on the victims pc /// </summary> /// <param name="percentage">0-100</param> private static void SetVolume(string percentage) { try { float volume = float.Parse(percentage) / 100; keybd_event(VK_VOLUME_UP, MapVirtualKey(VK_VOLUME_UP, 0), KEYEVENTF_EXTENDEDKEY, 0); keybd_event(VK_VOLUME_UP, MapVirtualKey(VK_VOLUME_UP, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); IMMDeviceEnumerator deviceEnumerator = MMDeviceEnumeratorFactory.CreateInstance(); IMMDevice speakers; const int eRender = 0; const int eMultimedia = 1; deviceEnumerator.GetDefaultAudioEndpoint(eRender, eMultimedia, out speakers); object aepv_obj; speakers.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out aepv_obj); IAudioEndpointVolume aepv = (IAudioEndpointVolume)aepv_obj; Guid ZeroGuid = new Guid(); int res = aepv.SetMasterVolumeLevelScalar(volume, ZeroGuid); } catch (Exception) { } }
/* Set the Volume on Windows Vista by obtaining an * IAudioEndpointVolume COM object. */ public static void SetVolumeWindowsVista(int value) { object enumerator = null; Guid devEnumeratorGuid = new Guid("BCDE0395-E52F-467C-8E3D-C4579291692E"); Guid idevEnumeratorGuid = new Guid("00000000-0000-0000-C000-000000000046"); int CLSCTX_INPROC_SERVER = 1; CoInitialize(null); int result = CoCreateInstance(ref devEnumeratorGuid, null, CLSCTX_INPROC_SERVER, ref idevEnumeratorGuid, out enumerator); if (result != 0 || enumerator == null) { return; } IMMDeviceEnumerator devEnum = enumerator as IMMDeviceEnumerator; if (devEnum == null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(devEnum); return; } IntPtr devicePtr = IntPtr.Zero; int eRender = 0; int eConsole = 0; int ret = devEnum.GetDefaultAudioEndpoint(eRender, eConsole, ref devicePtr); if (ret != 0) { System.Runtime.InteropServices.Marshal.ReleaseComObject(devEnum); return; } object obj = System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(devicePtr); IMMDevice device = obj as IMMDevice; if (device == null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(devEnum); System.Runtime.InteropServices.Marshal.ReleaseComObject(device); } Guid iAudioEndPointVolGuid = new Guid("5CDF2C82-841E-4546-9722-0CF74078229A"); uint context = (uint)0x17; IntPtr activationParams = IntPtr.Zero; IntPtr endPoint = IntPtr.Zero; ret = device.Activate(ref iAudioEndPointVolGuid, context, activationParams, ref endPoint); if (ret != 0) { System.Runtime.InteropServices.Marshal.ReleaseComObject(devEnum); System.Runtime.InteropServices.Marshal.ReleaseComObject(device); return; } obj = System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(endPoint); IAudioEndpointVolume audioEndpoint = obj as IAudioEndpointVolume; if (audioEndpoint == null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(devEnum); System.Runtime.InteropServices.Marshal.ReleaseComObject(device); return; } Guid empty = Guid.Empty; audioEndpoint.SetMasterVolumeLevelScalar((float)(value / 100.0), empty); System.Runtime.InteropServices.Marshal.ReleaseComObject(audioEndpoint); System.Runtime.InteropServices.Marshal.ReleaseComObject(device); System.Runtime.InteropServices.Marshal.ReleaseComObject(devEnum); }