arun
arun

Reputation: 31

Illegal state exception media player

I have a button in my activity. on click of button I am calling finish(), problem is I am getting Illegal state exception on call to finish. please help.

import java.io.IOException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.MediaController;
import android.widget.MediaController.MediaPlayerControl;

import com.mds.perfumastic.R;
import com.mds.perfumastic.constants.Constants;

public class AudioPlayerActivity extends Activity implements
    OnPreparedListener , MediaPlayerControl{
    private Handler handler = new Handler();
    private final MediaPlayer mediaPlayer = new MediaPlayer();
    private MediaController mediaController;
    private ProgressDialog progressBar;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audioplayer);
        progressBar =  new ProgressDialog(this);
        progressBar.setMessage("Playing...");
        progressBar.show();
        String audioUrl = getIntent().getStringExtra(Constants.INTENT_EXTRA_AUDIO_URL);
        mediaController = new MediaController(this);


        try {
            mediaPlayer.setOnPreparedListener(this);
            mediaPlayer.reset();
            mediaPlayer.setDataSource(audioUrl);
            mediaPlayer.prepare();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    @Override
    public void onPause() {
        super.onPause();
        progressBar.dismiss();
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
        }
        mediaPlayer.release();
    }

    // --MediaPlayerControl
            // methods----------------------------------------------------
            public void start() {
                mediaPlayer.start();
            }

            public void pause() {
                mediaPlayer.pause();
            }

            public int getDuration() {
                return mediaPlayer.getDuration();
            }

            public int getCurrentPosition() {
                return mediaPlayer.getCurrentPosition();
            }

            public void seekTo(int i) {
                mediaPlayer.seekTo(i);
            }

            public boolean isPlaying() {
                return mediaPlayer.isPlaying();
            }

            public int getBufferPercentage() {
                return 0;
            }

            public boolean canPause() {
                return true;
            }

            public boolean canSeekBackward() {
                return true;
            }

            public boolean canSeekForward() {
                return true;
            }

            // --------------------------------------------------------------------------------

            public void onPrepared(final MediaPlayer mediaPlayer) {
                Log.d("TAG", "onPrepared");
                progressBar.hide();
                mediaController.setMediaPlayer(this);
                mediaController.setAnchorView(findViewById(R.id.newView));
                mediaPlayer.start();

                handler.post(new Runnable() {
                    public void run() {
                        mediaController.setEnabled(true);
                        mediaController.show(mediaPlayer.getDuration());
                    }
                });
            }
            /** Use screen touches to toggle the video between playing and paused. */
            @Override
            public boolean onTouchEvent(MotionEvent ev) {
                if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                    if (mediaPlayer.isPlaying()) {
                        mediaPlayer.pause();
                    } else {
                        mediaPlayer.start();
                        mediaController.show(mediaPlayer.getDuration());
                    }
                    return true;
                } else {
                    return false;
                }
            }
            public void onClick(View v) {
                if(v.getId() == R.id.btn_back) {

                    finish();
                }
            }

}

here is log messages:

12-12 11:55:34.791: E/AndroidRuntime(1336): FATAL EXCEPTION: main
12-12 11:55:34.791: E/AndroidRuntime(1336): java.lang.IllegalStateException
12-12 11:55:34.791: E/AndroidRuntime(1336):     at android.media.MediaPlayer.getCurrentPosition(Native Method)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at com.mds.perfumastic.activites.AudioPlayerActivity.getCurrentPosition(AudioPlayerActivity.java:80)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at android.widget.MediaController.setProgress(MediaController.java:415)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at android.widget.MediaController.access$500(MediaController.java:71)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at android.widget.MediaController$3.handleMessage(MediaController.java:386)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at android.os.Looper.loop(Looper.java:137)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at java.lang.reflect.Method.invokeNative(Native Method)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at java.lang.reflect.Method.invoke(Method.java:511)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-12 11:55:34.791: E/AndroidRuntime(1336):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 2

Views: 6035

Answers (2)

code8x
code8x

Reputation: 1970

Currently I am working on a project that uses MediaPlayer class. I had an error same as you when app gets into onDestroy and onBackPressed states. But I already called mp.release() method to release mediaplayer object. But this hadn't be enough, I still got the same error. Error was due to getDuration() method that I use in my Handler. So I just add removeCallbacks() method before calling mp.release(); :

mHandler.removeCallbacks(mUpdateTimeTask);

here mHandler is my Handler object and mUpdateTimeTask is Runnable.

It worked for me!. That way I never got "IllegalStateException" error. I hope this solution helps others.

Upvotes: 1

Michael
Michael

Reputation: 58507

One possible cause of this exception is that the MediaController could still be querying the MediaPlayer object for its current position even as you're finishing the Activity and releasing the MediaPlayer object. Try adding a mediaController.hide() before doing mediaPlayer.stop().

Since you're calling finish you might also want to override onDestroy, since onPauseis for when the Activity goes into the background but still is alive.

Upvotes: 4

Related Questions