Question

I am working on a Android media player application (Android ver 2.3 - 4.1 to be supported). I want to detect audio path transitions, like speaker to wired-headset or wired-headset to BT etc. I need to take some action in my app on these transitions. So is there any single way (preferably broadcast-receiver) which Android provides when switching audio-paths? If possible I want to avoid detecting wired-headset and BT-headset connect/disconnect separately.

I am still adapting to Android and hoping for some help here.

Thanks in advance.

Was it helpful?

Solution

On Jellybean and later you might be able to get the information you want by using the MediaRouter class.

The MediaRouter class has a hidden method named getSystemAudioRoute() that returns a RouteInfo object. You should be able to get hold of that method by using reflection on the MediaRouter class.
Once you have the RouteInfo you can call getName() on it, which should return the name of the current audio route as a CharSequence.
Keep in mind that this will just be a strong hint at where audio is being routed, as it's up to the audio HAL to decide for each stream type where they should be routed.

On Android versions earlier than Jellybean you could listen for broadcasts of the related intents (ACTION_HEADSET_PLUG, ACTION_HDMI_AUDIO_PLUG, etc).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top