Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving the application name that has audio focus change

I can't seem to find anything related to finding out what application got audio focus. I can correctly determine from my application what type of focus change it was, but not from any other application. Is there any way to determine what application received focus?

"What am I wanting to do?"

I have managed to record internal sound whether it be music or voice. If I am currently recording audio no matter the source, I want to determine what application took the focus over to determine what my application need's to do next.

Currently I am using the AudioManager.OnAudioFocusChangeListener for my application to stop recording internal sounds once the focus changes, but I want the application's name that gained the focus.

like image 447
Trevor Avatar asked Jul 24 '15 17:07

Trevor


3 Answers

Short Answer: There's no good solution... and Android probably intended it this way.

Explanation: Looking at the source code, AudioManager has no API's(even hidden APIs) for checking who has Audio Focus. AudioManager wraps calls to AudioService which holds onto the real audio state. The API that AudioService exposes through it's Stub when AudioManager binds to it also does not have an API for querying current Audio Focus. Thus, even through reflection / system level permissions you won't be able get the information you want.

If you're curious how the focus changes are kept track of, you can look at MediaFocusControl whose instance is a member variable of AudioService here.

Untested Hacky Heuristic: You might be able to get some useful information by looking at UsageStats timestamps. Then once you have apps that were used within say ~500ms of you losing AudioFocus you can cross-check them against apps with Audio Permissions. You can follow this post to get permissions for any installed app.

This is clearly a heuristic and could require some tuning. It also requires the user to grant your app permissions to get access to the usage stats. Mileage may vary.

like image 64
Trevor Carothers Avatar answered Sep 28 '22 10:09

Trevor Carothers


Looking at the MediaContorller class (new in lollipop, available in comparability library for older versions).

There are these two methods that look interesting:

https://developer.android.com/reference/android/media/session/MediaController.html#getPackageName()

https://developer.android.com/reference/android/media/session/MediaController.html#getSessionActivity()

getPackageName supposedly returns the current sessions package name: http://androidxref.com/5.1.1_r6/xref/frameworks/base/media/java/android/media/session/MediaController.java#397

getSessionActivity gives you a PendingIntent with an activity to start (if one is supplied), where you could get the package as well.

Used together with your audio listener and a broadcast receiver for phone state to detect if the phone is currently ringing you might be able to use this in order to get a more fine grained detection than you currently have. As Trevor Carothers pointed out above, there is no way to get the general app with audio focus.

like image 37
JohanShogun Avatar answered Sep 28 '22 08:09

JohanShogun


You can use dumpsys audio to find who are using audio focus. And, you can also look into the results of dumpsys media_session.

And, if you want to find who're playing music, you can choose dumpsys media.audio_flinger. For myself, I switch to this command.

like image 24
liudongmiao Avatar answered Sep 28 '22 08:09

liudongmiao