Android 应用程序在调用 onPause 时崩溃
你好,我是 Android 开发新手,我正在使用 Eclipse,并且一直在开发一些音板。我的问题是这样的:
public class BernardsActivity extends Activity {
MediaPlayer eileanBeag;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bernards_layout);
Button eileanBeagButton = (Button) findViewById(R.id.ButtonB1);
eileanBeagButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
eileanBeag = MediaPlayer.create(getApplicationContext(), R.raw.eilean_beag);
eileanBeag.start();
}
});
***public void onPause() {
eileanBeag.release();
}***
}
应用程序工作正常,所有音频都播放良好,但是当我使用后退按钮离开应用程序时,它告诉我该过程已意外停止,我必须强制退出。
当我在 onCreate 方法之外定义 MediaPlayer 时,该方法工作正常,但我希望使用许多声音并减少加载时间,我只在按下按钮后才定义它。
我尝试过使用 OnCompleteListener,它似乎确实有效,但它会提前切断我的声音剪辑。
因此,我们将非常感谢对此的任何帮助。
谢谢。
Hello I am newish to android dev, I am using Eclipse and I have been developing some soundboards. my issue is this:
public class BernardsActivity extends Activity {
MediaPlayer eileanBeag;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bernards_layout);
Button eileanBeagButton = (Button) findViewById(R.id.ButtonB1);
eileanBeagButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
eileanBeag = MediaPlayer.create(getApplicationContext(), R.raw.eilean_beag);
eileanBeag.start();
}
});
***public void onPause() {
eileanBeag.release();
}***
}
the app works fine, all audio is playing well, but when I leave the app using the back button, it tells me that the process has stop unexpectedly and I have to force quit.
the method works fine when I define the MediaPlayer outside of the onCreate method, but I wish to use many sounds and to cut down on loading time I only define it once the button has been pushed.
I have tried using an OnCompleteListener and it does seem to work but then it cuts my sound clip off early.
so any help on this would be very much appreciated.
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
唯一看起来有问题的是,
在调用
eileanBeag.release();
之前,您应该调用eileanBeag.stop();
。当你正在玩的时候你不能直接释放一个Player。您还缺少super.onPause();
The only thing that looks like a problem is,
before calling
eileanBeag.release();
you should calleileanBeag.stop();
. You cannot directly release a Player when you are playing. Also you are missingsuper.onPause();
应该是这样的:
It should be like this:
您需要调用
super.onPause()
作为onPause
方法内的第一个语句You need to call
super.onPause()
as the first statement inside of youronPause
method