/// <summary> /// Background thread that monitors running audio session & update labeltext. /// </summary> void Monitor() { int sessionCnt = 0; while (true) { Thread.Sleep(1000); foreach (AudioSession item in AudioUtilities.GetAllSessions()) { if (item.Process == null) { item.Dispose(); continue; } //if audio playback active. if (item.State == AudioSessionState.Active) { System.Diagnostics.Debug.WriteLine(item.Process.ProcessName + "\t" + item.Process.MainWindowTitle); sessionCnt += 1; if (item.Process.ProcessName.Equals("spotify", StringComparison.InvariantCultureIgnoreCase)) { UpdateLabel_T(item.Process.MainWindowTitle); } else if (item.Process.ProcessName.Equals("foobar2000", StringComparison.InvariantCultureIgnoreCase)) { //UpdateLabel_T(item.Process.MainWindowTitle.Replace("[foobar2000]", "")); UpdateLabel_T(item.Process.MainWindowTitle.Substring(0, item.Process.MainWindowTitle.Length - 12)); } else if (item.Process.ProcessName.Equals("vlc", StringComparison.InvariantCultureIgnoreCase)) { UpdateLabel_T(item.Process.MainWindowTitle.Substring(0, item.Process.MainWindowTitle.Length - 19)); } else if (item.Process.ProcessName.Equals("nvcontainer", StringComparison.InvariantCultureIgnoreCase)) // nvidia gfe screen recording. { sessionCnt -= 1; } else { UpdateLabel_T(item.Process.MainWindowTitle); } } item.Dispose(); } if (sessionCnt <= 0) { UpdateLabel_T("No Playback"); } sessionCnt = 0; } }
public String returnName() { int sessionCnt = 0; string name = "No Playback"; foreach (AudioSession item in AudioUtilities.GetAllSessions()) { if (item.Process == null) { item.Dispose(); continue; } //if audio playback active. if (item.State == AudioSessionState.Active) { if (item.Process.ProcessName.Equals("spotify", StringComparison.InvariantCultureIgnoreCase)) { name = item.Process.MainWindowTitle; } else if (item.Process.ProcessName.Equals("foobar2000", StringComparison.InvariantCultureIgnoreCase)) { //UpdateLabel_T(item.Process.MainWindowTitle.Replace("[foobar2000]", "")); name = item.Process.MainWindowTitle.Substring(0, item.Process.MainWindowTitle.Length - 12); } else if (item.Process.ProcessName.Equals("vlc", StringComparison.InvariantCultureIgnoreCase)) { name = item.Process.MainWindowTitle.Substring(0, item.Process.MainWindowTitle.Length - 19); } else if (item.Process.ProcessName.Equals("nvcontainer", StringComparison.InvariantCultureIgnoreCase)) // nvidia gfe screen recording. { sessionCnt -= 1; } else { name = item.Process.MainWindowTitle; } } item.Dispose(); } return(name); }
/// <summary> /// Background thread that monitors running audio session & update labeltext. /// </summary> void Monitor() { int sessionCnt = 0; while (true) { Thread.Sleep(1000); foreach (AudioSession item in AudioUtilities.GetAllSessions()) { if (item.Process == null || IntPtr.Equals(item.Process.MainWindowHandle, IntPtr.Zero)) { item.Dispose(); continue; } item.Process.Refresh(); //if audio playback active. if (item.State == AudioSessionState.Active) { sessionCnt += 1; try { if (item.Process.ProcessName.Equals("spotify", StringComparison.InvariantCultureIgnoreCase)) { UpdateLabel_T(item.Process.MainWindowTitle); } else if (item.Process.ProcessName.Equals("foobar2000", StringComparison.InvariantCultureIgnoreCase)) { //UpdateLabel_T(item.Process.MainWindowTitle.Replace("[foobar2000]", "")); UpdateLabel_T(item.Process.MainWindowTitle.Substring(0, item.Process.MainWindowTitle.Length - 12)); //cheaper. } else if (item.Process.ProcessName.Equals("vlc", StringComparison.InvariantCultureIgnoreCase)) { UpdateLabel_T(item.Process.MainWindowTitle.Substring(0, item.Process.MainWindowTitle.Length - 19)); } else if (item.Process.ProcessName.Equals("chrome", StringComparison.InvariantCultureIgnoreCase)) { // won't update if video changes & different tab open on browser. if (item.Process.MainWindowTitle.Contains(" - YouTube - Google Chrome")) { UpdateLabel_T(item.Process.MainWindowTitle.Substring(0, item.Process.MainWindowTitle.Length - 26)); } } else if (item.Process.ProcessName.Equals("firefox", StringComparison.InvariantCultureIgnoreCase)) { //failed to find correct process for firefox, finding windowhandle instead. hWnd = StaticPinvoke.FindWindow("MozillaWindowClass", null); StaticPinvoke.GetWindowThreadProcessId(hWnd, out processID); currProcess = Process.GetProcessById(processID); // won't update if video changes & different tab open on browser. if (currProcess.MainWindowTitle.Contains(" - YouTube - Mozilla Firefox")) { UpdateLabel_T(currProcess.MainWindowTitle.Substring(0, currProcess.MainWindowTitle.Length - 28)); } } else if (item.Process.ProcessName.Equals("nvcontainer", StringComparison.InvariantCultureIgnoreCase)) // nvidia gfe screen recording. { sessionCnt -= 1; } else { UpdateLabel_T(item.Process.MainWindowTitle); } } catch (ArgumentOutOfRangeException) //when window title < subtracting value. { UpdateLabel_T(item.Process.MainWindowTitle); } } item.Dispose(); } if (sessionCnt <= 0) { UpdateLabel_T("No Playback"); } sessionCnt = 0; } }