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.
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.
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.
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.
Related
my main goal is to get DTMF tones in my Android app and perform related actions. I've seen that TelephonyManager can send DTMF but there is no method to receive them. I came across many pages (most of them quite old) which suggest the use of FFT libraries through buffering the VOICE_DOWNLINK audio stream with AudioRecorder (setting proper permissions in the manifest). I tried both with AudioRecorder and MediaRecorder (which is mainly intended for creating file) as well as "Visulizer" class which contains a proper getFft methos out of the box but none of them can access MediaRecorder.AudioSource.VOICE_DOWNLINK & co even if permissions are correctly managed and granted.
uses-permission android:name="android.permission.RECORD_AUDIO"
None of the Visualizer/MediaRecorder/AudioRecorder get initialized and I'm getting frustrated by the fact that I'm chasing after record permissions in order to listen for DTMF tones. Is there any other way I'm not considering?
After many research it appears impossible since
There is no DTMF listen method (Google... why??)
There are limitation accessing other's phone audio to FFT.
Especially related to this last point I've seen that basically all app based on a common used Google/GitHub code (https://github.com/pjasiun/dtmf-decoder) crashes from a certain SDK during calls. They all work fine with phone mic which is useless for phonecalls
Leaving this post for other people. For my purposes I resolved with an Arduino DTMF recognizer.
I'm currently using the READ_PHONE_STATE permission in Android to pause my media player when there's a call, and to resume playback when the call is over. The permission seems to scare a lot of people, so I was wondering if there was an alternative to catching the starting and stopping of a phone call without it. Any help is appreciated. Thanks!
If you want to adjust your audio output in response to something else wanting to perform audio output (e.g., an incoming phone call), look into Android's audio focus support.
READ_PHONE_STATE, as noted, is a bit of a scary permission. Moreover, it only deals with phone calls, and not other things that might need the same capability (e.g., VOIP calls, as AFAIK those don't tie into READ_PHONE_STATE-enabled stuff).
Unfortunately, audio focus is not a substitute for READ_PHONE_STATE. My app has to use both. The standard phone app on my Galaxy S3 I9300/ Android 4.3 doesn't seem to request the audio focus at all. The TelephonyManager class gives you a way to detect the end of the phone call (by sending the CALL_STATE_IDLE state update). The AudioManager doesn't seem to do anything similar, so even if audio focus could be used, it would not be as useful. And, from what I see in the documentation, there doesn't seem to be a narrower permission than READ_PHONE_STATE that would allow the app to read the phone state but not the call information. If I'm wrong about any of this, please correct me.
Unfortunately, audio focus is not a substitute for READ_PHONE_STATE. My app has to use both. The standard phone app on my Galaxy S3 I9300/ Android 4.3 doesn't seem to request the audio focus at all. The TelephonyManager class gives you a way to detect the end of the phone call (by sending the CALL_STATE_IDLE state update). The AudioManager doesn't seem to do anything similar, so even if audio focus could be used, it would not be as useful. And, from what I see in the documentation, there doesn't seem to be a narrower permission than READ_PHONE_STATE that would allow the app to read the phone state but not the call information. If I'm wrong about any of this, please correct me.
I have an audio app which will drive an external audio amplifier. In the code I set the maximum audio level:
int maxAudioVolume = audioManager.getStreamMaxVolume(AudioManager.STREA M_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_M USIC,maxAudioVolume,0);
If I plug in the amplifier, then always the following alert is issued:
Raise volume above recommended level?
This is becoming irritating. Does anybody know how the alert can be avoided? Actually, this issue has been addressed in other forums, and sometimes it was solved, but always by using some app. Thus it should be possible, but the question is: how?
Wouter Boeke
See this:
Disable sound safe level notification in android & xda-developers; Unsafe Volume - disable safe media volume popup/check
Basically you need to root your phone, and set config_safe_media_volume_enabled at boot time to false. Which happens I think if the user clicks ok, you can't get rid of it without root.
What is the difference between calling AudioManager.setRingerMode to calling AudioManager.setStreamMute(AudioManager.STREAM_VOICE_CALL, ...)
What does the documentation mean by "Ringer mode"? I'm pretty sure it is the phone ringer mode. Then how does it differ from calling setStreamMute with STREAM_VOICE_CALL?
If it is not the phone ringer mode, then what is it?
Thanks in advance.
I have never used the audio stream on the android platform, however, based on reading the documentation, I think setRingerMode will affect how the phone reacts to incoming calls. For example, AudioManager.setRingerMode(RINGER_MODE_SILENT) will disable vibrations and sound when an incoming call is received.
However, AudioManager.setStreamMute seems to control more than just the audio stream for phone rings.
From the documentation at http://developer.android.com/reference/android/media/AudioManager.html#STREAM_VOICE_CALL
I think that AudioManager.setRingerMode(RINGER_MODE_SILENT) will act the same way as AudioManager.setStreamMute(STREAM_RING, true).
I think the best way to see what the difference is (nd to see if what I am saying is true) would be to write a small program that tests the two features.
I'd like to create an app that allow you to filter incoming call to various answering message with :
"this number is not available" for black listed phone numbers
A formal message for strangers
A informative message about what your doing for friends
I don't know how you can get automatically a call, play a recorded message then wait for the answer and record it. Or maybe there is just a way to interact with the actual answering system so I just have to plugin.
Any clue strongly appreciated. A human sacrifice for any code snippet :-)
Access to the internal telephony is not possible or planned for future releases of Android:
http://groups.google.com/group/android-developers/browse_thread/thread/e8904c82a2c4a333
This would present a security risk as app developers could intercept and hijack sensitive calls (eg. telephone banking)
This is not possible on the tmobile G1 at this time. There is no way for an android SDK application to access the call input or output on this hardware/firmware combination.
http://groups.google.com/group/android-developers/browse_thread/thread/d04c307973345fef/a628e578900b3dce?lnk=gst&q=dave+sparks+play+audio#a628e578900b3dce
and
http://groups.google.com/group/android-developers/browse_thread/thread/185e33a3f420d1ac/e14e1dc84bb6dd24?lnk=gst&q=play+sound+call#e14e1dc84bb6dd24
I'm not sure this answers the question, but it is somewhat related I think.
You can install Ultimate Voice Recorder which can record your conversations (very useful when calling customer 'service'). Since it can record it, it must have some way to access the conversation.
Also, the capabilities you have to give the app are quite scary (translated from dutch: full internet access, intercept outgoing calls, change preferences, call phone numbers directly, record audio/take pictures, update contacts, auto startup). It seems to me there must be something in there that can help you?
However, I don't think it can inject audio into the stream. The symbian version had an option to insert beeps into the conversation, but I don't think the android version has it.
http://www.fingertip-access.com/
I have found out att for this use of your Phone Android or ISO, so far they ar decades behinde symbian and an inferior alternativ sadly, if you don't install a custom kernel/jailbreak it's not possible to record incoming calls and screen them. "Ultimate Voice Record ned you to use the phone in speaker mode."
it is possible to record voice calls with automatic answer. An update on this issue would be very helpful...
It is possible to have a resource that answers the calls. Enter a message and record the call. And together don't activate the microphone...
In short, an answering machine...