开发者

Android app crash when call onPause

开发者 https://www.devze.com 2023-04-11 11:49 出处:网络
Hello I am newish to android dev, I am using Eclipse and I have been developing some soundboards. my issue is this:

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.


The only thing that looks like a problem is, before calling eileanBeag.release(); you should call eileanBeag.stop();. You cannot directly release a Player when you are playing. Also you are missing 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();
    }
}


You need to call super.onPause() as the first statement inside of your onPause method

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号