/// <summary> /// Initializes a new instance of the <see cref="AudioDevice"/> class. /// </summary> /// <param name="id">The identifier.</param> /// <param name="friendlyName">The friendly name.</param> /// <param name="flow">The flow of the auudio.</param> /// <param name="enabled">Whether the audio device is enabled.</param> /// <param name="assignedDefault">The enumeration that represents whether this device is a default audio device.</param> internal AudioDevice(string id, string friendlyName, AudioFlowType flow, bool enabled = true, DefaultAudioDeviceType assignedDefault = DefaultAudioDeviceType.None) { this.AssignedDefault = assignedDefault; this.Enabled = enabled; this.Flow = flow; this.FriendlyName = friendlyName; this.Id = id; }
/// <summary> /// Gets the default audio device for the specified process. /// </summary> /// <param name="processId">The process identifier.</param> /// <param name="flow">The audio flow; either input or output.</param> /// <returns>The audio device; otherwise <c>null</c>.</returns> public string GetDefaultAudioDevice(uint processId, AudioFlowType flow) { try { var dataFlow = this.GetDataFlow(flow); this.AudioPolicyConfig.GetPersistedDefaultAudioEndpoint(processId, dataFlow, Role.Multimedia | Role.Console, out string deviceId); return(this.ParseDeviceId(deviceId)); } catch { return(null); } }
/// <summary> /// Sets the default audio device for the specified process. /// </summary> /// <param name="processName">The process name.</param> /// <param name="audioFlow">The audio flow; either input or output.</param> /// <param name="deviceId">The device identifier.</param> public void SetDefaultAudioDevice(string processName, AudioFlowType audioFlow, string deviceId) { var flow = this.GetDataFlow(audioFlow); if (this.TryGetAudioSessionProcessId(processName, flow, out var audioSessionProcessId)) { // Default to zero pointer; this will only change if an audio device has been specified. var hstring = IntPtr.Zero; if (!AudioDevices.Current.IsDefaultPlaybackDevice(deviceId)) { using (var device = AudioDevices.Current.GetDevice(deviceId)) { var persistDeviceId = this.GenerateDeviceId(device.ID); Combase.WindowsCreateString(persistDeviceId, (uint)persistDeviceId.Length, out hstring); } } // Set the audio device for the process. this.AudioPolicyConfig.SetPersistedDefaultAudioEndpoint(audioSessionProcessId, flow, Role.Multimedia, hstring); this.AudioPolicyConfig.SetPersistedDefaultAudioEndpoint(audioSessionProcessId, flow, Role.Console, hstring); } }
/// <summary> /// Sets the default audio device for the specified process. /// </summary> /// <param name="processId">The process identifier.</param> /// <param name="flow">The audio flow; either input or output.</param> /// <param name="deviceId">The device identifier.</param> public void SetDefaultAudioDevice(uint processId, AudioFlowType flow, string deviceId) { var processName = Process.GetProcessById((int)processId).ProcessName; this.SetDefaultAudioDevice(processName, flow, deviceId); }
/// <summary> /// Gets the data flow from the specified <see cref="AudioFlowType"/>. /// </summary> /// <param name="flow">The flow.</param> /// <returns>The interop data flow.</returns> private DataFlow GetDataFlow(AudioFlowType flow) => flow == AudioFlowType.Playback ? DataFlow.Render : DataFlow.Capture;