android media player on background not stop playing while open other activity

2k views Asked by At

My application has 3 options(Buttons) in my main activity and a media player that plays a song when the application is launched. The media player starts correctly when the application is launched, but If i press a button to start a new activity while the audio is still playing, the application crushes (unfortunately app has stopped). If i press "OK" in the message it opens the new activity and media player stops.

My aim is to start the new activity and stop the media player (song).

Could anyone help me with these issues.?

TextView  logoname;
Button autismlogo,visionlogo,hearinglogo;
private SensorManager mSensorManager;
private ShakeEventListener mSensorListener;
MediaPlayer player; 


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


logoname = (TextView)findViewById(R.id.logotext);
autismlogo = (Button)findViewById(R.id.autismbutton);
visionlogo = (Button)findViewById(R.id.visionbutton);
hearinglogo = (Button)findViewById(R.id.hearingbutton);



final MediaPlayer player = MediaPlayer.create(MainActivity.this, R.raw.welcome); 
player.start();

// ---SENSORS-------- 

mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorListener = new ShakeEventListener();   

mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {   
  public void onShake() {
      Intent vision = new Intent(getApplicationContext(),Vision_main.class);
    startActivity(vision);
  }
});


// ----ON CLICK EVENTS -----------
autismlogo.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {


        Intent autism = new Intent(getApplicationContext(),Autism_main.class);
        startActivity(autism);

    }

});

visionlogo.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {



        Intent vision = new Intent(getApplicationContext(),Vision_main.class);
        startActivity(vision);

    }
});

hearinglogo.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        Intent hearing = new Intent(getApplicationContext(),Hearing_main.class);
        startActivity(hearing);     
    }
});

}

public void onResume() {
    super.onResume();

    mSensorManager.registerListener(mSensorListener,
            mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_UI);
}

public void onPause() {
    super.onPause();
    player.stop();
    mSensorManager.unregisterListener(mSensorListener);
}

Log Cat error

E/AndroidRuntime(27610): FATAL EXCEPTION: main E/AndroidRuntime(27610): Process: com.giorgospapadopoulos.move4all, PID: 27610 E/AndroidRuntime(27610): java.lang.RuntimeException: Unable to pause activity {com.giorgospapadopoulos.move4all/com.giorgospapadopoulos.move4all.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.stop()' on a null object reference E/AndroidRuntime(27610): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3260) E/AndroidRuntime(27610): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3219) E/AndroidRuntime(27610): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3194) E/AndroidRuntime(27610): at android.app.ActivityThread.access$1000(ActivityThread.java:151) E/AndroidRuntime(27610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1314) E/AndroidRuntime(27610): at android.os.Handler.dispatchMessage(Handler.java:102) E/AndroidRuntime(27610): at android.os.Looper.loop(Looper.java:135) E/AndroidRuntime(27610): at android.app.ActivityThread.main(ActivityThread.java:5254) E/AndroidRuntime(27610): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(27610): at java.lang.reflect.Method.invoke(Method.java:372) E/AndroidRuntime(27610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) E/AndroidRuntime(27610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) E/AndroidRuntime(27610): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.stop()' on a null object reference E/AndroidRuntime(27610): at com.giorgospapadopoulos.move4all.MainActivity.onPause(MainActivity.java:220) E/AndroidRuntime(27610): at android.app.Activity.performPause(Activity.java:6101) E/AndroidRuntime(27610): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1310) E/AndroidRuntime(27610): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3246) E/AndroidRuntime(27610): ... 11 more

2

There are 2 answers

1
George Daramouskas On

Your error is quite clear. You get a NullPointerException at line 120 in your onPause() method. That's because you haven't created the player object and you try to invoke one of it's methods.

You have declared it as a global variable but you haven't created it. You create a different player object inside your onCreate() method but that's just a local variable.

0
Mahesh Kumar Prajapati On
  1. Firstly in line

final MediaPlayer player = MediaPlayer.create(MainActivity.this, R.raw.welcome);

not declare MediaPlayer player as local, it should be global.

  1. Use this in onPause()

    if(player!=null){ player.stop(); }

  2. in onResume()

    if( player!=null){ player.start(); }

by above code player will play music when app in foreground, when app in background player stop playing music and when app again comes in foreground state player will play music.