Getting Wrong state Error in MediaPlayer seek

Viewed 1253

I am a newbie is android/java world and I am playing a video(remote URL) on click of a listview. I am trying to get the video to pause when the app goes in backhround and then resume when user reopens the app. I have looked through several questions about this and on implementing what has been said I am getting Attempt to perform seekTo in wrong state: mPlayer=0x0, mCurrentState=0 and the video is starting from the beginning. This is my activity code:

public class VideoPlayback extends Activity {
    private static final String TAG = Dashboard.class.getSimpleName();
    ProgressBar progressBar = null;
    MediaPlayer mMediaPlayer = new MediaPlayer();
    int media_length;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_playback);
        Intent intent = getIntent();
        String message = intent.getStringExtra(Dashboard.EXTRA_MESSAGE);
        String link = "http://" + message;
        final VideoView vidView = (VideoView)findViewById(R.id.myVideo);
        progressBar = (ProgressBar) findViewById(R.id.progressbar);
        //mMediaPlayer = new MediaPlayer();
        Uri vidUri = Uri.parse(link);
        Log.d(TAG, link);
        vidView.setVideoURI(vidUri);
        try {
            MediaController vidControl = new MediaController(this);
            vidControl.setAnchorView(vidView);
            vidView.setMediaController(vidControl);


        }catch (Exception e)
        {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        vidView.requestFocus();
        vidView.start();
        progressBar.setVisibility(vidView.VISIBLE);
        vidView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
                mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                    @Override
                    public void onVideoSizeChanged(MediaPlayer mp, int arg1, int arg2) {
                        // TODO Auto-generated method stub
                        Log.d(TAG, "Changed");
                        progressBar.setVisibility(vidView.GONE);
                        mp.start();
                    }
                });
            }

        });

    }

These are the override functions:

@Override
protected void onPause(){
    super.onPause();
    Log.d(TAG, "Pause called");
    mMediaPlayer.pause();
    media_length = mMediaPlayer.getCurrentPosition();



}
@Override
protected void onResume(){
    super.onResume();
    Log.d(TAG, "Resume");
    mMediaPlayer.seekTo(media_length);
    mMediaPlayer.start();


}

This is from logcat:

03-15 13:28:17.184 25886-25886/ E/MediaPlayer: error (-38, 0)
03-15 13:28:17.184 25886-25886/ E/MediaPlayer: start called in state 0
03-15 13:28:17.239 25886-25886/ D/MediaPlayer: Couldn't open file on client side, trying server side
03-15 13:28:17.247 25886-25903/ I/MediaHTTPConnection: proxyName: 0.0.0.0 0
03-15 13:28:17.265 25886-25886/ E/MediaPlayer: Error (-38,0)
03-15 13:28:20.611 25886-25886/ D/MediaPlayer: getMetadata
03-15 13:28:20.817 25886-25886/ D/Dashboard: Changed
03-15 13:28:20.818 25886-26816/ W/MediaPlayer: info/warning (3, 0)
03-15 13:28:25.077 25886-25886/ D/Dashboard: Pause called
03-15 13:28:25.077 25886-25886/ E/MediaPlayer: pause called in state 0
03-15 13:28:30.862 25886-25886/ D/Dashboard: **Resume**
03-15 13:28:30.862 25886-25886/ E/MediaPlayer: Attempt to perform seekTo in wrong state: mPlayer=0x0, mCurrentState=0
03-15 13:28:30.866 25886-25886/ E/MediaPlayer: start called in state 0

I am new to this technology and would appreciate all the help.

0 Answers
Related