Good Squishy
Good Squishy

Reputation: 73

Android app crash when call onPause

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.

Upvotes: 0

Views: 2203

Answers (3)

Pete Houston
Pete Houston

Reputation: 15089

It should be like this:

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

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

Upvotes: 2

Lalit Poptani
Lalit Poptani

Reputation: 67296

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();

Upvotes: 3

Travis Webb
Travis Webb

Reputation: 15028

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

Upvotes: 0

Related Questions