是对上次创建的GVR播放器的优化与增强,建议先阅读上篇
本篇对播放器设置做了进一步优化:
1.支持手触模式
VR播放器有两种变换观看视频角度的模式:
陀螺仪模式和手触模式,GVR中默认陀螺仪模式一直存在,手触模式有一个开关可以打开或者关闭(默认关闭)
mVideoView.setTouchTrackingEnabled(true);//开启手触模式
2.取消手机放入VR盒子中的提示
GVR播放器在进入眼镜模式之前有一个页面提示我们将手机放入GoogleCardboard眼镜中,如图所示:
有时候手机静止不动,还不能进入眼镜模式,好像系统在监测是否我们在进行将手机放入眼镜盒子中这一过程,如果想去掉这一过程怎么办?
mVideoView.setTransitionViewEnabled(false);//设置将手机放入盒子中的提示取消
3.去除Android 7.0 without Google VR Services警告对话框
在Android 7.0的手机中使用GVR控件(全景图或者全景视频控件)总是会弹出一个警告对话框,如图所示:
纠结这个问题已经半年多了,现在终于可以解决这个问题了,经过用户反馈,Google终于修复了这个bug,处理如下:
将compile 'com.google.vr:sdk-videowidget:1.10.0'
换成compile 'com.google.vr:sdk-videowidget:1.40.0'
在1.40.0的这个版本中Google修复了without Google VR Services弹出警告对话框的问题
我现在直接说解决办法好像很简单的样子,其实这个问题的解决过程还是很漫长的,github和stackoverflow都有讨论这个问题,感兴趣的同学可以看一下:
Avoid Google VR service Warning for nonDaydream devices?
Avoid Warning after Android 7 Update for nonDaydream devices?
除了上面对播放器设置的优化处理之外,还对播放的Activity做了一些优化处理:
4.刚开始加载视频时,添加了一个等待的加载圈(一直转圈,提醒用户视频正在加载中,视频加载成功后隐藏)
5.播放控制面板的显示和隐藏(使用Handler发消息控制)
a.加载视频成功后,延时5秒隐藏控制面板
b.点击屏幕显示控制面板后,延时5秒隐藏
c.设置播放进度完成,手指离开进度条之后,延时5秒隐藏
d.视频播放完成之后,延时5秒隐藏
6.添加播放完成后重播的功能
a.添加重播按钮
b.播放完成后逻辑处理
c.点击重播按钮逻辑处理
7.播放器Activity状态的保存
播放器Activity状态的保存可以带给用户更舒适的体验,比如用户播放视频的过程中,按下home键或者锁屏键,之后又点击我们的应用,那么视频应该继续播放(用户看到哪个位置,就从哪个位置继续播放),如果未做播放器Activity状态的保存处理,可能播放会重头开始,也可能进入播放页面视频暂停播放,这些都是不好的用户体验。
对于播放Activity状态的保存主要代码如下:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
//保存当前播放进度,视频总时长,暂停播放状态
savedInstanceState.putLong(STATE_PROGRESS_TIME, mVideoView.getCurrentPosition());
savedInstanceState.putLong(STATE_VIDEO_DURATION, mVideoView.getDuration());
savedInstanceState.putBoolean(STATE_IS_PLAYING, isPlaying);
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
long progressTime = savedInstanceState.getLong(STATE_PROGRESS_TIME);
mVideoView.seekTo(progressTime);//得到播放进度进行设置
long duration = savedInstanceState.getLong(STATE_VIDEO_DURATION);
mTotalDuration = RegularExpress.parseDuration(duration);
mSeekBar.setMax((int) duration);
mSeekBar.setProgress((int) progressTime);
isPlaying = savedInstanceState.getBoolean(STATE_IS_PLAYING);
performChangePlayState(isPlaying);//根据保存的播放状态进行播放/暂停处理
}
private void handleIntent() {
Uri uri;
if ("".equals(mUrl)) {
uri = Uri.parse("http://resource.vr-store.cn/appfile/6cc160ba38394af0a251d4275ea66c29.mp4");//坝上的云
} else {
uri = Uri.parse(mUrl);
}
try {
mVideoView.loadVideo(uri, option);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent();
}
另外对播放Activity的生命周期方法也做了对应的播放状态处理:
@Override
protected void onPause() {
super.onPause();
// Prevent the view from rendering continuously when in the background.
mVideoView.pauseRendering();
// If the video is playing when onPause() is called, the default behavior will be to pause
// the video and keep it paused when onResume() is called.
//performChangePlayState(false);//停止播放,更换图标状态
mVideoView.pauseVideo();//只做暂停处理,不对isPlaying进行赋值(可能是按home键锁屏键等情况)
}
@Override
protected void onResume() {
super.onResume();
// Resume the 3D rendering.
mVideoView.resumeRendering();
performChangePlayState(isPlaying);//根据之前的状态执行播放/暂停处理(home/锁屏退出又进入的情况)
}
@Override
protected void onDestroy() {
//https://developers.google.com/vr/android/reference/com/google/vr/sdk/widgets/common/VrWidgetView#shutdown()
mVideoView.shutdown();
mHandler.removeMessages(HIDE);
mHandler = null;
super.onDestroy();
}
至此,这次VR播放器的更新优化已经说完了,贴一下播放Activity的代码:
public class PlayerActivity extends Activity implements View.OnClickListener {
private static final int HIDE = 0;//隐藏播放控制面板
private static final String STATE_PROGRESS_TIME = "progressTime";
private static final String STATE_VIDEO_DURATION = "videoDuration";
private static final String STATE_IS_PLAYING = "isPlaying";
private VrVideoView mVideoView;
private VrVideoView.Options option = new VrVideoView.Options();
private String mUrl;//上一个Activity传递过来的url播放地址
private TextView mVideoDuration;
private SeekBar mSeekBar;
private View mVideoPorgressContainer;
private String mTotalDuration;//视频总时长, 00:08格式
private ImageView mPlayView;
private boolean isPlaying;//播放/暂停状态标记
private View mVideoVr;
private View mReplay;//重播按钮
private View mVideoBuffer;//加载进度圈
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case HIDE:
mVideoPorgressContainer.setVisibility(View.GONE);//隐藏播放控制面板
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
Intent intent = getIntent();
mUrl = intent.getStringExtra("url");//上一个Activity传递过来的url播放地址
initView();
initData();
initListener();
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
//保存当前播放进度,视频总时长,暂停播放状态
savedInstanceState.putLong(STATE_PROGRESS_TIME, mVideoView.getCurrentPosition());
savedInstanceState.putLong(STATE_VIDEO_DURATION, mVideoView.getDuration());
savedInstanceState.putBoolean(STATE_IS_PLAYING, isPlaying);
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
long progressTime = savedInstanceState.getLong(STATE_PROGRESS_TIME);
mVideoView.seekTo(progressTime);//得到播放进度进行设置
long duration = savedInstanceState.getLong(STATE_VIDEO_DURATION);
mTotalDuration = RegularExpress.parseDuration(duration);
mSeekBar.setMax((int) duration);
mSeekBar.setProgress((int) progressTime);
isPlaying = savedInstanceState.getBoolean(STATE_IS_PLAYING);
performChangePlayState(isPlaying);//根据保存的播放状态进行播放/暂停处理
}
private void handleIntent() {
Uri uri;
if ("".equals(mUrl)) {
uri = Uri.parse("http://resource.vr-store.cn/appfile/6cc160ba38394af0a251d4275ea66c29.mp4");//坝上的云
} else {
uri = Uri.parse(mUrl);
}
try {
mVideoView.loadVideo(uri, option);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent();
}
private void initView() {
mVideoView = (VrVideoView) findViewById(R.id.video_view);
mPlayView = (ImageView) findViewById(R.id.play);
mReplay = findViewById(R.id.replay);
mVideoDuration = (TextView) findViewById(R.id.video_duration);
mSeekBar = (SeekBar) findViewById(R.id.video_progress);
mVideoPorgressContainer = findViewById(R.id.video_progress_container);
mVideoVr = findViewById(R.id.video_vr);
mVideoBuffer = findViewById(R.id.video_buffer);
mVideoView.setInfoButtonEnabled(false);//设置左侧信息原圈不可见
mVideoView.setFullscreenButtonEnabled(false);//设置全屏按钮不可见
mVideoView.setStereoModeButtonEnabled(false);//设置立体眼镜模式按钮不可见
mVideoView.setTransitionViewEnabled(false);//设置将手机放入盒子中的提示取消
mVideoView.setTouchTrackingEnabled(true);//开启手触模式
}
private void initData() {
handleIntent();
//第一次视频加载成功的时候,isPlaying应该为true,onLoadSuccess()方法会执行多次(初次加载视频,seekTo()被调用,home/锁屏退出再进入等都会执行)
isPlaying = true;
mSeekBar.setMax(Integer.MAX_VALUE);//防止刚加载视频时进度条跳一下又返回正常比例,主要因为第一次设置progress时,可能还未设置最大值
}
private void initListener() {
mPlayView.setOnClickListener(this);
mVideoVr.setOnClickListener(this);
mReplay.setOnClickListener(this);
mSeekBar.setOnSeekBarChangeListener(new SeekBarListener());
mVideoView.setEventListener(new VrVideoEventListener() {
@Override
public void onClick() {
//处理控制面板的显示和隐藏
int visibility = mVideoPorgressContainer.getVisibility();
if (visibility == View.VISIBLE) {
mVideoPorgressContainer.setVisibility(View.GONE);
} else {
mVideoPorgressContainer.setVisibility(View.VISIBLE);
hidePlayerControllerDelayed();//延时隐藏控制面板
}
}
/**
* Make the video mPlayView in a loop. This method could also be used to move to the next video in
* a playlist.
*/
@Override
public void onCompletion() {
//播放完成后的操作
//mVideoView.seekTo(0);//循环播放效果
performChangePlayState(false);
mVideoPorgressContainer.setVisibility(View.VISIBLE);
mReplay.setVisibility(View.VISIBLE);
hidePlayerControllerDelayed();//延时隐藏控制面板
}
@Override
public void onNewFrame() {
updateVideoProgress();
}
@Override
public void onLoadSuccess() {
mVideoBuffer.setVisibility(View.GONE);//视频加载成功隐藏加载进度圈
long duration = mVideoView.getDuration();//视频总时长,毫秒
mTotalDuration = RegularExpress.parseDuration(duration);
mSeekBar.setMax((int) duration);
performChangePlayState(isPlaying);//视频加载成功,开始播放更新状态
mVideoPorgressContainer.setVisibility(View.VISIBLE);//默认不可见,当加载视频成功后显示视频时长等信息
hidePlayerControllerDelayed();
/**这里解释一下为什么没把下面的判断逻辑操作放在performClickPlay()方法的else语句中,因为seekTo是耗时操作,不能马上完成,在else语句中虽然seekTo(0)
* 但是紧接着执行mVideoView.playVideo();方法,视频这时的播放位置还是在最后,会触发onCompletion()方法,该方法中的mReplay.setVisibility(View.VISIBLE);
* 就被执行了,结果就是视频虽然重播了,但是重播按钮还是显示的,为避免这种情况,故做了下面的判断操作[因为seekTo(0)之后会执行onLoadSuccess()方法]
*/
if (mReplay.getVisibility() == View.VISIBLE) {
mReplay.setVisibility(View.GONE);
}
}
@Override
public void onLoadError(String errorMessage) {
super.onLoadError(errorMessage);
Toast.makeText(PlayerActivity.this, "加载视频失败,换个高配手机试试吧...", Toast.LENGTH_LONG).show();
mVideoBuffer.setVisibility(View.GONE);//隐藏加载进度圈
}
@Override
public void onDisplayModeChanged(int newDisplayMode) {
super.onDisplayModeChanged(newDisplayMode);
}
});
}
/**
* 更新播放进度
*/
private void updateVideoProgress() {
long currentPosition = mVideoView.getCurrentPosition();
String currentPos = RegularExpress.parseDuration(currentPosition);
mSeekBar.setProgress((int) (currentPosition));//更新播放进度
StringBuilder sb = new StringBuilder();
sb.append(currentPos);
sb.append(" / ");
if (mTotalDuration == null)
mTotalDuration = RegularExpress.parseDuration(mVideoView.getDuration());
sb.append(mTotalDuration);
mVideoDuration.setText(sb);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.play:
performClickPlay();
break;
case R.id.video_vr:
performClickVideoVr();
break;
case R.id.replay:
performClickReplay();
break;
default:
break;
}
}
private void performClickReplay() {
mVideoView.seekTo(0);//重播时进度置为初始进度0
performChangePlayState(true);
mReplay.setVisibility(View.GONE);
}
/**
* 控制播放的状态
*
* @param b 是否播放
*/
private void performChangePlayState(boolean b) {
if (b) {
mVideoView.playVideo();
mPlayView.setImageResource(R.mipmap.stop);
isPlaying = true;
} else {
mVideoView.pauseVideo();
mPlayView.setImageResource(R.mipmap.play);
isPlaying = false;
}
}
private void performClickVideoVr() {
mVideoView.setDisplayMode(3);//enterStereoMode,眼镜模式
}
/**
* 播放暂停切换
*/
private void performClickPlay() {
if (isPlaying) {
mVideoView.pauseVideo();
mPlayView.setImageResource(R.mipmap.play);
isPlaying = false;
} else {
if (mReplay.getVisibility() == View.VISIBLE) {
mVideoView.seekTo(0);//重播按钮出现时,点击播放进行重播功能
}
mVideoView.playVideo();
mPlayView.setImageResource(R.mipmap.stop);
isPlaying = true;
}
}
@Override
protected void onPause() {
super.onPause();
// Prevent the view from rendering continuously when in the background.
mVideoView.pauseRendering();
// If the video is playing when onPause() is called, the default behavior will be to pause
// the video and keep it paused when onResume() is called.
//performChangePlayState(false);//停止播放,更换图标状态
mVideoView.pauseVideo();//只做暂停处理,不对isPlaying进行赋值(可能是按home键锁屏键等情况)
}
@Override
protected void onResume() {
super.onResume();
// Resume the 3D rendering.
mVideoView.resumeRendering();
performChangePlayState(isPlaying);//根据之前的状态执行播放/暂停处理(home/锁屏退出又进入的情况)
}
@Override
protected void onDestroy() {
//https://developers.google.com/vr/android/reference/com/google/vr/sdk/widgets/common/VrWidgetView#shutdown()
mVideoView.shutdown();
mHandler.removeMessages(HIDE);
mHandler = null;
super.onDestroy();
}
/**
* 播放器进度条监听
*/
private class SeekBarListener implements SeekBar.OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mVideoView.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
hidePlayerControllerDelayed();
mReplay.setVisibility(seekBar.getProgress() < seekBar.getMax() ? View.GONE : View.VISIBLE);
}
}
/**
* 延时隐藏播放器控制面板
*/
private void hidePlayerControllerDelayed() {
mHandler.removeMessages(HIDE);//为了保证可见的时间为5秒,去除之前延时隐藏的消息
mHandler.sendEmptyMessageDelayed(HIDE, 5000);
}
}
谢谢大家的支持!今后还会继续更新推出有关VR方面的博客,期待您的关注……
源码下载链接