0

我创建了一个卡拉 OK 应用程序。但我无法在没有错误的情况下停止 MediaPlayer。当我按下返回按钮回家时,我得到“对不起,应用程序已停止工作”如果我尝试开始另一个活动,也会发生同样的事情。

这是我的代码。

public class Main extends Activity implements MediaPlayerControl, OnClickListener {
private MediaController mMediaController;
private MediaPlayer mMediaPlayer;
public TextView subtitles;
Handler mHandler = new Handler();


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    subtitles = (TextView) findViewById(R.id.subs1);
    Button btnvol = (Button) findViewById(R.id.volc);
    btnvol.setOnClickListener(this);

    mMediaPlayer = new MediaPlayer();
    mMediaController = new MediaController(this);

    mMediaController.setMediaPlayer(Main.this);
    mMediaController.setAnchorView(findViewById(R.id.audioView));

    try {
        mMediaPlayer
                .setDataSource(
                        this,
                        Uri.parse("android.resource://com.app.audioplayer/raw/buchatri"));
        mMediaPlayer.prepare();
    } catch (IllegalStateException e){
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
              final long currentPos = mMediaPlayer.getCurrentPosition();
              if (currentPos < 5130) {
                    subtitles.setText("Stupid");
                } else if (currentPos > 5130 && currentPos < 10572) {
                    subtitles.setText("Sorry");
                } else if (currentPos > 10572 && currentPos < 10597) {
                    subtitles.setText("555+");
                } else if (currentPos > 15312 && currentPos < 18478) {
                    subtitles.setText("OMG");
                } else if (currentPos > 18478 && currentPos < 24191) {
                    subtitles.setText("AFK");
                } else if (currentPos > 24191 && currentPos < 28137) {
                    subtitles.setText("Help me !");
                } else if (currentPos > 28137 && currentPos < 29500) {
                    subtitles.setText("Oh no !");
                }
              mHandler.sendEmptyMessageDelayed(0, 1);
        }
    };
    mHandler.sendEmptyMessage(0);


    mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
        public void onPrepared(MediaPlayer mp) {
            mHandler.post(new Runnable() {
                public void run() {
                    mMediaController.show(0);
                    mMediaPlayer.start();
                }
            });
        }
    });
}

protected void onDestroy() {
    super.onDestroy();
    mMediaPlayer.stop();
    mMediaPlayer.release();
}

public boolean canPause() {
    return true;
}

public boolean canSeekBackward() {
    return true;
}

public boolean canSeekForward() {
    return true;
}

public int getBufferPercentage() {
    int percentage = (mMediaPlayer.getCurrentPosition() * 100)
            / mMediaPlayer.getDuration();
    return percentage;
}

public int getCurrentPosition() {
    return mMediaPlayer.getCurrentPosition();
}

public int getDuration() {
    return mMediaPlayer.getDuration();
}

public boolean isPlaying() {
    return mMediaPlayer.isPlaying();
}

@Override
public void pause() {
    // TODO Auto-generated method stub
    super.onPause();
    if (mMediaPlayer.isPlaying()) {
        mMediaPlayer.stop();
    }

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

public void seekTo(int pos) {
    mMediaPlayer.seekTo(pos);

}

public void start() {
    mMediaPlayer.start();
}

public boolean onTouchEvent(MotionEvent event) {
    mMediaController.show(0);
    return false;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}
}

我想我对“if else”有一个错误。我怎样才能解决这个问题 ?

4

2 回答 2

0

你可以试试这个

发布后,您应该将 mediaplayer 设置为 null

private void releaseMediaPlayer() {
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }
于 2012-12-12T16:57:51.160 回答
0

先尝试mMediaPlayer.stop()然后释放它mMediaPlayer.release()

于 2012-12-12T17:09:41.620 回答