Abandoning audio focus does not resume music streaming for other apps - android

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
}

Related

How to check if external media player is playing in android?

I want to stop my media player if external media player is playing.
I've implemented following code but it is called even when my app media player is playing.
if (audioManager.isMusicActive()) {
return;
}
How to distinguish between in-app media player and external media player?
Any help would be appreciated.
Two or more Android apps can play audio to the same output stream
simultaneously. The system mixes everything together. While this is
technically impressive, it can be very aggravating to a user. To avoid
every music app playing at the same time, Android introduces the idea
of audio focus. Only one app can hold audio focus at a time.
Further,
A well-behaved audio app should manage audio focus according to these
general guidelines:
Call requestAudioFocus() immediately before starting to play and
verify that the call returns AUDIOFOCUS_REQUEST_GRANTED. If you design
your app as we describe in this guide, the call to requestAudioFocus()
should be made in the onPlay() callback of your media session. When
another app gains audio focus, stop or pause playing, or duck the
volume down. When playback stops, abandon audio focus. Audio focus is
handled differently depending on the the version of Android that is
running:
You can check the more detail from developer link.

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

How to detect if video is playing in the foreground?

I am trying to detect whether audio I playing or not or if video (any video played using any video player application) is playing in the foreground or not, based on which I want to perform some action if it is playing.
I tried to check on the music stream, whether it is active or not (since audio music stream will be active, when video is active), using the following code :
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
if(audioManager.isMusicActive())
{
//do something
}
else
{
//do something else
}
This works for all audio players and some video players like VLC, or Google Photos, but not with Samsung Video player.
So, I wanted to know if there is a universal way to check if video is currently active (that is, playing). Any solutions with code would be highly appreciated.

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

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);
}

Mixing audio with music player and AudioManager.requestAudioFocus

Is it possible to mix my app's audio with the music player audio?
I find that by default, android does mix the audio of the music player with my app.
However this stops working after calling AudioManager.requestAudioFocus method. Meaning, if the music player is playing, when my app starts playing, it stops the music player. I want them to play at the same time.
I request the audio focus so I can know when other interruptions such as phone calls have happened. I do want to stop my app's audio when a phone call happens.
My audio plays sin waves, it is a medical device. I request the audio focus with AudioManager.STREAM_MUSIC and AudioManager.AUDIOFOCUS_GAIN.
I am using AudioTrack to play them.

Categories

Resources