I'm trying to find the first audio playing tab in Chrome (or other browsers).
I've already managed to get the titles of all opened chrome tabs (mainly using the information from Getting the current tab's URL from Google Chrome using C# and Get chrome browser title using c#) .
But just looking for the a tab including the word "Youtube" seems a bit weird (since it would also find the linked questions or other "non music stuff" as long as it finds the word Youtube).
Is there a way to find the first audio playing tab?
Edit
Fixed the initial problem:
Process[] chromeProcesses = Process.GetProcessesByName("chrome");
if(chromeProcesses.Length > 0)
{
foreach(Process proc in chromeProcesses)
{
if (proc.MainWindowHandle == IntPtr.Zero)
{
continue;
}
AutomationElement chrome = AutomationElement.FromHandle(proc.MainWindowHandle);
var tabs = chrome.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem));
if (tabs != null)
{
foreach(AutomationElement tab in tabs)
{
string tabTitle = tab.GetCurrentPropertyValue(AutomationElement.NameProperty).ToString();
//...
I'm currently checking for "- YouTube" (at the end of the string) in the tabtitle. But there's a way to get even more precise; by getting the next element to the tabs text.
With the inspect.exe
tool I've found out that a "currently playing" tab has an empty button (actually the speakers icon) next to the text - otherwise it's a close button. Hence by checking for the next element it should be possible to find out if the tab is also playing music.
But I'm currently struggling to find the next element. How to do that with the AutomationElement
class?