/** 更新播放进度条 */ private void updatePlayProgress() { int allTime = mSuperVideoView.getDuration(); int playTime = mSuperVideoView.getCurrentPosition(); int loadProgress = mSuperVideoView.getBufferPercentage(); int progress = playTime * 100 / allTime; mMediaController.setProgressBar(progress, loadProgress); }
/** 关闭视频 */ public void close() { mMediaController.setPlayState(MediaController.PlayState.PAUSE); stopHideTimer(true); stopUpdateTimer(); mSuperVideoView.pause(); mSuperVideoView.stopPlayback(); mSuperVideoView.setVisibility(GONE); }
/** 播放视频 should called after setVideoPath() */ private void startPlayVideo(int seekTime) { if (null == mUpdateTimer) resetUpdateTimer(); resetHideTimer(); mSuperVideoView.setOnCompletionListener(mOnCompletionListener); mSuperVideoView.start(); if (seekTime > 0) { mSuperVideoView.seekTo(seekTime); } mMediaController.setPlayState(MediaController.PlayState.PLAY); }
@Override public void onProgressTurn(MediaController.ProgressState state, int progress) { if (state.equals(MediaController.ProgressState.START)) { mHandler.removeMessages(MSG_HIDE_CONTROLLER); } else if (state.equals(MediaController.ProgressState.STOP)) { resetHideTimer(); } else { int time = progress * mSuperVideoView.getDuration() / 100; mSuperVideoView.seekTo(time); updatePlayTime(); } }
private void initView(Context context) { mContext = context; View.inflate(context, R.layout.super_vodeo_player_layout, this); mSuperVideoView = (SuperVideoView) findViewById(R.id.video_view); mMediaController = (MediaController) findViewById(R.id.controller); mProgressBarView = findViewById(R.id.progressbar); mCloseBtnView = findViewById(R.id.video_close_view); mTvBtnView = findViewById(R.id.video_share_tv_view); mDLNARootLayout = findViewById(R.id.rel_dlna_root_layout); mMediaController.setMediaControl(mMediaControl); mSuperVideoView.setOnTouchListener(mOnTouchVideoListener); setDLNAButton(false); setCloseButton(false); mDLNARootLayout.setVisibility(GONE); showProgressView(false); mDLNARootLayout.setOnClickListener(mOnClickListener); mDLNARootLayout.findViewById(R.id.txt_dlna_exit).setOnClickListener(mOnClickListener); mCloseBtnView.setOnClickListener(mOnClickListener); mTvBtnView.setOnClickListener(mOnClickListener); mProgressBarView.setOnClickListener(mOnClickListener); mAllVideo = new ArrayList<>(); }
/** * 加载并开始播放视频 * * @param videoUrl videoUrl */ private void loadAndPlay(VideoUrl videoUrl, int seekTime) { showProgressView(seekTime > 0); setCloseButton(true); if (TextUtils.isEmpty(videoUrl.getFormatUrl())) { Log.e("TAG", "videoUrl should not be null"); return; } mSuperVideoView.setOnPreparedListener(mOnPreparedListener); if (videoUrl.isOnlineVideo()) { mSuperVideoView.setVideoPath(videoUrl.getFormatUrl()); } else { Uri uri = Uri.parse(videoUrl.getFormatUrl()); mSuperVideoView.setVideoURI(uri); } mSuperVideoView.setVisibility(VISIBLE); startPlayVideo(seekTime); }
@Override public void onCompletion(MediaPlayer mediaPlayer) { stopUpdateTimer(); stopHideTimer(true); mMediaController.playFinish(mSuperVideoView.getDuration()); mVideoPlayCallback.onPlayFinish(); // Toast.makeText(mContext, "视频播放完成", Toast.LENGTH_SHORT).show(); }
@Override public void onPlayTurn() { if (mSuperVideoView.isPlaying()) { pausePlay(true); } else { goOnPlay(); } }
/** 更新播放的进度时间 */ private void updatePlayTime() { int allTime = mSuperVideoView.getDuration(); int playTime = mSuperVideoView.getCurrentPosition(); mMediaController.setPlayProgressTxt(playTime, allTime); }
/** 更换清晰度地址时,续播 */ private void playVideoAtLastPos() { int playTime = mSuperVideoView.getCurrentPosition(); mSuperVideoView.stopPlayback(); loadAndPlay(mNowPlayVideo.getPlayUrl(), playTime); }
/** * 继续播放 */ public void goOnPlay() { mSuperVideoView.start(); mMediaController.setPlayState(MediaController.PlayState.PLAY); resetHideTimer(); resetUpdateTimer(); }
/** * 暂停播放 * * @param isShowController 是否显示控制条 */ public void pausePlay(boolean isShowController) { mSuperVideoView.pause(); mMediaController.setPlayState(MediaController.PlayState.PAUSE); stopHideTimer(isShowController); }
/** 如果在地图页播放视频,请先调用该接口 */ @SuppressWarnings("unused") public void setSupportPlayOnSurfaceView() { mSuperVideoView.setZOrderMediaOverlay(true); }