public DS4Audio(DataFlow audioFlags = DataFlow.Render) { var audioEnumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator; IMMDeviceCollection audioDevices; audioEnumerator.EnumAudioEndpoints(audioFlags, DeviceState.Active, out audioDevices); int numAudioDevices; Marshal.ThrowExceptionForHR(audioDevices.GetCount(out numAudioDevices)); for (int deviceNumber = 0; deviceNumber < numAudioDevices; ++deviceNumber) { IMMDevice audioDevice; Marshal.ThrowExceptionForHR(audioDevices.Item(deviceNumber, out audioDevice)); string deviceName = GetAudioDeviceName(ref audioDevice); if (deviceName.Contains("DUALSHOCK®4 USB Wireless Adaptor")) { object interfacePointer; Marshal.ThrowExceptionForHR(audioDevice.Activate(ref IID_IAudioEndpointVolume, ClsCtx.ALL, IntPtr.Zero, out interfacePointer)); endpointVolume = interfacePointer as IAudioEndpointVolume; } Marshal.ReleaseComObject(audioDevice); } Marshal.ReleaseComObject(audioDevices); Marshal.ReleaseComObject(audioEnumerator); }
public DS4Audio(DataFlow audioFlags = DataFlow.Render, string searchName = "Wireless Controller") { var audioEnumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator; IMMDeviceCollection audioDevices; audioEnumerator.EnumAudioEndpoints(audioFlags, DeviceState.Active, out audioDevices); int numAudioDevices; Marshal.ThrowExceptionForHR(audioDevices.GetCount(out numAudioDevices)); for (int deviceNumber = 0; deviceNumber < numAudioDevices; ++deviceNumber) { IMMDevice audioDevice; Marshal.ThrowExceptionForHR(audioDevices.Item(deviceNumber, out audioDevice)); string deviceName = GetAudioDeviceName(ref audioDevice); if (deviceName.Contains(searchName)) { object interfacePointer; Marshal.ThrowExceptionForHR(audioDevice.Activate(ref IID_IAudioEndpointVolume, ClsCtx.ALL, IntPtr.Zero, out interfacePointer)); instAudioFlags = audioFlags; endpointVolume = interfacePointer as IAudioEndpointVolume; endpointVolume.RegisterControlChangeNotify(this); } RefreshVolume(); Marshal.ReleaseComObject(audioDevice); } Marshal.ReleaseComObject(audioDevices); Marshal.ReleaseComObject(audioEnumerator); }
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(); }