How to stop music in my service when youtube app resumes video? - android

In an android application, I have written a service that plays music at the background. I have implemented OnAudioFocusChangeListener that listens to change in audio focus. The problem is I dont recieve a call back to onAudioFocusChange() in the following situation.
Play music in my app and put the app in background. (Music keeps playing at the background).
Start youtube app, and play any video. This gives a callback to onAudioFocusChange() where I pause my media player.
Now I press home button for youtube(youtube stops playing).
Go back to my app and play music again and put the app in background(music keeps playing at background).
Now start youtube and resume the video(stopped earlier), This does not give me a call back to onAudioFocusChange().
Note : Only resuming a video does not give a call back to onAudioFocusChange(), but playing a new video, gives a call back to onAudioFocusChange().

Found a solution for my own problem. This is how I solved the issue.
Every time your application loses audio focus, your have to request for audio focus and then play music. This will allow you to listen to the changes in audio focus through onAudioFocusChange().
public boolean requestFocus()
{
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAudioManager
.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}

Related

Resume music from another android app

I have an app that runs the Youtube API and plays a video in a certain view. When the video plays, the music from any background apps are paused. I've tried to resume the music with the following code which is run onBackPressed():
private void resumeMusic() {
if(wasMusicPlaying){
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "play");
ExerciseViewer.this.sendBroadcast(i);
}
}
When I go back, I can't get the music to resume. I know its possible because when using snapchat, the background music automatically resumes after a snap video finishes playing but I can't seem to get this functionality on my app.
I've also tried to use "togglepause" instead of play but that does not work either.
I've not tried this, but I think this can be achieved using Android - MediaPlayer.
When entering to your app at the begining of the code, you can check audio is playing. (this should done before calling to youtube api)
mediaPlayer.isPlaying()
if true, you know that a media was playing when someone enter into your application. Store that in your application logic. maybe call it boolean wasplaying,
boolean wasPlaying = mediaPlayer.isPlaying();
when exiting your application, check whether there's was an any audio playing. if any audio is playing, resume it.
if(wasPlaying){
mediaPlayer.start(); //resumes mediaplayer audio
}
I'm not sure about the code and media player does the exact thing, but you can try to use a logic like that.
You can have a look at https://developer.android.com/reference/android/media/MediaPlayer.html
if mediaplayer doesn't work getting the current playing audio, this would help Track info of currently playing music

Abandoning audio focus does not resume music streaming for other apps

I'm working on an app allowing users to watch videos. When they open a video to watch, I call:
AudioManager mAudioManager = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
This stops playback from any other apps streaming music at the time. I've tested with Play Music, Spotify, Soundcloud, etc and they all stop music playback at this point.
When the user is done watching a video, I call
mAudioManager.abandonAudioFocus(this);
but the app that was streaming music and previously paused does not resume streaming music. How can I get this to work?
I've tried doing AUDIOFOCUS_GAIN and AUDIOFOCUS_GAIN_TRANSIENT. Everything I've read on Stack Overflow just says I just need to release the audio manager. This seems so simple, but I can't figure it out. I want the functionality to be similar to how Instagram pauses and resumes music when you view a video.
As you are requesting the complete 'AUDIO_FOCUS', the external application has to hit the play button to request the 'AUDIO_FOCUS' back. Just because your application has gained the 'AUDIO_FOCUS' and then abandoned it, this wouldn't resume the previous music/video which was being played.
The only way to overcome this would be to rethink the requirement of the application. Therefore, you can request temporary audio focus, i.e. AudioManager.AUDIOFOCUS_GAIN_TRANSIENT (or AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_CAN_DUCK depending on whether or not you think it's ok for other audio to keep playing in the background at a lower volume).
int requestAudioFocusResult = audioManager.requestAudioFocus(TimeWhisperService.this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
if(requestAudioFocusResult == AudioManager.AUDIOFOCUS_GAIN) {
//ACCESS GRANTED
}

Android - Media session/Media buttons in a video player activity (not a service)

In a video player implemented in an activity, NOT A SERVICE, I am using a MediaSessionCompat to receive media button actions (bluetooth and hset). This video player supports background play, that is the video goeson playing when another activity comes in the foreground.
setActive(true) activates the session and receive media command after the activity
is launched. My player desativates its media session when the it looses audio focus, not after pausing. Everything is fine up to this point.
If another player (I am testing with VLC) activates request the audio focus and setActive(true) everything works as expected.
But when the user of my app retakes the audio focus by cliking the start button on screen (not with media button), it does not activate the media session and media button actions are not received.
A media session behaves differently in a service than in an activity but this behaviour seems to contradict android doc which says :
Android routes media button events in this order: 1) Android first
dispatches media button events to the foreground activity (the
activity on the screen).
Anyway, could you please help me to reactivate the media session when my app is back on the screen.

How to stop Android music player when my app is ringing

I have implemented a VoIP application and everything works fine. When a user is playing a music on his device using Android default music player or any other third party apps, and my app starts to ring or a message is received, my app's ringtone is not played and the music player is still playing and this situation keeps happening even in incall and call termination states.
How do I detect when a music player (default player for this stage) is playing a music and how to pause and resume it on my app's ringing and call termination states?
An application should request Audio Focus when it needs to utilize the audio output, either with music or notification sounds. Once it has the priority of Audio Focus, it can use the sound output freely, while listening for focus changes. If Audio Focus loses the focus, it should immediately either kill the audio or lower it to a quiet level and only resume loud playback when it receives focus again.
If an application needs to output music, Audio Focus should be requested. The method requestAudioFocus() should be called from the AudioManager.
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int result = audioManager.requestAudioFocus( this, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
{
// could not get audio focus.
}
Take a look on Handling Audio Sound from Overlapping with VOIP call

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!

Categories

Resources