public void UpdateAudioEndPointDevices() { DefaultEndPointDevice = AudioMMDevices.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia); AudioEndPointDevices = AudioMMDevices.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active); AudioEndPointDevice = IsAudioEndPointDeviceIndexAvailable() ? AudioEndPointDevices[AudioEndPointDeviceIndex.Value] : DefaultEndPointDevice; AudioDeviceVolume = (int)(AudioEndPointDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100); }
private MMDevice GetDefaultAudioEndpoint(DataFlow dataFlow, Role deviceRole) { if (AudioMMDevices == null) { return(null); } if (!AudioMMDevices.HasDefaultAudioEndpoint(dataFlow, deviceRole)) { return(null); } return(AudioMMDevices.GetDefaultAudioEndpointSafeException(DataFlow.Render, Role.Multimedia, !_disposed)); }
private void Dispose(bool disposing) { if (_disposed) { return; } if (disposing) { _defaultEndpointVolumeController.AudioEndpointVolume.OnVolumeNotification -= AudioEndpointVolume_OnVolumeNotification; _defaultEndpointVolumeController.Dispose(); AudioMMDevices.Dispose(); AudioMMDevices = null; } _disposed = true; }
public void OnDefaultDeviceChanged(DataFlow dataFlow, Role deviceRole, string defaultDeviceId) { if (AudioMMDevices == null) { return; } if (!AudioMMDevices.HasDefaultAudioEndpoint(dataFlow, deviceRole)) { return; } if (DefaultAudioEndPointDeviceID != null && _defaultEndpointVolumeController != null) { _defaultEndpointVolumeController.AudioEndpointVolume.OnVolumeNotification -= AudioEndpointVolume_OnVolumeNotification; } SetDefaultAudioEndpoint(dataFlow, deviceRole); }
private void Dispose(bool disposing) { if (_disposed) { return; } if (disposing) { SetSpotifyVolumeToHighAndOthersToMute(false); AudioMMDevices.UnregisterEndpointNotificationCallback(AudioMMDevicesManager); AudioMMDevices.Dispose(); AudioMMDevices = null; _audioLoopback.Dispose(); _cancellationTokenSource.Cancel(); } _disposed = true; }
private async Task SetSpotifyAudioSessionsAndProcessId(bool running) { _spotifyAudioSessionProcessId = null; while (running && _spotifyAudioSessionProcessId == null && _spotifyProcessesIds.Any()) { var sessionsAudioEndPointDevices = AudioMMDevices .EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active) .Select(x => x.AudioSessionManager.Sessions) .ToList(); lock (sessionsAudioEndPointDevices) { foreach (var sessionAudioEndPointDevice in sessionsAudioEndPointDevices) { for (var i = 0; i < sessionAudioEndPointDevice.Count; i++) { var currentAudioSessionControl = sessionAudioEndPointDevice[i]; var currentProcessId = (int)currentAudioSessionControl.GetProcessID; if (!IsSpotifyAudioSessionControl(currentProcessId)) { continue; } if (!SpotifyAudioSessionControls.Contains(currentAudioSessionControl)) { SpotifyAudioSessionControls.Add(currentAudioSessionControl); } _spotifyAudioSessionProcessId = currentProcessId; break; } if (_spotifyAudioSessionProcessId.HasValue) { break; } } } await Task.Delay(300); _spotifyProcessesIds = SpotifyProcess.GetSpotifyProcesses(_processManager).Select(x => x.Id).ToList(); } }
public void OnDeviceStateChanged(string deviceId, DeviceState newState) { switch (newState) { case DeviceState.Active: var device = AudioMMDevices.GetDevice(deviceId); if (device.DataFlow != DataFlow.Render) { return; } if (!AudioEndPointDeviceNames.IncludesKey(deviceId)) { AudioEndPointDeviceNames.Add(deviceId, device.GetFriendlyName()); } break; case DeviceState.Disabled: case DeviceState.NotPresent: case DeviceState.Unplugged: case DeviceState.All: default: if (AudioEndPointDeviceNames.IncludesKey(deviceId)) { AudioEndPointDeviceNames.Remove(deviceId); } break; } if (DefaultAudioEndPointDeviceID == null || !AudioEndPointDeviceNames.IncludesKey(DefaultAudioEndPointDeviceID)) { DefaultAudioEndPointDeviceID = GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia)?.ID; } if (!AudioEndPointDeviceNames.IncludesKey(AudioEndPointDeviceID)) { AudioEndPointDeviceID = DefaultAudioEndPointDeviceID; } FrmEspionSpotify.Instance.UpdateAudioDevicesDataSource(); }
public void Dispose() { Dispose(true); AudioMMDevices.Dispose(); GC.SuppressFinalize(this); }