How do I get the process that is playing audio? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
crocodile
Posts: 98
Joined: 28 Dec 2020, 13:41

How do I get the process that is playing audio?

Post by crocodile » 05 Feb 2023, 21:13

How do I get the process that is playing audio?

I have multiple music players, but I struggle with how to control them. I need to know which process is currently playing so that I can call the appropriate control script based on the process name.

User avatar
lmstearn
Posts: 694
Joined: 11 Aug 2016, 02:32
Contact:

Re: How do I get the process that is playing audio?

Post by lmstearn » 05 Feb 2023, 23:46

Take a look at this thread, there's also VIsta Audio which uses IControlChangeNotify.
If only sound devices had a "busy" flag incremented by each process playing it, much easier - there may be a few ideas in this SO answer re audio device property and state changes.
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH

crocodile
Posts: 98
Joined: 28 Dec 2020, 13:41

Re: How do I get the process that is playing audio?

Post by crocodile » 06 Feb 2023, 02:16

Thank you. I managed to get all the processes that loaded the sound. But still need to figure out if it is playing or not.

Code: Select all

	DetectHiddenWindows True


	SoundPIDs() {
		PIDs := []

		IMMDeviceEnumerator := ComObjValue(ComObject("{BCDE0395-E52F-467C-8E3D-C4579291692E}", "{A95664D2-9614-4F35-A746-DE8DB63617E6}"))

		DllCall(NumGet(NumGet(IMMDeviceEnumerator, "uptr") + 4 * A_PtrSize, "uptr"), "UPtr", IMMDeviceEnumerator, "UInt", 0, "UInt", 1, "UPtrP", &IMMDevice := 0, "UInt")

		GUID := buffer(16)
		DllCall("Ole32.dll\CLSIDFromString", "Str", "{77AA99A0-1BD6-484F-8BC7-2C654C9A9B6F}", "UPtr", GUID)
		DllCall(NumGet(NumGet(IMMDevice, "uptr") + 3 * A_PtrSize, "uptr"), "UPtr", IMMDevice, "UPtr", GUID, "UInt", 23, "UPtr", 0, "UPtrP", &IAudioSessionManager2 := 0, "UInt")

		ObjRelease(IMMDevice)

		DllCall(NumGet(NumGet(IAudioSessionManager2, "uptr") + 5 * A_PtrSize, "uptr"), "UPtr", IAudioSessionManager2, "UPtrP", &IAudioSessionEnumerator := 0, "UInt")

		ObjRelease(IAudioSessionManager2)

		DllCall(NumGet(NumGet(IAudioSessionEnumerator, "uptr") + 3 * A_PtrSize, "uptr"), "UPtr", IAudioSessionEnumerator, "UIntP", &SessionCount := 0, "UInt")
		Loop SessionCount
		{
			DllCall(NumGet(NumGet(IAudioSessionEnumerator, "uptr") + 4 * A_PtrSize, "uptr"), "UPtr", IAudioSessionEnumerator, "Int", A_Index - 1, "UPtrP", &IAudioSessionControl := 0, "UInt")
			IAudioSessionControl2 := ComObjValue(ComObjQuery(IAudioSessionControl, "{BFB7FF88-7239-4FC9-8FA2-07C950BE9C6D}"))
			ObjRelease(IAudioSessionControl)

			DllCall(NumGet(NumGet(IAudioSessionControl2, "uptr") + 14 * A_PtrSize, "uptr"), "UPtr", IAudioSessionControl2, "UIntP", &PID := 0, "UInt")

			ISimpleAudioVolume := ComObjValue(ComObjQuery(IAudioSessionControl2, "{87CE5498-68D6-44E5-9215-6DA47EF883D8}"))

			DllCall(NumGet(NumGet(ISimpleAudioVolume, "uptr") + 4 * A_PtrSize, "uptr"), "UPtr", ISimpleAudioVolume, "FloatP", &Volume := 0, "UInt")
			if PID
			PIDs.push([PID, WinGetProcessPath("ahk_pid" PID), Volume])
			ObjRelease(IAudioSessionControl2)
		}
		ObjRelease(IAudioSessionEnumerator)
		ObjRelease(IMMDeviceEnumerator)

		return PIDs
	}

User avatar
lmstearn
Posts: 694
Joined: 11 Aug 2016, 02:32
Contact:

Re: How do I get the process that is playing audio?  Topic is solved

Post by lmstearn » 06 Feb 2023, 04:08

There's an answer here how to control sound out of applications with external program.
One rather inefficient way is to set up a sound recorder, and change the volume of the application (a little say 5-10%) using ideas from this or this.
If there is no difference in the recorded output, the app is not playing!
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH

crocodile
Posts: 98
Joined: 28 Dec 2020, 13:41

Re: How do I get the process that is playing audio?

Post by crocodile » 09 Feb 2023, 03:39

Thanks, I didn't find a solution from AutoHotkey, but I tried to solve it from music software, for example foobar2000 can show play status in the title. But unfortunately not all software has this customizability.

Post Reply

Return to “Ask for Help (v1)”