1

Event/State change for change of audio output

Tasker currently has state change to detect Headset Plugged, and BT connect/disconnect.

However recent versions of Android (and some apps e.g. Pocket Casts) provide an ability to show/change audio output without connecting/disconnecting e.g. BT device is connected, can change to internal speaker, or with 2 BT devices connected can switch between them.

Therefore I'd like an event/state change to detect when audio output changes - perhaps with 2 associated variables: audio output (i.e. Speaker/BT/Plugged headset) and device name (to allow trigger only for specific audio output e.g. Car).

I currently do this using logcat entries, however this is device/phone specific (so needs amending for each new phone), and on newer versions of Android isn't ideal (so a solution using standard Android APIs would be better)..

2 replies

Hhmm, I don't know any Android APIs that report that state. Do you know any by any chance?

B

I was in a similar situation a little while ago where I wanted to more consistently detect audio output changes in Tasker. I, too, was using logcat initially, but after Android 13 came along and needlessly made it hell to read logs I started searching for a new, (hopefully) cleaner way.

My findings: Android does not provide any way to receive information about audio output changes -- at least not one that's intended for use by user apps. However, there is this intent: "android.media.STREAM_DEVICES_CHANGED_ACTION", which I have been able to receive in Tasker via the "Intent Receive" event without any issue (so far). It includes 3 extras: the stream type affected by the change (e.g. the media stream), the new audio output device(s), and the previous audio output device(s) that were active before this change occurred. You can find more info in the AudioManager source code:

https://cs.android.com/android/platform/superproject/+/main:frameworks/base/media/java/android/media/AudioManager.java

The comments do make note that this intent is "for internal platform use only", so although it may work fine as of now, I suppose it's highly possible that a future Android release could break things.