0

If I press the home button without playing the audio. The app crashes, but it works perfectly when I play the audio and press the home button. Here's my code:

@Override
protected void onPause()
{
    Context context = getApplicationContext();
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    if (!taskInfo.isEmpty()) {
        ComponentName topActivity = taskInfo.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            play.pause();
            but19.setBackgroundResource(R.drawable.play);
        }
    }
    super.onPause();
}
@Override
public void onBackPressed(){
    if(play!=null && (play.isPlaying())){
        if(play.isPlaying())
            play.stop();
        play.release();
    }
    super.onBackPressed();
}
Kunal Yadav
  • 139
  • 10
Devk
  • 117
  • 11

4 Answers4

0

problem is in onPause() method. you need to check if play is playing before pausing it like you did in onBackPressed() and it is better to check for null also to avoid crashes

Prashant
  • 1,593
  • 1
  • 17
  • 32
0

Its crashing becasue play is null when you pause it crash so add chcek before calling pasue state.

Change play.pause(); to

if( play!=null && play.isPlaying()){ play.pause(); }

Complete Code:

@Override
protected void onPause()
{
    Context context = getApplicationContext();
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    if (!taskInfo.isEmpty()) {
        ComponentName topActivity = taskInfo.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
          if( play!=null && play.isPlaying()){
              play.pause();
             }
            but19.setBackgroundResource(R.drawable.play);
        }
    }
    super.onPause();
}
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
0

The Media framework is a very strict state machine, and it's really cumbersome to handle all the different states.

If the problem is a NullPointerException, add a null check.
But even if it's not null, you will get crashes in production, since pause() is allowed to be called only in {Started, Paused, PlaybackCompleted} states, or else it will crash your app.

When the audio is playing, it is on the correct state for your onPause() { play.pause() }.

Take a look at my answer here for a helper wrapper for MediaPlayer, which could simplify state check.

Community
  • 1
  • 1
Amir Uval
  • 14,425
  • 4
  • 50
  • 74
0

It would be better if you provide an error log. However, maybe I can answer your question:

if(play!=null && (play.isPlaying()))

In the if above, when the audio is not playing, perhaps the variable play could be "null", then the app will try to verify the second expression (play.isPlaying()) and because play is null, it will throw a NullPointerException.

When the audio is playing these two expressions will be true, because play isn't null and the audio is playing, so this could be why your app doesn't crash when playing the audio.

I don't know if I was clear enough, but I hope it helps.

Cheers

UeliDeSchwert
  • 1,146
  • 10
  • 23
Gui Herzog
  • 5,327
  • 2
  • 18
  • 22