How to detect if video is playing in the foreground? - android

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.

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

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
}

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.

Android media player(android.media.MediaPlayer) two music concurrently

Before open my application I played some of the music from default music player. With that background music I opened my application with the mediaplayer (android.media.MediaPlayer) to play the mp3 available in assets.
The default doesn't stop the music and I am getting two music concurrently from the device.
How do I stop or pause music of the default music player whenever my app started playing the music?
I think thats impossible. The default music player is a different app and out of your control. android does not allow controlling other app features from your app.
I had the same problem. I was trying to stop the background music of the default music player (if it is playing) when my VoIP SIP application receives an incoming call. I came up with:
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, true);
This does not stop or pause the music, but it make the music stream silent. Do not forget to unmute it later. This solution works well only if your second sound does not use the same stream.

Categories

Resources