Chris
Chris

Reputation: 27

Why MediaPlayer stops on app restart state in android?

I made a soundboard app and it works but when i press home button on phone and I click to bring app back ,when i press a button for the sound it stops working(crashes)

private MediaPlayer mp;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mp = MediaPlayer.create(this, R.raw.iaminevitable);

        iaminevitable = (Button) findViewById(R.id.iaminevitable);
        iaminevitable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mp.start();
            }
        });

 @Override
    protected void onStop() {
        super.onStop();
        if (mp != null) {
            mp.reset();
            mp.release();
            mp = null;
        }

My Error Logcat =

Process: soundboard.test, PID: 5429
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
        at soundboard.test.MainActivity$6.onClick(MainActivity.java:96)
        at android.view.View.performClick(View.java:4780)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5293)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Upvotes: 0

Views: 110

Answers (4)

YuvrajsinhJadeja
YuvrajsinhJadeja

Reputation: 1422

add onResume method as below

 @Override
        protected void onResume() {
            super.onResume();
            if (mp != null) {
                //if want to start again when open app
                mp.start();

                //play when click button
    //            iaminevitable.setOnClickListener(new View.OnClickListener() {
    //                @Override
    //                public void onClick(View view) {
    //                    mp.start();
    //                }
    //            });
            }
        }

Upvotes: 0

Sayed Talha
Sayed Talha

Reputation: 119

have you given the required permissions? check for it and please add some more code to the storage etc or if the problem is activity lifecycle just add this code

@Override
public void onRestart()
{
    super.onRestart();
     recreate();
    finish();
    overridePendingTransition(0, 0);
    startActivity(getIntent());
    overridePendingTransition(0, 0);
}

Upvotes: 2

Ajay Kulkarni
Ajay Kulkarni

Reputation: 3039

You should understand activity lifecycle and permissions needed for audio files.
If I were you, I'd use below permissions.

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />  

In onCreate(), you should initialize and destroy in onDestroy() method. Check which method is called when an app goes to background. Learn. Here is a link to learn about activity lifecycle.

Upvotes: 0

Philippe Banwarth
Philippe Banwarth

Reputation: 17755

The media player is set to null when the application goes to background, but not recreated when the activity is brought again to foreground (onCreate() is not called). You should handle the initialization and destruction of objects in paired callbacks of the lifecycle, like

  • onCreate() and onDestroy()

  • onStart() and onStop()

  • onResume() and onPause()

Upvotes: 0

Related Questions