internal MMDevice(IMMDevice realDevice) { _RealDevice = realDevice; GetPropertyInformation(); Marshal.ThrowExceptionForHR(_RealDevice.GetId(out _id)); IMMEndpoint ep = _RealDevice as IMMEndpoint; Marshal.ThrowExceptionForHR(ep.GetDataFlow(out _dataFlow)); Marshal.ThrowExceptionForHR(_RealDevice.GetState(out _state)); if (_PropertyStore.Contains(PKEY.PKEY_DeviceInterface_FriendlyName)) { _friendlyName = (string)_PropertyStore[PKEY.PKEY_DeviceInterface_FriendlyName].Value; } if (_PropertyStore.Contains(PKEY.PKEY_DeviceInterface_Icon)) { var iconPath = (string)_PropertyStore[PKEY.PKEY_DeviceInterface_Icon].Value; _icon = DeviceIconHelper.GetIconByPath(iconPath); } if (_PropertyStore.Contains(PKEY.PKEY_DeviceInterface_RealName)) { var nameValue = _PropertyStore[PKEY.PKEY_DeviceInterface_RealName].Value; if (nameValue is string s) { _realName = s; } } }
public EDataFlow GetDataFlow() { EDataFlow flow = EDataFlow.All; try { var guid = new Guid(GuidValue.External.IMMEndpoint); Marshal.QueryInterface(Marshal.GetIUnknownForObject(this.device), ref guid, out IntPtr endpointPtr); IMMEndpoint endpoint = (IMMEndpoint)Marshal.GetObjectForIUnknown(endpointPtr); endpoint.GetDataFlow(out flow); } catch {} return(flow); }
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(); }
internal MMDevice(IMMDevice realDevice) { _RealDevice = realDevice; GetPropertyInformation(); Marshal.ThrowExceptionForHR(_RealDevice.GetId(out _id)); IMMEndpoint ep = _RealDevice as IMMEndpoint; Marshal.ThrowExceptionForHR(ep.GetDataFlow(out _dataFlow)); Marshal.ThrowExceptionForHR(_RealDevice.GetState(out _state)); if (_PropertyStore.Contains(PKEY.PKEY_DeviceInterface_FriendlyName)) { _friendlyName = (string)_PropertyStore[PKEY.PKEY_DeviceInterface_FriendlyName].Value; } }