google vr 入门之制作简易的VR播放器(二)

另外对播放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 {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值