方向改变后MediaPlayer对象变为空?

时间:2015-09-17 06:43:10

标签: android video-streaming android-mediaplayer android-seekbar

我正在尝试在纵向模式下正常工作的视频流。然后我添加了onConfigurationChange的代码来为横向模式扩充另一个布局。但我的应用程序正在崩溃:

int timeElapsed = mediaPlayer.getCurrentPosition();

我已将mediaPlayer作为 null ,但它已经初始化了。

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

如何避免此问题?如何从用户在纵向模式下停止的相同位置再次启动视频?感谢。

1 个答案:

答案 0 :(得分:0)

建议编写一个扩展MediaPlayer的单例类,然后编写要用于媒体播放暂停恢复等的方法。现在可以在旋转之前和旋转屏幕时为该单例类保存调用暂停() resume()你的玩家。

暂停

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

恢复

Mediaplayer.seekTo(length);
Mediaplayer.start();