Android的媒体播放器强制力接近onquit [英] Android mediaPlayer causing force close onquit
问题描述
我的code是力量,只要我打的后退按钮离开活动闭幕。这里是。
我认为它的作用是比较自我解释。在调用时,它显示了一些文本加一个按钮,拍了拍当启动录音播放。当有人点击后退按钮,它应该回到previous活动,并停止录制。感谢您帮助我!
公共类ToBeOrNot延伸活动{@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_to_be_or_not); }
MediaPlayer的媒体播放器;公共无效反应(查看视图){
媒体播放器= MediaPlayer.create(ToBeOrNot.this,R.raw.achord);
mediaPlayer.start();
}
保护无效的onStop(){
mediaPlayer.release();
媒体播放器= NULL;
}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
//getMenuInflater().inflate(R.menu.activity_to_be_or_not,菜单);
//与ShareActionProvider找到菜单项
返回true;
}}
请提供登录猫错误日志,否则它的痛苦看看什么是错。
不过,我想你需要。
上的onPause媒体播放器,通话停止()()
如果你想让它继续玩,那么你需要使用的服务。
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.
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.
这篇关于Android的媒体播放器强制力接近onquit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!