Вопрос

My app has a button which is playing a short mp3 file when clicked. I want to release and reuse the mediaplayer object properly (so it will not interfere other apps) when e.g. user gets a phone call, or home button is being clicked.

If I implement onPause and onStop this way:

@Override
public void onPause() {
    super.onPause();
    mp.release();
    mp = null;
}

@Override
public void onStop() {
    super.onStop();
    mp.release();
    mp = null;
}

then how do I re-use mp when onRestart is being called? is it the right way to do that? maybe I should use mp.stop()?

thanks

Edit: I found a solution myself. re-creating the object again:

@Override
publib void onResume() {
    super.onResume();
    mp = new MediaPlayer();
}

does the job. still a noob...:) thanks

Это было полезно?

Решение

Use onCompletion

@Override
                                public void onCompletion(MediaPlayer mp) {
                                    // TODO Auto-generated method stub
                                    myStereo.setLooping(true);
                                    myStereo.release();
                                    try {
                                        myStereo.prepare();
                                    } catch (IllegalStateException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                    myStereo.start();


                }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top