1

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.

Mike
  • 374
  • 1
  • 8
  • 22

1 Answers1

0

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

prabushitha
  • 1,433
  • 12
  • 13