我在Android应用程序中的MediaPlayer做错了什么


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;
    }
}

问题来自MediaPlayer,这是我应用程序中的所有代码,在某个时候,应用程序甚至冻结,或者在计数器达到0时,歌曲并不总是停止。这是我面对的错误我尝试了几件事,但我不知道该怎么办。那我做错了什么?除了我想问的是我做错了什么之外,如果按下了主页按钮,我还是让我的应用程序停止音乐,但是如果按下了返回按钮,则如何停止它?

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)

您可以将下面的链接引用到后按钮中停止音乐。

Android覆盖onbackpresse()

您面临的例外是 null指针异常在函数" playerstop" 您的调用 iSplaying() null对象上,因此初始化对象并正确添加空检查。

相关内容

  • 没有找到相关文章

最新更新