简体   繁体   English

调用onPause时Android应用崩溃

[英]Android app crash when call onPause

Hello I am newish to android dev, I am using Eclipse and I have been developing some soundboards. 您好,我是android dev的新手,我正在使用Eclipse,并且一直在开发一些音板。 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. 当我在onCreate方法之外定义MediaPlayer时,该方法可以正常工作,但是我希望使用多种声音并减少加载时间,只有在按下按钮后才定义它。

I have tried using an OnCompleteListener and it does seem to work but then it cuts my sound clip off early. 我尝试使用OnCompleteListener,它似乎确实起作用,但随后它会尽早切断我的声音片段。

so any help on this would be very much appreciated. 因此,对此的任何帮助将不胜感激。

thanks. 谢谢。

The only thing that looks like a problem is, before calling eileanBeag.release(); 看起来唯一有问题的是,在调用eileanBeag.release();之前eileanBeag.release(); you should call eileanBeag.stop(); 您应该调用eileanBeag.stop(); . You cannot directly release a Player when you are playing. 播放时不能直接释放播放器。 Also you are missing super.onPause(); 你也缺少super.onPause();

It should be like this: 应该是这样的:

public void onPause() {
    super.onPause();
    eileanBeag.pause();
}

protected void onDestroy() {
    super.onDestroy();
    if(eileanBeag != null) {
        eileanBeag.stop();  
        eileanBeag.release();
    }
}

您需要在onPause方法中调用super.onPause()作为第一条语句

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

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