Is there any equivalent function of requestAudioFocus in Android API level 7? - android

My Android application needs to support Android platform 2.1.x (which is Android API level 7). I need to use requestAudioFocus() and abandonAudioFocus() methods to pause other applications (e.g. Music) to play when my application starts playing media and resume them after my application stops.
However, these two functions are only available at API Level 8 and above. What are the equivalent functions at API level 7? Or how to do this before API level 8?

Thanks to Christopher (http://stackoverflow.com/questions/1993471/android-can-i-mute-currently-playing-audio-applications), I am citating:
Audio handling on Android is going to be pretty horrible for a while. The APIs are pretty weird, poorly documented, and keep changing/deprecating/breaking between versions. Even the AudioManager code has FIXMEs in it.
Anyway, there are several stream types in Android (music, notifications, phone calls, etc.) and applications are meant to choose the appropriate one for playback. I imagine the majority of Android apps should use the music/media type (STREAM_TYPE_MUSIC). You set this on your MediaPlayer using the setAudioStreamType method.
The SDK does allow you to set a single stream type as solo — causing all other streams to be muted — but I don't believe you can identify the audio being played back by particular applications and somehow pause/unpause it. Music applications in general will use the PhoneStateListener to pause themselves when a call comes in.
So in your case, you could "borrow" the phone call stream for your MediaPlayer and use the method call AudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true) when playback begins, then un-solo the stream with false when playback or your Activity is done.
I can tell you that this works, but I can't remember offhand whether you need to also set the audio mode to MODE_IN_CALL when using the voice call stream (like this: AudioManager.setMode(AudioManager.MODE_IN_CALL)). If you find that is required, then you need to make sure you return the mode to MODE_NORMAL once playback completes, otherwise whenever you press the volume hard keys, it'll say "In-call volume"! However, if and when you do want to change back to MODE_NORMAL, you must check that a genuine phone call isn't happening at that time...
Maybe you could use another stream type rather than the voice call one, but I'm just speaking from experience working on an app that could use either the speakerphone or the earpiece for audio playback, which requires the use of the voice call stream.
Like I said, audio handling isn't particularly fun... ;)

Related

Sharing audio(Mic) input to multiple apps in Android

As per official documentation
Android 10 (API level 29) and higher imposes a priority scheme that can switch the input audio stream between apps while they are running. In most cases, if a new app acquires the audio input, the previously capturing app continues to run but receives silence. In some cases, the system can continue to deliver audio to both apps. The various sharing scenarios are explained below.
Other than some special cases, audio is not shared between apps.
But I have seen many apps sharing the audio input without being in the above special cases.
For eg. Zoom, when I'm on a call in zoom and start an audio recorder then both the apps are getting audio though zoom audio decreases in intensity.
Similarly, Omlet arcade is able to record mic audio even when mic access is given to other apps.
How is it possible? And as per the documentation, this shouldn't be allowed.
Update:
Was able to achieve it with the usage of Oboe. But it is not consistent on all devices. This also causes a sync issue in my live streaming app. Audio is audible with a delay
This is not possible in Android 5+ . You need a rooted phone to perform this action. In Omlet Arcade Whenever you play a Game and switch ON in-game mic, Omlet Arcade will stop receiving any audio input. However, Omlet Arcade will still function but you have to restart it in order to get voice input back.
Though, in a recent MIUI bug, People were able to listen to calls on Zoom and in-game mic apps. In your case, it might be not official Android and Edited Android like MIUI and OxygenOS

Why it is not possible to play an audio file on a voice call in android

