Is it possible to start playing audio from the background when a geofence is entered/exited? - android

I'm having trouble determining if iOS and/or Android has the capability of starting audio playback from the background. The general order of operations would be...
User starts app
User chooses to hear audio when they arrive/depart a location
Region monitoring is used in the background to determine location changes
Region is entered/exited: if the app is configured to play certain audio for this region, load it from a URL and start playing it. If another track is already playing, queue the new one.
If music/other audio is already playing in the background, duck it until my queue has completed
I know that I'll need to enable background audio, but I'm not able to determine if I can initiate playback from the background.

Related

Pause/Stop/Mute music at service interupts

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.

Should Games request Audio Focus on Android?

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.

how to know when audio is started playing in android

In my application I want to get start time of music player when user started and end time when it stops.I don't want to start any music player in my app. I just want to track user activity in device. So i want my application to get any notification when user started the music .
Do I get any intent for music player started and it stopped.Or do I get any intent for when user opens music files.
Is there any other method other than intent to capture the start time and and time when user starts music player.
Well you can implement the interface 'AudioManager.OnAudioFocusChangeListener' in which there is a method 'onAudioFocusChange' which lets you know if audio focus has been changed, and it can also tell if focus was gained or lost.
See this link, it explains the Audio Focus in detail. An application must gain audio focus through a request, and you can implement the 'AudioManager.OnAudioFocusChangeListener' and if there is focus gain, you can detect if the media player is running, (because some other application such as you tube may gain focus to play its audio), see this to see how to detect which application or service is currently running. You can find if audio has started to play, and if it was the media player or not.
Hope this helps. It was interesting question and I have learned some new things while searching for the answer!

Is there any Listener for Media Recorder?

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.

Is it possible to control the volume of the default media player in Android from an Android application

I have the following requirement.
I am developing an Android mobile application. A timer has been set for a specific duration for an activity.
I need to play "beep" sound three times when the timer duration is 10 seconds left to complete (i.e. become zero), two times "beep" sound when the timer duration is 5 seconds left to complete & once "beep" sound when the timer completes.
The user may be playing music using the default music player of the Android phone while using the Android mobile application. I need to implement the logic so that when the "beep" sound is being played from the mobile application, I need to first decrease the volume of the default music & then play the "beep" sound & again reset to the original volume after the "beep" sound have been played the required no of times.
I wanted to know, whether this is technically feasible or not.
Yes, you can use AudioManager to change the volume for music. The function [setStreamVolume][2] is what you're looking for. The stream type you're looking for is AudioManager.STREAM_MUSIC
[2]: http://developer.android.com/reference/android/media/AudioManager.html#setStreamVolume(int, int, int)
One way to achieve this is by using the platform's ability to allow your app to request audio focus while allowing other apps to "duck". This allows you to tell the system .. "Hey, I'm going to play something, tell other apps also playing audio that it's ok to continue playing but to lower their volume".
Here is the relevant sample code and note from the Managing Audio Focus section.
When requesting transient audio focus you have an additional option:
whether or not you want to enable "ducking." Normally, when a
well-behaved audio app loses audio focus it immediately silences its
playback. By requesting a transient audio focus that allows ducking
you tell other audio apps that it’s acceptable for them to keep
playing, provided they lower their volume until the focus returns to
them.
AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);
// Request audio focus for playback
int result = am.requestAudioFocus(afChangeListener,
// Use the music stream.
AudioManager.STREAM_NOTIFICATION,
//tell them it's ok to duck
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// Start playback.
}
I hope it helps someone.

Categories

Resources