问题描述
我想这样做是在肖像模式下工作的罚款视频流。然后,我添加了 onConfigurationChange
code夸大另一个布局横向模式。但我的应用程序在这一行越来越崩溃:
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();
在这里,我得到的MediaPlayer
为空的,而它是否已被初始化。
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.
推荐答案
建议写延伸MediaPlayer的一个单类,然后写要使用媒体播放暂停等恢复现在,你可以调用保存方法暂停()为前旋转屏幕时,旋转然后继续()您的播放器。该单例类
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.
用于暂停
Mediaplayer.pause();
media_length=Mediaplayer.getCurrentPosition();
为恢复
Mediaplayer.seekTo(length);
Mediaplayer.start();
这篇关于MediaPlayer对象定位改变后得到空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!