public static AudioDevice SetById(string id) { var(DevEnum, DeviceCollection) = GetAudioDevice.GetDeviceCollection(); // For every MMDevice in DeviceCollection for (int i = 0; i < DeviceCollection.Count; i++) { // If this MMDevice's ID is the same as the string received by the ID parameter if (string.Compare(DeviceCollection[i].ID, id, System.StringComparison.CurrentCultureIgnoreCase) == 0) { // Create a new audio PolicyConfigClient PolicyConfigClient client = new PolicyConfigClient(); // Using PolicyConfigClient, set the given device as the default communication device (for its type) client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eCommunications); // Using PolicyConfigClient, set the given device as the default device (for its type) client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eMultimedia); // Output the result of the creation of a new AudioDevice while assining it the index, and the MMDevice itself, and a default value of true return(new AudioDevice(i + 1, DeviceCollection[i], true)); } } // Throw an exception about the received ID not being found throw new System.ArgumentException("No enabled AudioDevice found with that ID"); }
public static AudioDevice SetByInputObject(AudioDevice inputObject) { if (inputObject == null) { throw new ArgumentNullException(nameof(inputObject)); } var(DevEnum, DeviceCollection) = GetAudioDevice.GetDeviceCollection(); for (int i = 0; i < DeviceCollection.Count; i++) { var tempDevice = DeviceCollection[i]; // If this MMDevice's ID is the same as the ID of the MMDevice received by the InputObject parameter if (DeviceCollection[i].ID == inputObject.ID) { // Create a new audio PolicyConfigClient PolicyConfigClient client = new PolicyConfigClient(); // Using PolicyConfigClient, set the given device as the default playback communication device client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eCommunications); // Using PolicyConfigClient, set the given device as the default playback device client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eMultimedia); // Output the result of the creation of a new AudioDevice while assining it the an index, and the MMDevice itself, and a default value of true return(new AudioDevice(i + 1, DeviceCollection[i], true)); } } // Throw an exception about the received device not being found throw new System.ArgumentException("No such enabled AudioDevice found"); }
static void SetProperty(string devID, EPKey ek, Guid formatId, uint propertyId, PropVariant pv) { var config = new PolicyConfigClient(); var pk = new _tagpropertykey(); pk.fmtid = guidEnhancements; pk.pid = propLoudnessEqualization; config.SetPropertyValue(devID, EPKey.eFXKey, ref pk, ref pv.InnerPV); }
static PropVariant GetProperty(string devID, EPKey ek, Guid formatId, uint propertyId) { var config = new PolicyConfigClient(); var propvar = new PropVariant(); var pk = new _tagpropertykey(); pk.fmtid = formatId; pk.pid = propertyId; config.GetPropertyValue(devID, EPKey.eFXKey, ref pk, ref propvar.InnerPV); return(propvar); }
//Set a new default audio device public static bool SetDefaultDevice(string deviceId) { try { PolicyConfigClient autoPolicyConfigClient = new PolicyConfigClient(); autoPolicyConfigClient.SetDefaultEndpoint(deviceId, ERole.eMultimedia); Debug.WriteLine("Changed default audio device: " + deviceId); return(true); } catch { Debug.WriteLine("Failed to set new default audio device: " + deviceId); return(false); } }
public static AudioDevice SetByIndex(int index) { var(DevEnum, DeviceCollection) = GetAudioDevice.GetDeviceCollection(); // If the Index is valid if (index >= 1 && index <= DeviceCollection.Count) { // Create a new audio PolicyConfigClient PolicyConfigClient client = new PolicyConfigClient(); // Using PolicyConfigClient, set the given device as the default communication device (for its type) client.SetDefaultEndpoint(DeviceCollection[index - 1].ID, ERole.eCommunications); // Using PolicyConfigClient, set the given device as the default device (for its type) client.SetDefaultEndpoint(DeviceCollection[index - 1].ID, ERole.eMultimedia); // Output the result of the creation of a new AudioDevice while assining it the index, and the MMDevice itself, and a default value of true return(new AudioDevice(index, DeviceCollection[index - 1], true)); } else { // Throw an exception about the received Index not being found throw new System.ArgumentException("No enabled AudioDevice found with that Index"); } }
static void Main(string[] args) { MMDeviceEnumerator DevEnum = new MMDeviceEnumerator(); MMDeviceCollection devices = DevEnum.EnumerateAudioEndPoints(EDataFlow.eCapture, EDeviceState.DEVICE_STATE_ACTIVE); MMDevice DefaultDevice = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia); MMDevice SecondaryDevice = null; PolicyConfigClient client = new PolicyConfigClient(); Console.WriteLine("-----------------------------------"); Console.WriteLine("List of sound cards installed"); Console.WriteLine("-----------------------------------"); for (int i = 0; i < devices.Count; i++) { if (devices[i].ID != DefaultDevice.ID) { SecondaryDevice = devices[i]; } Console.WriteLine(devices[i].FriendlyName); Console.ReadLine(); } Console.WriteLine("Default Device"); Console.WriteLine("-----------------------------------"); Console.WriteLine(DefaultDevice.FriendlyName); Console.ReadLine(); Console.WriteLine("Secondary Device"); Console.WriteLine("-----------------------------------"); Console.WriteLine(SecondaryDevice.FriendlyName); Console.ReadLine(); client.SetDefaultEndpoint(SecondaryDevice.ID, ERole.eCommunications); client.SetDefaultEndpoint(SecondaryDevice.ID, ERole.eMultimedia); client.SetDefaultEndpoint(SecondaryDevice.ID, ERole.eConsole); DefaultDevice = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia); Console.WriteLine("New Default Device"); Console.WriteLine("-----------------------------------"); Console.WriteLine(DefaultDevice.FriendlyName); Console.ReadLine(); }
protected override void ProcessRecord() { MMDeviceEnumerator DevEnum = new MMDeviceEnumerator(); MMDeviceCollection devices = DevEnum.EnumerateAudioEndPoints(EDataFlow.eRender, EDeviceState.DEVICE_STATE_ACTIVE); PolicyConfigClient client = new PolicyConfigClient(); if (!string.IsNullOrEmpty(name)) { for (int i = 0; i < devices.Count; i++) { if (string.Compare(devices[i].FriendlyName, name, StringComparison.CurrentCultureIgnoreCase) == 0) { index = i; break; } } } if (inputObject != null) { for (int i = 0; i < devices.Count; i++) { if (devices[i].ID == inputObject.Device.ID) { index = i; break; } } } client.SetDefaultEndpoint(devices[index].ID, ERole.eCommunications); client.SetDefaultEndpoint(devices[index].ID, ERole.eMultimedia); WriteObject(new AudioDevice(index, devices[index])); }
public Audio() { _deviceEnumerator = new MMDeviceEnumerator(); _client = new PolicyConfigClient(); }
private void SetDefault(MMDevice mmDevice, DeviceRoles deviceRole) { var client = new PolicyConfigClient(); client.SetDefaultEndpoint(mmDevice.Id, deviceRole); }
// Cmdlet execution protected override void ProcessRecord() { // Create a new MMDeviceEnumerator MMDeviceEnumerator DevEnum = new MMDeviceEnumerator(); // Create a MMDeviceCollection of every devices that are enabled MMDeviceCollection DeviceCollection = DevEnum.EnumerateAudioEndPoints(EDataFlow.eAll, EDeviceState.DEVICE_STATE_ACTIVE); // If the InputObject parameter received a value if (inputObject != null) { // For every MMDevice in DeviceCollection for (int i = 0; i < DeviceCollection.Count; i++) { // If this MMDevice's ID is the same as the ID of the MMDevice received by the InputObject parameter if (DeviceCollection[i].ID == inputObject.ID) { // Create a new audio PolicyConfigClient PolicyConfigClient client = new PolicyConfigClient(); // Using PolicyConfigClient, set the given device as the default playback communication device client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eCommunications); // Using PolicyConfigClient, set the given device as the default playback device client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eMultimedia); // Output the result of the creation of a new AudioDevice while assining it the an index, and the MMDevice itself, and a default value of true WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true)); // Stop checking for other parameters return; } } // Throw an exception about the received device not being found throw new System.ArgumentException("No such enabled AudioDevice found"); } // If the ID parameter received a value if (!string.IsNullOrEmpty(id)) { // For every MMDevice in DeviceCollection for (int i = 0; i < DeviceCollection.Count; i++) { // If this MMDevice's ID is the same as the string received by the ID parameter if (string.Compare(DeviceCollection[i].ID, id, System.StringComparison.CurrentCultureIgnoreCase) == 0) { // Create a new audio PolicyConfigClient PolicyConfigClient client = new PolicyConfigClient(); // Using PolicyConfigClient, set the given device as the default communication device (for its type) client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eCommunications); // Using PolicyConfigClient, set the given device as the default device (for its type) client.SetDefaultEndpoint(DeviceCollection[i].ID, ERole.eMultimedia); // Output the result of the creation of a new AudioDevice while assining it the index, and the MMDevice itself, and a default value of true WriteObject(new AudioDevice(i + 1, DeviceCollection[i], true)); // Stop checking for other parameters return; } } // Throw an exception about the received ID not being found throw new System.ArgumentException("No enabled AudioDevice found with that ID"); } // If the Index parameter received a value if (index != null) { // If the Index is valid if (index.Value >= 1 && index.Value <= DeviceCollection.Count) { // Create a new audio PolicyConfigClient PolicyConfigClient client = new PolicyConfigClient(); // Using PolicyConfigClient, set the given device as the default communication device (for its type) client.SetDefaultEndpoint(DeviceCollection[index.Value - 1].ID, ERole.eCommunications); // Using PolicyConfigClient, set the given device as the default device (for its type) client.SetDefaultEndpoint(DeviceCollection[index.Value - 1].ID, ERole.eMultimedia); // Output the result of the creation of a new AudioDevice while assining it the index, and the MMDevice itself, and a default value of true WriteObject(new AudioDevice(index.Value, DeviceCollection[index.Value - 1], true)); // Stop checking for other parameters return; } else { // Throw an exception about the received Index not being found throw new System.ArgumentException("No enabled AudioDevice found with that Index"); } } // If the PlaybackMute parameter received a value if (playbackmute != null) { // Set the mute state of the default playback device to that of the boolean value received by the Cmdlet DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioEndpointVolume.Mute = (bool)playbackmute; } // If the PlaybackMuteToggle paramter was called if (playbackmutetoggle) { // Toggle the mute state of the default playback device DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioEndpointVolume.Mute = !DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioEndpointVolume.Mute; } // If the PlaybackVolume parameter received a value if (playbackvolume != null) { // Set the volume level of the default playback device to that of the float value received by the PlaybackVolume parameter DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia).AudioEndpointVolume.MasterVolumeLevelScalar = (float)playbackvolume / 100.0f; } // If the RecordingMute parameter received a value if (recordingmute != null) { // Set the mute state of the default recording device to that of the boolean value received by the Cmdlet DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).AudioEndpointVolume.Mute = (bool)recordingmute; } // If the RecordingMuteToggle paramter was called if (recordingmutetoggle) { // Toggle the mute state of the default recording device DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).AudioEndpointVolume.Mute = !DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).AudioEndpointVolume.Mute; } // If the RecordingVolume parameter received a value if (recordingvolume != null) { // Set the volume level of the default recording device to that of the float value received by the RecordingVolume parameter DevEnum.GetDefaultAudioEndpoint(EDataFlow.eCapture, ERole.eMultimedia).AudioEndpointVolume.MasterVolumeLevelScalar = (float)recordingvolume / 100.0f; } }