01. 優先介紹一下模擬器中沒有聲音的問題:

官方Demo中的內容較多,我這邊按照自己的需求,做了個小DEMO,便於后期引用學習
-----------------------------------------
02. 依賴配置
1 //視頻播放 2 implementation 'com.shuyu:GSYVideoPlayer:7.1.4' 3 //圖像加載 4 implementation 'com.github.bumptech.glide:glide:4.12.0' 5 annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
03. 主頁,做為不同視頻播放效果的導航模塊,內置多個Button, 用於演示不同效果 (實際內容查看相應的demo)

04. 常規使用模式
1 private void init() { 2 videoPlayer = (StandardGSYVideoPlayer) findViewById(R.id.video_player); 3 //視頻資源配置 4 String source1 = "http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4"; 5 videoPlayer.setUp(source1, true, "測試視頻"); 6 //增加封面 7 ImageView imageView = new ImageView(this); 8 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 9 imageView.setImageResource(R.mipmap.xxx1); 10 videoPlayer.setThumbImageView(imageView); 11 //增加title 12 videoPlayer.getTitleTextView().setVisibility(View.VISIBLE); 13 //設置返回鍵 14 videoPlayer.getBackButton().setVisibility(View.VISIBLE); 15 //設置旋轉 16 orientationUtils = new OrientationUtils(this, videoPlayer); 17 //設置全屏按鍵功能,這是使用的是選擇屏幕,而不是全屏 18 videoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() { 19 @Override 20 public void onClick(View v) { 21 //Toast.makeText(videoPlayer.getContext(),"點擊了全屏按鈕",Toast.LENGTH_SHORT).show(); 22 orientationUtils.resolveByClick(); 23 } 24 }); 25 //是否可以滑動調整 26 videoPlayer.setIsTouchWiget(true); 27 //設置返回按鍵功能 28 videoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() { 29 @Override 30 public void onClick(View v) { 31 //Toast.makeText(videoPlayer.getContext(),"點擊了返回功能按鈕",Toast.LENGTH_SHORT).show(); 32 onBackPressed(); 33 } 34 }); 35 //開始播放 36 videoPlayer.startPlayLogic(); 37 }
在APP不同狀態,同步播放器相應的狀態
1 @Override 2 protected void onPause() { 3 super.onPause(); 4 videoPlayer.onVideoPause(); 5 } 6 7 @Override 8 protected void onResume() { 9 super.onResume(); 10 videoPlayer.onVideoResume(); 11 } 12 13 @Override 14 protected void onDestroy() { 15 super.onDestroy(); 16 GSYVideoManager.releaseAllVideos(); 17 if (orientationUtils != null) 18 orientationUtils.releaseListener(); 19 } 20 21 @Override 22 public void onBackPressed() { 23 //先返回正常狀態 24 if (orientationUtils.getScreenType() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { 25 videoPlayer.getFullscreenButton().performClick(); 26 return; 27 } 28 //釋放所有 29 videoPlayer.setVideoAllCallBack(null); 30 super.onBackPressed(); 31 }
關於不同清晰度視頻的配置
1 String source1 = "http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4"; 2 String name = "普通"; 3 SwitchVideoModel switchVideoModel = new SwitchVideoModel(name, source1); 4 5 String source2 = "http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f30.mp4"; 6 String name2 = "清晰"; 7 SwitchVideoModel switchVideoModel2 = new SwitchVideoModel(name2, source2); 8 9 String source3 = "https://res.exexm.com/cw_145225549855002"; 10 String name3 = "外史"; 11 SwitchVideoModel switchVideoModel3 = new SwitchVideoModel(name3, source3); 12 13 14 List<SwitchVideoModel> list = new ArrayList<>(); 15 list.add(switchVideoModel); 16 list.add(switchVideoModel2); 17 list.add(switchVideoModel3); 18 19 binding.videoPlayer.setUp(list, true, "測試視頻");
關於廣告內容項的配置
1 ArrayList<GSYSampleADVideoPlayer.GSYADVideoModel> urls = new ArrayList<>(); 2 //廣告1 3 urls.add(new GSYSampleADVideoPlayer.GSYADVideoModel("http://7xjmzj.com1.z0.glb.clouddn.com/20171026175005_JObCxCE2.mp4", 4 "", GSYSampleADVideoPlayer.GSYADVideoModel.TYPE_AD)); 5 //正式內容1 6 urls.add(new GSYSampleADVideoPlayer.GSYADVideoModel("http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4", 7 "正文1標題", GSYSampleADVideoPlayer.GSYADVideoModel.TYPE_NORMAL)); 8 //廣告2 9 urls.add(new GSYSampleADVideoPlayer.GSYADVideoModel("http://7xjmzj.com1.z0.glb.clouddn.com/20171026175005_JObCxCE2.mp4", 10 "", GSYSampleADVideoPlayer.GSYADVideoModel.TYPE_AD, true)); 11 //正式內容2 12 urls.add(new GSYSampleADVideoPlayer.GSYADVideoModel("http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f30.mp4", 13 "正文2標題", GSYSampleADVideoPlayer.GSYADVideoModel.TYPE_NORMAL)); 14 15 binding.adPlayer.setAdUp(urls, true, 0);
播放器相關功能配置
1 binding.adPlayer.setIsTouchWiget(true); 2 binding.adPlayer.setRotateViewAuto(false);//關閉自動旋轉 3 binding.adPlayer.setLockLand(false); 4 binding.adPlayer.setShowFullAnimation(false); 5 binding.adPlayer.setNeedLockFull(true); 6 binding.adPlayer.setVideoAllCallBack(this); 7 binding.adPlayer.setLockClickListener(new LockClickListener() { 8 @Override 9 public void onClick(View view, boolean lock) { 10 if (orientationUtils != null) { 11 //配合下方的onConfigurationChanged 12 orientationUtils.setEnable(!lock); 13 } 14 } 15 });
04. 對於帶廣告播放器功能的擴展
在官方DEMO中,有帶廣告的視頻播放,但原DEMO中都是直接可以跳過去的,
結合當前在線視頻對廣告內容的跳過規則,(非會員,必須看完廣告, 或者 至少要看多少秒以后才能跳過廣告)
基於這項需求,自行擴展了一個播放器類
1 public class MVideoPlayer extends GSYSampleADVideoPlayer { 2 3 public static final int tmpCanSkip=10; //10秒后可跳出廣告 4 5 public MVideoPlayer(Context context, Boolean fullFlag) { 6 super(context, fullFlag); 7 } 8 9 public MVideoPlayer(Context context) { 10 super(context); 11 } 12 13 public MVideoPlayer(Context context, AttributeSet attrs) { 14 super(context, attrs); 15 } 16 17 @Override 18 protected void setProgressAndTime(int progress, int secProgress, int currentTime, int totalTime, boolean forceChange) { 19 super.setProgressAndTime(progress, secProgress, currentTime, totalTime, forceChange); 20 //控制 21 if (currentTime==0){ 22 mJumpAd.setVisibility(GONE); 23 } 24 if ((mJumpAd != null) && isAdModel && (currentTime>0) ) { 25 int passSeconds = currentTime/1000; 26 mJumpAd.setVisibility(passSeconds>=tmpCanSkip?VISIBLE:GONE); 27 } 28 } 29 }
--------------------------------------
沒有找着上傳附件的地方,后期引用時,可在電腦上搜索 DemoGSYVideoPlayer.zip
