fm.jiecao:jiecaovideoplayer:x.x.x
優化fm.jiecao.jcvideoplayer_lib中視頻橫豎屏自動適配原視頻方案:
僅優化關鍵代碼部分,源碼:
public void startWindowFullscreen() {Log.i(TAG, "startWindowFullscreen " + " [" + this.hashCode() + "] ");hideSupportActionBar(getContext());ViewGroup vp = (ViewGroup) (JCUtils.scanForActivity(getContext())).findViewById(Window.ID_ANDROID_CONTENT);View old = vp.findViewById(FULLSCREEN_ID);if (old != null) {vp.removeView(old);}if (textureViewContainer.getChildCount() > 0) {textureViewContainer.removeAllViews();}try {Constructor<JCVideoPlayer> constructor = (Constructor<JCVideoPlayer>) JCVideoPlayer.this.getClass().getConstructor(Context.class);JCVideoPlayer jcVideoPlayer = constructor.newInstance(getContext());jcVideoPlayer.setId(FULLSCREEN_ID);WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);int w = wm.getDefaultDisplay().getWidth();int h = wm.getDefaultDisplay().getHeight();FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(h, w);lp.setMargins((w - h) / 2, -(w - h) / 2, 0, 0);vp.addView(jcVideoPlayer, lp);jcVideoPlayer.setShowDownloadIcon(showDownloadIcon);if (needSumWatchVideoTimerLongFlag) {jcVideoPlayer.setUpWithWatchVideoTime(videoId, url, JCVideoPlayerStandard.SCREEN_WINDOW_FULLSCREEN, objects);} else {jcVideoPlayer.setUp(videoId, url, JCVideoPlayerStandard.SCREEN_WINDOW_FULLSCREEN, objects);}jcVideoPlayer.setUiWitStateAndScreen(currentState);jcVideoPlayer.addTextureView();jcVideoPlayer.setRotation(90);final Animation ra = AnimationUtils.loadAnimation(getContext(), R.anim.start_fullscreen);jcVideoPlayer.setAnimation(ra);JCVideoPlayerManager.setLastListener(this);JCVideoPlayerManager.setListener(jcVideoPlayer);downloadButton.setVisibility(showDownloadIcon ? VISIBLE : View.GONE);} catch (InstantiationException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}
優化
詳細方案
- 隱藏ActionBar: 在全屏模式下,通常需要隱藏ActionBar以提供更好的觀看體驗。
- 獲取屏幕尺寸: 使用WindowManager獲取當前設備的屏幕寬度和高度。
- 視頻尺寸判斷:?通過JCMediaManager獲取當前視頻的寬度和高度,并根據寬高比決定是橫屏還是豎屏。
- 布局參數設置: 根據視頻的寬高比,設置FrameLayout.LayoutParams以確保視頻在全屏模式下正確顯示。
- 旋轉設置: 如果視頻寬度大于高度,旋轉90度以橫屏顯示;否則,保持豎屏。
- 添加視圖和設置參數: 將新的JCVideoPlayer實例添加到視圖中,并設置相關參數和動畫。
- 異常處理: 捕獲并處理可能的異常,以確保程序的健壯性。
代碼:
public void startWindowFullscreen() {Log.i(TAG, "startWindowFullscreen " + " [" + this.hashCode() + "] ");// 隱藏支持的ActionBarhideSupportActionBar(getContext());// 獲取當前Activity的內容視圖ViewGroup vp = (ViewGroup) (JCUtils.scanForActivity(getContext())).findViewById(Window.ID_ANDROID_CONTENT);View old = vp.findViewById(FULLSCREEN_ID);if (old != null) {vp.removeView(old); // 移除舊的全屏視圖}if (textureViewContainer.getChildCount() > 0) {textureViewContainer.removeAllViews(); // 移除TextureView容器中的所有視圖}try {// 通過反射創建一個新的JCVideoPlayer實例Constructor<JCVideoPlayer> constructor = (Constructor<JCVideoPlayer>) JCVideoPlayer.this.getClass().getConstructor(Context.class);JCVideoPlayer jcVideoPlayer = constructor.newInstance(getContext());jcVideoPlayer.setId(FULLSCREEN_ID); // 設置全屏視圖的ID// 獲取屏幕的寬度和高度WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);int screenWidth = wm.getDefaultDisplay().getWidth();int screenHeight = wm.getDefaultDisplay().getHeight();// 獲取視頻的寬度和高度int videoWidth = JCMediaManager.instance().currentVideoWidth;int videoHeight = JCMediaManager.instance().currentVideoHeight;FrameLayout.LayoutParams lp;// 根據視頻的寬高比設置布局參數和旋轉角度if (videoWidth > videoHeight) {// 如果視頻寬度大于高度,設置為橫屏lp = new FrameLayout.LayoutParams(screenHeight, screenWidth);lp.setMargins((screenWidth - screenHeight) / 2, -(screenWidth - screenHeight) / 2, 0, 0);jcVideoPlayer.setRotation(90); // 旋轉90度} else {// 如果視頻高度大于或等于寬度,設置為豎屏lp = new FrameLayout.LayoutParams(screenWidth, screenHeight);jcVideoPlayer.setRotation(0); // 不旋轉}// 將新的JCVideoPlayer添加到視圖中vp.addView(jcVideoPlayer, lp);// 設置視頻播放器的參數jcVideoPlayer.setUp(url, JCVideoPlayerStandard.SCREEN_WINDOW_FULLSCREEN, objects);jcVideoPlayer.setUiWitStateAndScreen(currentState); // 設置UI狀態和屏幕jcVideoPlayer.addTextureView(); // 添加TextureView// 設置全屏動畫final Animation ra = AnimationUtils.loadAnimation(getContext(), R.anim.start_fullscreen);jcVideoPlayer.setAnimation(ra);// 設置視頻播放器管理器的監聽器JCVideoPlayerManager.setLastListener(this);JCVideoPlayerManager.setListener(jcVideoPlayer);} catch (InstantiationException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}
}