MediaPlayer object getting Null after orientation change?

匿名 (未验证) 提交于 2019-12-03 01:26:01

问题:

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.

回答1:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!