I basically have an audio application that will be playing some music. I want to be able to pause/stop/mute the music when there is an interrupt.
These interrupts include: GPS directions, Phone Call, GPS, etc. (if there are more audio interupts, please let me know)
I already implemented the phone call interrupt, stops the music when phone call received and plays after phone call ends.
How would I do the other interrupts?
EDIT:
I noticed that Android's Play Music application does this. But I am unable to find the source code of that, not sure if that would be helpful.
Make sure you correctly ask for and release Audio Focus as described here:
http://developer.android.com/training/managing-audio/audio-focus.html
With multiple apps potentially playing audio it's important to think about how they should interact. To avoid every music app playing at the same time, Android uses audio focus to moderate audio playback—only apps that hold the audio focus should play audio.
Basically this allows the framework to handle interrupts properly as you cannot specifically code for every situation.
Related
I have an audio recording service in my app which will record the sound continuously. So, it will always occupy the AudioRecord. It means no other app can use audio recorder as it is already occupied by the service. Is there any way to notify that other app is requesting for audio recorder(so that I can release it) and also when the app releases it(so that I can assign it back to the service)?
Maybe a possible way is to create a BroadcastReceiver which receives an event from the app which is requesting the control over the mic source. The onReceive() method should interact with the service and release the resource. When the other app is finishing it can revert the process to start the service again. If you can't get control over the behavior of the requesting app I think there's a slightly different problem. Anyway:
The problem is all about knowing when the resource is being requested, this can be done through AudioManager intent types.
Be sure to check Managing audio focus which talks about audio focus loss in TRANSIENT way!
As #Rekire mentioned, there is possibly no way to achieve this. Also, AudioManager provide no such broadcasts, so it is not possible for different apps. Maybe rooting the device is the only option.
This can be done with AudioManager.OnAudioFocusChangeListener callback. Just stop recording on AUDIOFOCUS_LOSS_TRANSIENT event and start again on AUDIOFOCUS_GAIN event.
This solution works well for Google Voice Search (Google Search widget, Google Chrome, etc).
But unfortunately it works poorly for other ordinary applications (for example HTC M7 Voice Recorder app is not able to start recording on first click on "Record" button, second click do the trick - it seems app should be ready to retry recording on failure several times).
I have been reading the Android documentation on "Audio Focus", and the best-practices they lay out, but one thing alludes me...
Games need music pretty much the whole time, so it makes sense to request Audio Focus OnStart, but this can lead to a bad user experience.
If my App requests Audio Focus, and something is currently playing music already (eg. Samsung Music Player), my request will forcefully stop their music. The only special case I know of is if you request Audio Focus while the user is in a Phone Conversation.
I think what the user expects to happen, is if they are not already listening to music (or podcast, or whatever), then the game music should play. However, if they are already listening to their own music, then the game should not play music (but still play sound effects).
This is how things work on Xbox, Windows Phone, PS3, etc.
Is this just how it is on Android? Has anyone come up with a nice work around?
Note: I am familiar with AudioManager.IOnAudioFocusChangeListener. I am speaking specifically about the initial request for Audio Focus.
The other special case to avoid stopping something from playing when requesting audio focus is to allow it to duck, but that applies to transient loss which isn't what you would be doing. There's no way to know if another app is playing audio unfortunately so you will either take the audio and kill the music or you can not request the audio and hope for the best.
It might be worth prompting the user on launch if they are listening to their own music and if so then your game music will remain silent.
I am writing a android app which is supposed to play back a audio file when a call is in progress coming from a specific number .. I tried many approaches.. but all went in vein
Separate Thread
Listener on Telephone service
starting a service in parallel
can any one please help me how to proceed regarding this ?
Update :
I am able to play a mp3 file on call recieve and i am able to play it load on speaker.. but how ever loud i play the calling party is not able to listen to it.... is there anyway i can push the speaker stream to call stream
From the api doc here
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.
So from this and lots of other so answers we can conclude that we can not play an audio during a call process.
But a long time ago from a personal experience with a handset I got a result where the audiotrack was playing while there was a call established and both the voices were heard at the same time. So I think this depends on handsets if it allows it then you can play.
You can try another thing experimentally. play the audio using a different route ( speakerphone or bluetooth).
Another option is to build your custom android build
Actually i am creating an application which starts recording when user start speaking and
stop recording automatically when user stop speaking.
so is there any way to find if user is speaking or not?
is there any listener for Media Recorder for this?
No, AFAIK there are no listeners or intents that would notify your app that sound level has gone above some threshold (e.g. user started talking).
You could use AudioRecord class to record the microphone audio and then analyze it to see the volume. However this would require your app to run at that time.
Right now my application lets the user start recording audio and puts an ongoing notification that can pause/restart recording on press using android.media.AudioRecord. All was fine and dandy until I realized that this blocks any other App from using an AudioRecorder (ie google voice search).
Is there a way I can set up a broadcast reciever to detect a call for an AudioRecorder from another app and pause my recording. Alternatively, is there another way to record audio that wont interfere with other Apps that use audio?
Cheers!
I have been looking into this very question for a while now. It seems that there is no clean way of achieving this as there is no broadcast that alerts when another app would like access to the mic.
The way that we have solved it (albeit not cleanly) is we poll what app is in the foreground and get its permissions; if that app has permission to use the mic, we terminate recording until there isn't an app in the foreground with the mic permission.
Although polling is a solution, I would be very interested if anyone has better!