This question might seem to be a repetition of the questions such as following:
How to play an audio file on a voice call in android
Background Audio for a Call in Progress - Possible?
The answers of these questions suggests that it is not possible to play a pre-recorded audio on a voice call in android. I want to know why it is not possible? What is the limitation (hardware/software)? Is it really a limitation or done purposely? Can we alter the source code of android to make it possible?
I think this is a limitation, imposed for security reasons and restricted at the OS level.
Let's analyze the security threat, first of all. If you were able to play custom audio files to the callee, a whole world of cons opens up: you could trick customer supports, you could pretend to be someone else, you could give unauthorized purchase confirmations, and so on. For this reason, neither Android nor iOS allows this functionality.
On Android, you won't be able to do so in a programmatic way, simply because the current APIs won't allow you to do so. It is stated in the official documentation as well, as pointed out here. If you dig into the source code, you can probably enable this feature by accessing the microphone output during a phone call, but that would require running your custom version of Android. A good starting point would be the AudioTrack source, available here.
EDIT: a good example of an audio mod involves enabling the Nexus 5 earpiece as a second loudspeaker (requires root). Can be found here.
After a thorough research, what I have come to know is that there are more than one limitations/hurdles to make it possible. These limitations/hurdles are at three different levels.
First limitation is at API level, because there is no high-level API to play sound files in the conversation audio during a call as mentioned in Android official documentation.
Second limitation is at Radio Interface Layer (RIL). RIL passes on complete control of the call to Radio Daemon (rild) of the Linux library which then further passes the control to the vendor RIL. That means we cannot manipulate voice call in android source code.
Even if we are able to remove these two limitations, we may still not be able to play audio file to an ongoing voice call. Because there is a third limitation. Every vendor has their own library of RIL that communicates with Radio Daemon (rild). This requires that vendor RIL to be open source which is not actually. Hardware vendors do not usually make their device drivers code available.
Detail discussion on this topic is present at this link.
This is software related due to the prioritization of audio routing in Android.
Take a look into the CallManager where you can dig into the method setAudioMode(). After the audio mode was set to MODE_IN_COMMUNICATION the following code is called
audioManager.requestAudioFocusForCall(AudioManager.STREAM_VOICE_CALL,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
From this point on the telephony service has the highest priority and won't let any other audio play in parallel.
Note: You can play back the audio data only to the standard output device. Currently, that is the mobile device speaker or a Bluetooth headset. You cannot play sound files in the conversation audio during a call.
See official link
http://developer.android.com/guide/topics/media/mediaplayer.html
By implementing the AudioManager.OnAudioFocusChangeListener you can get the state of the audiomanager. so by this if any music is playing in the background you can get the AudioManager states(playing and pausing is completely in developer hands) similarly......
Some of the native music players in android device where handling this, they restrict the music when call is in TelephonyManager.EXTRA_STATE_OFFHOOK.so this scenario is also completely in developer hand (whether to handle or not) if he is not handling both will play parallel y

Multiple audio players(Java or C++), that can be modified real-time

Looking for some help with audio playback on Android. We have an OpenGL app (Java + C++) and now we want to play sounds effects. Players should allow to modify playback rate and volume while playing.
Might be OpenSL or Audiotrack.
First question? Is there any free or commercial library/wrapper that can do the thing?(might be java or native)
..,if not, I'll explain what we made so far, and problems we experience.
We created MusicPlayer class (extends AsyncTask) with AudioTrack instance. In activity's onResume() we created 5 instances of it, executing it on thread pool. In task's doInBackground() we have a running loop checks states change, load files, and write to buffer. In JNI we have singleton that stores events queue and send them to java once per 10 miliseconds. It somehow works, but is rather far away from being acceptable. We experience following problems:
When file starts to play we can hear short noise on start. Like click or something.
Even if we flush or release AudioTrack it seems the sound plays in queue (especially when need to change buffers quick)
We can't create a loop in MODE_STREAM
When we modify AsyncTask's local variable CHANGE_RATE and RATE it should call audioTrack.setPlaybackRate(RATE). It does, but nothing happens.
I used to write in Obj-C for iOS and there are plenty "ready-to-use" solutions(e.g. cocoacontrols). Never thought dealing with sound on Android would be such a nightmare;/ Any help will be highly appreciated:)
Android 4.0 or higher does not support playback rate control anymore. I'm not sure what was the reason for disabling it. I had to adopt SOLA algorithm implemented by SoundTouch library to add playback rate control to my app.
If you want to use it, you need to write a player using OpenSL. There are a lot of working examples around. Take the algorithms and use it for modifying PCM stream before sending the stream to the output sink.
Update 22.01.2016: Android appears to include Sonic library into 6.0 release. Thus playback rate control should be available starting from Android M.

Set the volume control stream from a service on Android

I'm working on a music app which consists in a Service playing music while other apps are running.
In order to enjoy the music and only the music, the Service mutes all the other apps. In order to do that, it mutes STREAM_MUSIC and play music over STREAM_VOICE_CALL (found that solution to mute other apps here)
As the Service uses STREAM_VOICE_CALL to play music, what I'm trying to find is a way to make the volume buttons control this stream when a sound is playing.
What I already tried:
setVolumeControlStream: only works in Activities
Capturing volume keys pressed events: Services do not receive such events (source)
audioManager.setMode(AudioManager.MODE_IN_CALL) : does not work in my case. Plus, the SDK documentation states that this "mode should only be used by the telephony application when it places a phone call".
At this point, I'm out of options and any help would be appreciated. Thanks!
I would personally not suggest you to STREAM_VOICE_CALL as it is meant to be recommended to use it for Voice calls as compared to media playback. Using this may prevent you from using features exposed by Android for media playback.
Having said this & taking into consideration your use-case, I think you need to look for changing 'call volume' level as compared to 'media volume' level.
Have a look at the MediaPlaybackService.java as used in the stock Music Player app and try to customize it as per your needs.
I havn't tried it myself. but looks like it should work. Kindly try it out.
m_audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
m_audioManager.setMode(AudioManager.MODE_IN_CALL);

Android: Manipulating voice in phone calls

I'm currently searching for options on how to manipulate audio on android. The goal is to process audio from the microphone in real time during a phone call. The best solution would be to do this on a native call. But rebuilding a telephone app (no VOIP) would be fine too. Are there any ways to achieve this with Android APIs (also undocumented)?
If not, which steps would be necessary to get things running?
On iOS there are some apps which manipulate voice but create a VOIP connection. I heard that on Android you can "clone" the telephone app and eventually feed it with your own audio stream? Aren't there apps which add noises during a call? What kind of APIs are involved?
The best solution would be to do this on a native call.
This is not possible. You have no access to the in-call audio stream, except perhaps in speakerphone mode.
But rebuilding a telephone app (no VOIP) would be fine too.
The last official word from Google (2010), the entire OS has no access to the in-call audio stream, as it is all handled at a lower level. Even if newer versions of Android do have access to the in-call audio stream, "rebuilding a telephone app" is only possible if you are creating custom firmware.
As a drawback i could imagine to record the Downlink Stream using the MediaRecorder API and write it back to hardware using AudioTracks write() method. By this i could manipulate incoming voice. But still I think this wont work during phone calls. And I do not see a way to choose different hardware destinations.

Categories

Resources