public static void listDeviceProperties(IMMDevice dev, DEVICE_SUMMARY defaultDevice) { IPropertyStore propertyStore; dev.OpenPropertyStore(0 /*STGM_READ*/, out propertyStore); PROPVARIANT property; propertyStore.GetValue(ref PropertyKey.PKEY_Device_EnumeratorName, out property); System.Console.WriteLine(" EnumeratorName: " + (string)property.Value); propertyStore.GetValue(ref PropertyKey.PKEY_Device_FriendlyName, out property); System.Console.WriteLine(" FriendlyName: " + (string)property.Value); if (!defaultDevice.Equals(null) && defaultDevice.FriendlyName == (string)property.Value) { System.Console.WriteLine(" **Default: True**"); } Marshal.ReleaseComObject(propertyStore); IAudioEndpointVolume epv = null; var epvid = typeof(IAudioEndpointVolume).GUID; Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv)); bool mute; epv.GetMute(out mute); if (mute) { System.Console.WriteLine(" **GetMute: " + mute + "**"); } float vol; epv.GetMasterVolumeLevelScalar(out vol); System.Console.WriteLine(" GetMasterVolumeLevelScalar: " + vol); }
static void Main(string[] args) { Console.Error.WriteLine(); Console.Error.WriteLine("Syntax: audio.cs.exe [setMute] [dataFlow]"); // args[0] args[1] Console.Error.WriteLine(" [setMute] 0=mute; 1=unmute; *=followDefault"); Console.Error.WriteLine(" [dataFlow] 0=render/playback; 1=capture/recording; 2=All"); Console.Error.WriteLine(); bool defaultDeviceMuted; string setMute = (args.Length > 0) ? args[0] : ""; int dataFlow = 2; if (args.Length > 1) { switch (args[1]) { case "0": case "1": case "2": dataFlow = Convert.ToInt32(args[1]); break; } } System.Collections.Generic.List <DEVICE_SUMMARY> defaultDevices = new System.Collections.Generic.List <DEVICE_SUMMARY>(); if (dataFlow == 2) { DEVICE_SUMMARY defaultDevice = getDefaultDeviceSummary(0, 1); defaultDeviceMuted = defaultDevice.isMuted; defaultDevices.Add(defaultDevice); defaultDevices.Add(getDefaultDeviceSummary(1, 1)); } else { DEVICE_SUMMARY defaultDevice = getDefaultDeviceSummary(dataFlow, 1); defaultDeviceMuted = defaultDevice.isMuted; defaultDevices.Add(defaultDevice); } if (setMute == "*") { Console.Error.WriteLine("defaultDeviceMuted: " + defaultDeviceMuted + "\n"); setMute = (defaultDeviceMuted) ? "0" : "1"; } listDevices(dataFlow, setMute, defaultDevices); Console.Error.WriteLine("OK"); return; } // Main()
public static void listDevices(int dataFlow, string setMute, System.Collections.Generic.List <DEVICE_SUMMARY> defaultDevices) { var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator; uint deviceCount = 0, i = 0; System.Console.Write("EnumAudioEndpoints: ..."); IMMDeviceCollection deviceCollection; enumerator.EnumAudioEndpoints(dataFlow, 1, out deviceCollection); // EDataFlow: 0=eRender;1=eCapture;2=eAll | 1=DEVICE_STATE_ACTIVE deviceCollection.GetCount(out deviceCount); System.Console.WriteLine(" deviceCount=" + deviceCount);//return; for (i = 0; i < deviceCount; i++) { System.Console.Write("\n" + i + ":"); IMMDevice dev = null; deviceCollection.Item(i, out dev); if (!Object.ReferenceEquals(null, dev)) { IMMEndpoint ep = (IMMEndpoint)dev; // Marshal.QueryInterface shouldn't be necessary, just use cast instead [ http://stackoverflow.com/questions/5077172/how-do-i-use-marshal-queryinterface ] int dataFlow1; ep.GetDataFlow(out dataFlow1); System.Console.WriteLine(" GetDataFlow: [" + dataFlow1 + "] " + ((dataFlow1 == 0)?"Playback":"Recording")); DEVICE_SUMMARY defaultDevice = new DEVICE_SUMMARY(); foreach (var dev0 in defaultDevices) { if (dev0.dataFlow == dataFlow1) { defaultDevice = dev0; break; } } listDeviceProperties(dev, defaultDevice); if (setMute == "0" || setMute == "1") { IAudioEndpointVolume epv = null; var epvid = typeof(IAudioEndpointVolume).GUID; Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv)); epv.SetMute((setMute == "0"), System.Guid.Empty); System.Console.WriteLine(" SetMute: " + (setMute == "0")); } } Marshal.ReleaseComObject(dev); } Marshal.ReleaseComObject(deviceCollection); System.Console.WriteLine(); }
public static DEVICE_SUMMARY getDefaultDeviceSummary(int dataFlow, int role) { DEVICE_SUMMARY dev1 = new DEVICE_SUMMARY(); dev1.dataFlow = dataFlow; dev1.role = role; var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator; IMMDevice dev = null; Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(dataFlow, role, out dev)); IPropertyStore propertyStore; dev.OpenPropertyStore(0 /*STGM_READ*/, out propertyStore); PROPVARIANT property; propertyStore.GetValue(ref PropertyKey.PKEY_Device_FriendlyName, out property); dev1.FriendlyName = (string)property.Value; Marshal.ReleaseComObject(propertyStore); IAudioEndpointVolume epv = null; var epvid = typeof(IAudioEndpointVolume).GUID; Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv)); epv.GetMute(out dev1.isMuted); Marshal.ReleaseComObject(dev); return(dev1); }