0

I am trying to do video streaming that is working fine in portrait mode. Then I added code for onConfigurationChange to inflate another layout for landscape mode. but my app is getting crashed at this line:

int timeElapsed = mediaPlayer.getCurrentPosition();

Here I am getting mediaPlayer as null while it has been already initialized.

 MediaPlayer mediaPlayer;
     public void onCreate(final Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_detailvideo);
            context = this;
             mediaPlayer = new MediaPlayer();
            videoView = (VideoViewCustom) findViewById(R.id.videoplayer);
                try {
           videoView.setDimensions(1000, 200);
           videoView.getHolder().setFixedSize(1000, 200);
           videoView.setVideoPath(streamURL);
           videoView.setMediaController(mediaController);
       }
       catch (Exception e) {
           Log.e("Error", e.getMessage());
           e.printStackTrace();
       }
 videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                Log.d(TAG,"setOnPreparedListener");
                mediaPlayer = mp;
                int timeDuration = 0;
                boolean videoPlaying = false;
                if(savedInstanceState != null) {
                    timeDuration = savedInstanceState.getInt("timeduration");
                    videoPlaying = savedInstanceState.getBoolean("videoPlaying");
                    Log.d(TAG, "timeDuration saved:" + timeDuration);
                    Log.d(TAG, "videoPlaying saved:" + videoPlaying);


                    if (videoPlaying) {
                        mediaPlayer.seekTo(timeDuration);
                        mediaPlayer.start();
                    } else {
                        mediaPlayer.seekTo(timeDuration);
                    }
                }
                else
                {
                    mediaPlayer.start();
                }
                videoView.start();
                finalTime = mediaPlayer.getDuration();
                Log.d(TAG, "mp.getCurrentPosition():" + mediaPlayer.getCurrentPosition());
                videoSeekBar.setProgress(timeDuration);
                videoSeekBar.setMax(finalTime);
               durationHandler.postDelayed(updateSeekBarTime,1000);
            }
        });
Runnable updateSeekBarTime = new Runnable() {
        public void run() {

            //get current position
            int timeElapsed = mediaPlayer.getCurrentPosition(); //**is crashing here after orientation change**
            //set seekbar progress
            videoSeekBar.setProgress((int) timeElapsed);
            Log.d(TAG,"timeElapsed:"+timeElapsed);
            //repeat yourself that again in 100 miliseconds
            durationHandler.postDelayed(this,1000);
        }
    };

How can I avoid this issue? And how can I start video again in landscape from the same position where user stopped in portrait mode? Thanks.

NightFury
  • 13,436
  • 6
  • 71
  • 120
Erum
  • 790
  • 3
  • 14
  • 36
  • Check out the answer here: http://stackoverflow.com/questions/10046369/losing-mediaplayer-other-variables-when-device-is-rotated You can use a Service to play audio. So if device is rotated, you will neither loose track of audio, nor it will be stopped. – NightFury Sep 17 '15 at 07:04
  • i m not using service .... i m playing video and that is video streaming from server – Erum Sep 17 '15 at 07:09
  • Just an idea to help u out. http://jtdowdall.blogspot.com/2014/03/maintain-video-playback-during.html Good luck! – NightFury Sep 17 '15 at 07:37

1 Answers1

0

It is recommended to write a singleton class which extends MediaPlayer and then write methods which you want to use for media playing pausing resuming etc. Now you can save call pause() for that singleton class before rotation and when screen is rotated then resume() your player.

for pausing

Mediaplayer.pause();
media_length=Mediaplayer.getCurrentPosition();

for resuming

Mediaplayer.seekTo(length);
Mediaplayer.start();
Akber
  • 521
  • 4
  • 10