@Override public void onHideCustomView() { // This method should be manually called on video end in all cases because it's not always // called automatically. // This method must be manually called on back key press (from this class' onBackPressed() // method). if (isVideoFullscreen) { showActionBar(); activityNonVideoView.setVideoPlaying(false); // Hide the video view, remove it, and show the non-video view ViewGroup activityVideoView = ((ViewGroup) MainActivity.webLayout.findViewById(R.id.webviewholder)); activityVideoView.removeView(videoViewContainer); activityNonVideoView.setVisibility(View.VISIBLE); // Call back (only in API level <19, because in API level 19+ with chromium webview it // crashes) if (videoViewCallback != null && !videoViewCallback.getClass().getName().contains(".chromium.")) { videoViewCallback.onCustomViewHidden(); } // Reset video related variables isVideoFullscreen = false; videoViewContainer = null; videoViewCallback = null; // Notify full-screen change if (toggledFullscreenCallback != null) { toggledFullscreenCallback.toggledFullscreen(false); } } }
@Override public void onHideCustomView() { // This method must be manually (internally) called on video end in the case of VideoView // (typically API level <11) // This method must be manually (internally) called on video end in the case of // HTML5VideoFullScreen (typically API level 11+) because it's not always called automatically // This method must be manually (internally) called on back key press (from this class' // onBackPressed() method) if (isVideoFullscreen()) { // Hide the video view, remove it, and show the non-video view activityVideoView.setVisibility(View.GONE); // 播放视频的 activityVideoView.removeView(videoViewContainer); activityNonVideoView.setVisibility(View.VISIBLE); ((Activity) activityNonVideoView.getContext()) .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 强制当前页面在手机中竖屏 // Call back if (videoViewCallback != null) videoViewCallback.onCustomViewHidden(); // Reset video related variables isVideoFullscreen = false; videoViewContainer = null; videoViewCallback = null; // Notify full-screen change if (toggledFullscreenCallback != null) { toggledFullscreenCallback.toggledFullscreen(false); } } }
@Override public void onShowCustomView(View view, CustomViewCallback callback) { super.onShowCustomView(view, callback); // Android SDK版本 if (Build.VERSION.SDK_INT >= 14) { if (view instanceof FrameLayout) { FrameLayout frameLayout = (FrameLayout) view; // A video wants to be shown View focusedChild = frameLayout.getFocusedChild(); this.isVideoFullscreen = true; this.videoViewContainer = frameLayout; this.videoViewCallback = callback; activityNonVideoView.setVisibility(View.GONE); // 隐藏掉视频所在布局中除了用来展示全屏视频以外的布局 activityVideoView.addView( videoViewContainer, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); activityVideoView.setVisibility(View.VISIBLE); // 设置全屏布局可见 ((Activity) activityVideoView.getContext()) .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // 强制当前页面在手机中横屏 if (focusedChild instanceof VideoView) { // VideoView (typically API level <11) VideoView videoView = (VideoView) focusedChild; videoView.setOnPreparedListener(this); // Handle all the required events videoView.setOnCompletionListener(this); videoView.setOnErrorListener(this); } else { // Usually android.webkit.HTML5VideoFullScreen$VideoSurfaceView, sometimes // android.webkit.HTML5VideoFullScreen$VideoTextureView // HTML5VideoFullScreen (typically API level 11+) // Handle HTML5 video ended event if (webView != null && webView.getSettings().getJavaScriptEnabled()) { // Run javascript code that detects the video end and notifies the interface String js = "javascript:"; js += "_ytrp_html5_video = document.getElementsByTagName('video')[0];"; js += "if (_ytrp_html5_video !== undefined) {"; { js += "function _ytrp_html5_video_ended() {"; { js += "_ytrp_html5_video.removeEventListener('ended', _ytrp_html5_video_ended);"; js += "_VideoEnabledWebView.notifyVideoEnd();"; // Must match Javascript interface // name and method of // VideoEnableWebView } js += "}"; js += "_ytrp_html5_video.addEventListener('ended', _ytrp_html5_video_ended);"; } js += "}"; webView.loadUrl(js); } } // Notify full-screen change if (toggledFullscreenCallback != null) { toggledFullscreenCallback.toggledFullscreen(true); } } } }
@Override public void onShowCustomView(View view, CustomViewCallback callback) { if (view instanceof FrameLayout) { hideActionBar(); // A video wants to be shown FrameLayout frameLayout = (FrameLayout) view; View focusedChild = frameLayout.getFocusedChild(); // Save video related variables activityNonVideoView.setVideoPlaying(true); this.isVideoFullscreen = true; this.videoViewContainer = frameLayout; this.videoViewCallback = callback; // Hide the non-video view, add the video view, and show it activityNonVideoView.setVisibility(View.GONE); ViewGroup activityVideoView = ((ViewGroup) MainActivity.webLayout.findViewById(R.id.webviewholder)); System.out.println("VIDV1" + activityVideoView); activityVideoView.addView( videoViewContainer, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); activityVideoView.setVisibility(View.VISIBLE); if (focusedChild instanceof android.widget.VideoView) { // android.widget.VideoView (typically API level <11) android.widget.VideoView videoView = (android.widget.VideoView) focusedChild; // Handle all the required events videoView.setOnPreparedListener(this); videoView.setOnCompletionListener(this); videoView.setOnErrorListener(this); } else { // Other classes, including: // - android.webkit.HTML5VideoFullScreen$VideoSurfaceView, which inherits from // android.view.SurfaceView (typically API level 11-18) // - android.webkit.HTML5VideoFullScreen$VideoTextureView, which inherits from // android.view.TextureView (typically API level 11-18) // - com.android.org.chromium.content.browser.ContentVideoView$VideoSurfaceView, which // inherits from android.view.SurfaceView (typically API level 19+) // Handle HTML5 video ended event only if the class is a SurfaceView // Test case: TextureView of Sony Xperia T API level 16 doesn't work fullscreen when loading // the javascript below if (webView != null && webView.getSettings().getJavaScriptEnabled() && focusedChild instanceof SurfaceView) { // Run javascript code that detects the video end and notifies the Javascript interface String js = "javascript:"; js += "var _ytrp_html5_video_last;"; js += "var _ytrp_html5_video = document.getElementsByTagName('video')[0];"; js += "if (_ytrp_html5_video != undefined && _ytrp_html5_video != _ytrp_html5_video_last) {"; { js += "_ytrp_html5_video_last = _ytrp_html5_video;"; js += "function _ytrp_html5_video_ended() {"; { js += "_VideoEnabledWebView.notifyVideoEnd();"; // Must match Javascript interface name // and method of VideoEnableWebView } js += "}"; js += "_ytrp_html5_video.addEventListener('ended', _ytrp_html5_video_ended);"; } js += "}"; webView.loadUrl(js); } } // Notify full-screen change if (toggledFullscreenCallback != null) { toggledFullscreenCallback.toggledFullscreen(true); } } }