What am I doing wrong with the MediaPlayer in android app

179 views Asked by At
MediaPlayer easysong;
MediaPlayer normalsong;
MediaPlayer hardsong;

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

    btn3 = (Button)findViewById(R.id.button3);
    restartButton = (Button)findViewById(R.id.restartButton);
}
@Override
public void onClick(View v) {
    easysong = MediaPlayer.create(MainActivity.this, R.raw.arideniro);
    normalsong = MediaPlayer.create(MainActivity.this, R.raw.junior);
    hardsong = MediaPlayer.create(MainActivity.this, R.raw.ketsathis);
    if (v == btn3) {
        btn3.setVisibility(View.GONE);
        new Thread(new Runnable() {
            public void run() {
                if(mstop==1){
                    if(i==500){
                        easysong.start();}
                    else if(i==375){
                        normalsong.start();
                    }else if(i==250){
                        hardsong.start();
                    }}
                while (counter > 0) {
                    try {

                        Thread.sleep(i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    counter--;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            scoreText.setText(Integer.toString(counter));
                        }
                    });
                    if(i>150){
                        i/=1.01;}
                    else if((i>90-(dif/10))){
                        i-=1;
                    }
                }if (counter==0) {
                    mChronometer.stop();
                    playerStop();

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            restartButton.setVisibility(View.VISIBLE);
                        }
                    });
                }
            }
        }).start();
    }
    if(v == restartButton){
        counter = 101;
        i = 500 - dif;
        new Thread(new Runnable() {

            public void run() {
                if(i==500){
                    easysong.start();}
                else if(i==375){
                    normalsong.start();
                }else if(i==250){
                    hardsong.start();
                }
                while (counter > 0) {
                    try {

                        Thread.sleep(i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    counter--;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            scoreText.setText(Integer.toString(counter));
                        }
                    });
                    if(i>150){
                        i/=1.01;}
                    else if(i>90-(dif/10)){
                        i-=1;
                    }
                }if (counter==0) {
                    playerStop();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            restartButton.setVisibility(View.VISIBLE);
                        }
                    });
                }
            }
        }).start();
        restartButton.setVisibility(View.GONE);
    }

}
public void updateTimerText(final String time){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mTvTime.setText(time);
        }
    });
}
@Override
public void onUserLeaveHint() {      // This is to stop the music if user presses home button
    if(easysong.isPlaying()) {
        easysong.stop();
        easysong.release();
        easysong = null;
    } else if(normalsong.isPlaying()) {
        normalsong.stop();
        normalsong.release();
        normalsong = null;
    }
    else if (hardsong.isPlaying()){
        hardsong.stop();
        hardsong.release();
        hardsong = null;
    }
}
public void playerStop(){
    if (easysong.isPlaying()) {
        easysong.stop();
        easysong.release();
        easysong = null;
    }else if (normalsong.isPlaying()) {
        normalsong.stop();
        normalsong.release();
        normalsong = null;
    }else if (hardsong.isPlaying()) {
        hardsong.stop();
        hardsong.release();
        hardsong = null;
    }
}

The problem comes from the mediaplayer and this is all the code for it in my application, at some point the application even freezes, or the song doesn't always stop when the counter reaches 0. These are the errors that I face and I tried several things but I don't know what to do anymore. So what did I do wrong ? And apart from what I did wrong I want to ask, I made my app to stop the music if home button is pressed but how to make it to stop it if the back button is pressed ?

12-23 23:28:19.780 8897-8897/com.example.user.myapplication D/MediaPlayer: setSubtitleAnchor in MediaPlayer
12-23 23:28:35.193 8897-8897/com.example.user.myapplication W/MediaPlayer: mediaplayer went away with unhandled events
12-23 23:33:21.518 8897-8897/com.example.user.myapplication D/MediaPlayer: create failed:
java.io.IOException: Prepare failed.: status=0x80000000
at android.media.MediaPlayer._prepare(Native Method)
at android.media.MediaPlayer.prepare(MediaPlayer.java:1184)
at android.media.MediaPlayer.create(MediaPlayer.java:943)
at android.media.MediaPlayer.create(MediaPlayer.java:914)
at com.example.user.myapplication.MainActivity.onClick(MainActivity.java:114)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

12-23 23:33:22.163 8897-21377/com.example.user.myapplication E/AndroidRuntime: FATAL EXCEPTION: Thread-11
Process: com.example.user.myapplication, PID: 8897
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.media.MediaPlayer.isPlaying()' on a null object reference
at com.example.user.myapplication.MainActivity.playerStop(MainActivity.java:207)
at com.example.user.myapplication.MainActivity$2.run(MainActivity.java:167)
at java.lang.Thread.run(Thread.java:761)
1

There are 1 answers

0
Ranjith KP On

You can refer the below link to stop music in back button press.

Android Overriding onBackPressed()

An about the exception you facing is a null pointer exception in the function "playerstop" your calling isPlaying() on a null object,So initialize objects and add null checking properly.