简体   繁体   English

Android mediaPlayer导致强制关闭

[英]Android mediaPlayer causing force close onquit

My code is force closing as soon as I hit the back button to leave the activity. 一旦按下返回按钮,我的代码将立即关闭以退出活动。 Here it is. 这里是。 I think what it does is relatively self explanatory. 我认为它的作用是相对自我解释的。 When called, it shows some text plus a button that when tapped starts a recording playing. 调用时,它会显示一些文本以及一个按钮,轻按该按钮即可开始播放录音。 When someone hits the back button, it should go back to the previous activity and stop the recording. 当某人按下“后退”按钮时,它应返回到上一个活动并停止录制。 Thanks for helping me! 感谢您的帮助!

public class ToBeOrNot extends Activity {

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

    }
MediaPlayer mediaPlayer;

public void react(View view) {
    mediaPlayer = MediaPlayer.create(ToBeOrNot.this, R.raw.achord);
    mediaPlayer.start(); 
}
protected void onStop(){
mediaPlayer.release();
mediaPlayer = null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.activity_to_be_or_not, menu);
// Locate MenuItem with ShareActionProvider
return true;
}

}

Please provide log-cat error logs, otherwise its pain to look whats wrong. 请提供log-cat错误日志,否则请查看错误内容。

But i guess you need to. 但是我想你需要。

call stop() on media player in onPause()

If you want it to keep playing then you need to use service. 如果您希望它继续播放,则需要使用服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM