WebView视频全屏播放

QuibblerQuibbler 2024-10-05 约 13 分钟 1660 次阅读

WebView视频全屏播放

在StackoverFlow上就看到一篇实现WebView全屏播放Youtube网页视频的方法,这个方法很不错。

废话不多说,直接看操作:继承WebChromeClient,重写WebChromeClient中的onShowCustomView(View , CustomViewCallback)和onHideCustomView()方法来实现网页视频全屏播放功能:

val chromeClient = object : WebChromeClient() {

    override fun onShowCustomView(view: View?, callback: CustomViewCallback?) {
        super.onShowCustomView(view, callback)
    }
    
    override fun onHideCustomView() {
        super.onHideCustomView()
    }
    
    //该方法已废弃
    @Deprecated("Deprecated in Java")
    override fun onShowCustomView(view: View?, requestedOrientation: Int, callback: CustomViewCallback?) {
        super.onShowCustomView(view, requestedOrientation, callback)
    }
    
}

首先看onShowCustomView(View view, CustomViewCallback callback)方法

/**
     * Notify the host application that the current page has entered full screen mode. After this
     * call, web content will no longer be rendered in the WebView, but will instead be rendered
     * in {@code view}. The host application should add this View to a Window which is configured
     * with {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN} flag in order to
     * actually display this web content full screen.
     *
     * <p>The application may explicitly exit fullscreen mode by invoking {@code callback} (ex. when
     * the user presses the back button). However, this is generally not necessary as the web page
     * will often show its own UI to close out of fullscreen. Regardless of how the WebView exits
     * fullscreen mode, WebView will invoke {@link #onHideCustomView()}, signaling for the
     * application to remove the custom View.
     *
     * <p>If this method is not overridden, WebView will report to the web page it does not support
     * fullscreen mode and will not honor the web page's request to run in fullscreen mode.
     *
     * <p class="note"><b>Note:</b> if overriding this method, the application must also override
     * {@link #onHideCustomView()}.
     *
     * @param view is the View object to be shown.
     * @param callback invoke this callback to request the page to exit
     * full screen mode.
     */
    public void onShowCustomView(View view, CustomViewCallback callback) {};

该方法传入一个View和CustomViewCallback回调,其中view就是包含全屏播放视频WV.vo的FrameLayout,callback则需要在退出全屏播放时调用。这两个我们都需要保存到变量中:

val chromeClient = object : WebChromeClient() {
        private var customView: View? = null
        private var customViewCallback: CustomViewCallback? = null
        ...
    }

接下来完善一些进入全屏播放的逻辑:

override fun onShowCustomView(view: View?, callback: CustomViewCallback?) {
        //自定义view不空,说明已经进入全屏,这时候再点击就推出全屏播放
        if (customView != null) {
            onHideCustomView()
            return
        }
        
        //保存这两个变量
        customView = view
        customViewCallback = callback
        
        //将全屏播放视频的view添加到我们当前的根布局中,注意必须用MATCH_PARENT
        val layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        (binding.root as ViewGroup).addView(customView, 0, layoutParams) //必要时添加到第一个位置
       
        //切换横屏,体验更好
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
    }

其次WebView退出全屏视频播放会回调onHideCustomView()方法:

/**
     * Notify the host application that the current page has exited full screen mode. The host
     * application must hide the custom View (the View which was previously passed to {@link
     * #onShowCustomView(View, CustomViewCallback) onShowCustomView()}). After this call, web
     * content will render in the original WebView again.
     *
     * <p class="note"><b>Note:</b> if overriding this method, the application must also override
     * {@link #onShowCustomView(View, CustomViewCallback) onShowCustomView()}.
     */
    public void onHideCustomView() {}

在该回调方法中需要移除全屏播放的View,并且释放之前保存的变量:

override fun onHideCustomView() {
        //从根布局中移除全屏播放的view
        (binding.root as ViewGroup).removeView(customView)
        //还记得前面保存的customViewCallback,这时候需要回调通知WebView已经推出全屏了
        customViewCallback?.onCustomViewHidden() 
      
        //移除保存的变量
        customView = null
        customViewCallback = null
         
        //根据业务需要重新恢复屏幕方向
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
    }

为了更好的用户体验,全屏播放一般都是横屏,所以在两个回调方法中我们都调用了setRequestedOrientation( int requestedOrientation)方法,该方法会导致配置变化,因此还需要在manifest.xml中给当前Activity加上configChanges:

<activity
	android:name=".view.ui.webview.FullVideoWebViewActivity"
	android:configChanges="orientation|screenSize|screenLayout"
	android:exported="true">

并且重写Activity中的onConfigurationChanged(newConfig: Configuration)方法:

override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
    }

基本上用不到的知识储备又增加了。

相关推荐

精选
ViewPager和PagerAdapter、FragmentPagerAdapter、FragmentStatePager
Android

ViewPager和PagerAdapter、FragmentPagerAdapter、FragmentStatePager

ViewPager1、ViewPagerandroidx.viewpager.widget.ViewPager,Android中使用非常广泛的控件,可以说是APP必备:首次打开引导页、页面Banner广告等。常用方法:setAdapter() 设置适配器setOffscreenPageLimit() 设置缓存的页面个数,默认是 1setCurrentItem() 跳转到特定的页面setOnPage

1.1k
获取Android内置WebView内核版本
Android

获取Android内置WebView内核版本

获取Android内置WebView内核版本竟然能遇到这样奇葩的事情,网页用的技术过于新颖,以至于只支持高版本Chromium内核的,低版本安卓系统中内置的内核版本较低,无法加载前端页面。1、设置查看在系统设置里 > 应用 > 应用管理 > 显示系统应用,查看WebView组件:2、页面查看通过WebView发起的网络请求,都会带上浏览器的UA,通常页面都可以通过UA判断浏览器的内核版本。这里有两

1.1w
Android 14适配总结
Android

Android 14适配总结

Android 14适配总结毕业工作至今已经适配了三个Android大版本,从Android 11到Android 12、再到Android 13。2023年,Google即将推出的Android 14,上半年已经开始第一批适配。现在,第四个Android版本已经适配完,总结记录一下。1、Android 14计划Google一般会在2月份对外发布预告,同时放出开发者预览版。“拉通”各大平台、厂商以

1.1w