public void SetDefaultEndPoint(string deviceId, int processId) { EnsurePolicyConfig(); IntPtr hstring = IntPtr.Zero; if (!string.IsNullOrWhiteSpace(deviceId)) { var str = GenerateDeviceId(deviceId); NativeMethods.WindowsCreateString(str, (uint)str.Length, out hstring); } int hr1, hr2; hr1 = _sharedPolicyConfig.SetPersistedDefaultAudioEndpoint((uint)processId, _flow, Role.Multimedia, hstring); hr2 = _sharedPolicyConfig.SetPersistedDefaultAudioEndpoint((uint)processId, _flow, Role.Console, hstring); if (hr1 != HResult.S_OK || hr2 != HResult.S_OK) { Exception ex1 = Marshal.GetExceptionForHR(hr1); Exception ex2 = Marshal.GetExceptionForHR(hr2); if (ex1.GetType() == typeof(ArgumentException) || ex2.GetType() == typeof(ArgumentException)) { ProcessCollection.Processes.FirstOrDefault(x => x.Id == processId).AudioDeviceTogglingNeeded = true; } else { // Print error or throw exception? Print error for now. Trace.WriteLine($"Error in {nameof(SetDefaultEndPoint)} for {processId}: MultimediaResult={ex1}, ConsoleResult={ex2}"); } } }
public static void SetDefaultEndPoint(string deviceId, int processId) { Trace.WriteLine($"AudioPolicyConfigService SetDefaultEndPoint {deviceId} {processId}"); try { EnsurePolicyConfig(); IntPtr hstring = IntPtr.Zero; if (!string.IsNullOrWhiteSpace(deviceId)) { var str = GenerateDeviceId(deviceId); Combase.WindowsCreateString(str, (uint)str.Length, out hstring); } s_sharedPolicyConfig.SetPersistedDefaultAudioEndpoint((uint)processId, EDataFlow.eRender, ERole.eMultimedia, hstring); s_sharedPolicyConfig.SetPersistedDefaultAudioEndpoint((uint)processId, EDataFlow.eRender, ERole.eConsole, hstring); } catch (Exception ex) { AppTrace.LogWarning(ex); } }
public void SetDefaultEndPoint(string deviceId, int processId) { Console.WriteLine($"AudioPolicyConfigService SetDefaultEndPoint {deviceId} {processId}"); try { EnsurePolicyConfig(); var hstring = IntPtr.Zero; if (!string.IsNullOrWhiteSpace(deviceId)) { var str = GenerateDeviceId(deviceId); Combase.WindowsCreateString(str, (uint)str.Length, out hstring); } _sharedPolicyConfig.SetPersistedDefaultAudioEndpoint((uint)processId, _flow, Role.Multimedia, hstring); _sharedPolicyConfig.SetPersistedDefaultAudioEndpoint((uint)processId, _flow, Role.Console, hstring); } catch (Exception ex) { Console.WriteLine($"{ex}"); } }