/// <summary> /// Load all audio sessions /// </summary> public void LoadAudioSessions() { // Get all current sessions Hashtable MissingInstanceIdentifier = new Hashtable(); foreach (AudioSession Current in AudioSessions) { MissingInstanceIdentifier.Add(Current.SessionInstanceIdentifier, true); } try { oSessionDefaultAudioEndPointDevice.RefreshSessions(); } catch (Exception e) { Debug.WriteLine("Cannot refresh Audio Sessions." + e.Message); } coSessionsDefaultAudioEndPointDevice = oSessionDefaultAudioEndPointDevice.Sessions; for (int iSession = 0; iSession < coSessionsDefaultAudioEndPointDevice.Count; iSession++) { if (MissingInstanceIdentifier.ContainsKey(coSessionsDefaultAudioEndPointDevice[iSession].GetSessionInstanceIdentifier)) { MissingInstanceIdentifier.Remove(coSessionsDefaultAudioEndPointDevice[iSession].GetSessionInstanceIdentifier); } // System.Diagnostics.Debug.WriteLine("Session:" + coSessionsDefaultAudioEndPointDevice[iSession].GetSessionInstanceIdentifier); if (Process.GetCurrentProcess().Id != coSessionsDefaultAudioEndPointDevice[iSession].GetProcessID) { AudioSession FindSessions = AudioSessions.Find(s => s.SessionInstanceIdentifier == coSessionsDefaultAudioEndPointDevice[iSession].GetSessionInstanceIdentifier); if (FindSessions == null) { AudioSession CurrentSession = new AudioSession(coSessionsDefaultAudioEndPointDevice[iSession]); AudioSessions.Add(CurrentSession); CurrentSession.SessionVolumeChanged += OnAllVolumeChanged; CurrentSession.StateChanged += OnAllStateChanged; coSessionsDefaultAudioEndPointDevice[iSession].RegisterEventClient(CurrentSession.eventClient); Debug.WriteLine("Add Audio session:" + coSessionsDefaultAudioEndPointDevice[iSession].GetSessionInstanceIdentifier); } } Process[] aProcesses = Process.GetProcessesByName(RecordingSourceProcessName); int ProcessCount = aProcesses.Count(s => s.Id == (coSessionsDefaultAudioEndPointDevice[iSession].GetProcessID)); if (ProcessCount >= 1) { // Spotify detected RecordingSourceAudioSession = coSessionsDefaultAudioEndPointDevice[iSession]; SessionInstanceIdentifierRecordingSource = coSessionsDefaultAudioEndPointDevice[iSession].GetSessionInstanceIdentifier; } } // Remove Left Sessions foreach (string OldSession in MissingInstanceIdentifier.Keys) { AudioSessions.Remove(AudioSessions.Find(s => s.SessionInstanceIdentifier == OldSession)); } AudioSessionsReload(new EventArgs()); }
public int GetRecordingSourceVolume() { if (!(string.IsNullOrEmpty(SessionInstanceIdentifierRecordingSource))) { AudioSession ASession = AudioSessions.Find(s => s.SessionInstanceIdentifier == SessionInstanceIdentifierRecordingSource); if (ASession != null) { try { return(ASession.Volume); } catch { return(-1); } } return(-1); } return(-1); }