必須須要的四步驟代碼以下所示java
//建立基礎視頻播放器,通常播放器的功能 BasisVideoController controller = new BasisVideoController(this); //設置控制器 mVideoPlayer.setVideoController(controller); //設置視頻播放連接地址 mVideoPlayer.setUrl(url); //開始播放 mVideoPlayer.start(); ```- 開始播放
//播放視頻 videoPlayer.start(); `
android
建立視頻播放器git
PlayerFactory playerFactory = IjkPlayerFactory.create(); IjkVideoPlayer ijkVideoPlayer = (IjkVideoPlayer) playerFactory.createPlayer(this); PlayerFactory playerFactory = ExoPlayerFactory.create(); ExoMediaPlayer exoMediaPlayer = (ExoMediaPlayer) playerFactory.createPlayer(this); PlayerFactory playerFactory = MediaPlayerFactory.create(); AndroidMediaPlayer androidMediaPlayer = (AndroidMediaPlayer) playerFactory.createPlayer(this); ```- 如何配置視頻內核
//播放器配置,注意:此爲全局配置,例以下面就是配置ijk內核播放器 VideoViewManager.setConfig(VideoPlayerConfig.newBuilder() .setLogEnabled(true)//調試的時候請打開日誌,方便排錯 .setPlayerFactory(IjkPlayerFactory.create()) .build()); `
- 切換視頻內核處理代碼github
關於全屏模式相關apiapi
//進入全屏 mVideoPlayer.startFullScreen(); //退出全屏 mVideoPlayer.stopFullScreen(); ```- 關於小窗口播放相關api
//開啓小屏 mVideoPlayer.startTinyScreen(); //退出小屏 mVideoPlayer.stopTinyScreen(); `
緩存
這個分爲兩部分:第一部分是播放模式監聽,第二部分是播放狀態監聽,暴露給開發者。這裏不建議使用0,1,很是不方便簡明之意,採用註解限定。網絡
mVideoPlayer.setOnStateChangeListener(new OnVideoStateListener() { /** * 播放模式 * 普通模式,小窗口模式,正常模式三種其中一種 * MODE_NORMAL 普通模式 * MODE_FULL_SCREEN 全屏模式 * MODE_TINY_WINDOW 小屏模式 * @param playerState 播放模式 */ @Override public void onPlayerStateChanged(int playerState) { switch (playerState) { case ConstantKeys.PlayMode.MODE_NORMAL: //普通模式 break; case ConstantKeys.PlayMode.MODE_FULL_SCREEN: //全屏模式 break; case ConstantKeys.PlayMode.MODE_TINY_WINDOW: //小屏模式 break; } } /**
第一步:初始化視頻播放器,建立VideoPlayer對象架構
mVideoView = new VideoPlayer(context); mVideoView.setOnStateChangeListener(new VideoPlayer.SimpleOnStateChangeListener() { @Override public void onPlayStateChanged(int playState) { //監聽VideoViewManager釋放,重置狀態 if (playState == ConstantKeys.CurrentState.STATE_IDLE) { PlayerUtils.removeViewFormParent(mVideoView); mLastPos = mCurPos; mCurPos = -1; } } }); mController = new BasisVideoController(context); mVideoView.setController(mController); ```- 第二步:設置RecyclerView和Adapter
mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() { @Override public void onItemChildClick(int position) { //點擊item播放視頻 startPlay(position); } }); mRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() { @Override public void onChildViewAttachedToWindow(@NonNull View view) { }app
@Override
public void onChildViewDetachedFromWindow(@NonNull View view) { FrameLayout playerContainer = view.findViewById(R.id.player_container); View v = playerContainer.getChildAt(0); if (v != null && v == mVideoView && !mVideoView.isFullScreen()) { //銷燬視頻 releaseVideoView(); } } }); `
- 第三步:播放視頻和銷燬視頻的邏輯代碼框架
/** * 開始播放 * @param position 列表位置 */ protected void startPlay(int position) { if (mCurPos == position) return; if (mCurPos != -1) { releaseVideoView(); } VideoInfoBean videoBean = mVideos.get(position); mVideoView.setUrl(videoBean.getVideoUrl()); View itemView = mLinearLayoutManager.findViewByPosition(position); if (itemView == null) return; VideoRecyclerViewAdapter.VideoHolder viewHolder = (VideoRecyclerViewAdapter.VideoHolder) itemView.getTag(); //把列表中預置的PrepareView添加到控制器中,注意isPrivate此處只能爲true。 mController.addControlComponent(viewHolder.mPrepareView, true); PlayerUtils.removeViewFormParent(mVideoView); viewHolder.mPlayerContainer.addView(mVideoView, 0); //播放以前將VideoView添加到VideoViewManager以便在別的頁面也能操做它 VideoViewManager.instance().add(mVideoView, "list"); mVideoView.start(); mCurPos = position; } private void releaseVideoView() {
設置視頻播放器背景圖,和視頻標題。
//注意,下面這個controller是指BasisVideoController //設置視頻背景圖 ImageView thumb = controller.getThumb(); Glide.with(this).load(R.drawable.image_default).into(controller.getThumb()); //設置視頻標題 controller.setTitle("視頻標題"); ```- 判斷是否鎖屏
//判斷是否鎖屏 boolean locked = controller.isLocked(); //設置是否鎖屏 controller.setLocked(true); `
- 設置播放視頻縮放類型。借鑑於網絡博客,相似圖片縮放。建議選擇16:9類型,最多見
這個舉一個例子,好比同時播放兩個視頻,固然這種狀況在app中可能比較少
//必須設置 player1.setUrl(VOD_URL_1); VideoPlayerBuilder.Builder builder = VideoPlayerBuilder.newBuilder(); builder.setEnableAudioFocus(false); VideoPlayerBuilder videoPlayerBuilder = new VideoPlayerBuilder(builder); player1.setVideoBuilder(videoPlayerBuilder); BasisVideoController controller1 = new BasisVideoController(this); player1.setController(controller1); mVideoViews.add(player1); //必須設置 player2.setUrl(VOD_URL_2); VideoPlayerBuilder.Builder builder2 = VideoPlayerBuilder.newBuilder(); builder.setEnableAudioFocus(false); VideoPlayerBuilder videoPlayerBuilder2 = new VideoPlayerBuilder(builder2); player2.setVideoBuilder(videoPlayerBuilder2); BasisVideoController controller2 = new BasisVideoController(this); player2.setController(controller2); mVideoViews.add(player2); ```- 那麼要是頁面切換到後臺,如何處理多個視頻的暫停功能呢?以下所示:
@Override protected void onPause() { super.onPause(); for (VideoPlayer vv : mVideoViews) { vv.pause(); } } @Override
protected void onResume() { super.onResume(); for (VideoPlayer vv : mVideoViews) { vv.pause(); } } @Override
protected void onDestroy() { super.onDestroy(); for (VideoPlayer vv : mVideoViews) { vv.release(); } } @Override
public void onBackPressed() { for (VideoPlayer vv : mVideoViews) { if (vv.onBackPressed()) return; } super.onBackPressed(); } `
關於視頻播放相關的api以下所示
//暫停播放 mVideoPlayer.pause(); //視頻緩衝完畢,準備開始播放時回調 mVideoPlayer.onPrepared(); //從新播放 mVideoPlayer.replay(true); //繼續播放 mVideoPlayer.resume(); //調整播放進度 mVideoPlayer.seekTo(100); //循環播放, 默認不循環播放 mVideoPlayer.setLooping(true); //設置播放速度 mVideoPlayer.setSpeed(1.1f); //設置音量 0.0f-1.0f 之間 mVideoPlayer.setVolume(1,1); //開始播放 mVideoPlayer.start(); ```- 關於視頻切換播放模式相關api
//判斷是否處於全屏狀態 boolean fullScreen = mVideoPlayer.isFullScreen(); //是不是小窗口模式 boolean tinyScreen = mVideoPlayer.isTinyScreen(); //進入全屏 mVideoPlayer.startFullScreen(); //退出全屏 mVideoPlayer.stopFullScreen(); //開啓小屏 mVideoPlayer.startTinyScreen(); //退出小屏 mVideoPlayer.stopTinyScreen(); `
- 關於其餘好比獲取速度,音量,設置屬性相關Api
Controller控制器相關的Api說明
以下所示
以下所示,這個是針對ViewPager
//獲取PreloadManager預加載管理者對象 mPreloadManager = PreloadManager.getInstance(this); //在播放視頻的時候 String playUrl = mPreloadManager.getPlayUrl(url); VideoLogUtils.i("startPlay: " + "position: " + position + " url: " + playUrl); mVideoPlayer.setUrl(playUrl); //在頁面滾動的時候 mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageScrollStateChanged(int state) { super.onPageScrollStateChanged(state); if (state == VerticalViewPager.SCROLL_STATE_IDLE) { mPreloadManager.resumePreload(mCurPos, mIsReverseScroll); } else { mPreloadManager.pausePreload(mCurPos, mIsReverseScroll); } } }); ```- 以下所示,這個是針對RecyclerView
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { /* 是否反向滑動 */ private boolean mIsReverseScroll; @Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); if (dy>0){ //表示下滑 mIsReverseScroll = false; } else { //表示上滑 mIsReverseScroll = true; } } @Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == VerticalViewPager.SCROLL_STATE_IDLE) { mPreloadManager.resumePreload(mCurPos, mIsReverseScroll); } else { mPreloadManager.pausePreload(mCurPos, mIsReverseScroll); } } }); `
public class BuriedPointEventImpl implements BuriedPointEvent { /** * 進入視頻播放 * @param url 視頻url */ @Override public void playerIn(String url) { } /** * 退出視頻播放 * @param url 視頻url */ @Override public void playerDestroy(String url) { } /** * 視頻播放完成 * @param url 視頻url */ @Override public void playerCompletion(String url) { } /** * 視頻播放異常 * @param url 視頻url * @param isNetError 是不是網絡異常 */ @Override public void onError(String url, boolean isNetError) { } /** * 點擊了視頻廣告 * @param url 視頻url */ @Override public void clickAd(String url) { } /** * 退出視頻播放時候的播放進度百度分 * @param url 視頻url * @param progress 視頻進度,計算百分比【退出時候進度 / 總進度】 */ @Override public void playerOutProgress(String url, float progress) { } /** * 視頻切換音頻 * @param url 視頻url */ @Override public void videoToMedia(String url) { }}