Webview 和 iFrame Video 全屏问题

问题描述 投票:0回答:3

我几周来一直在寻找解决方案,同时继续将其放在我的待办事项中。

我有一个简单的网络视图如下

WebView webView = FindViewById<WebView>(Resource.Id.webviewVideo);
webView.ClearCache(true);
webView.ClearHistory();                    
webView.SetWebChromeClient( new WebChromeClient { });
webView.Settings.JavaScriptEnabled = true;
webView.Settings.LoadWithOverviewMode = true;
webView.Settings.UseWideViewPort = true;                    
webView.LoadDataWithBaseURL("https://.", iFrameString, "text/html", "UTF-8", null);

我将 iFrame 传递给它,视频加载并播放正常,但全屏选项不可用。

我尝试过的解决方案

  • 启用JavaScript

  • 设置WebChromeClient

  • 使用BaseURL加载数据

    https://

我也有

allowfullscreen
例如下面的 iframe

<iframe width="560" height="315" src="https://www.youtube.com/embed/somevideoID" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe> 

有什么办法解决这个问题吗?

android xamarin xamarin.android
3个回答
10
投票

要启用 YouTube 播放器上的全屏按钮,

WebChromeClient
必须实现
OnShowCustomView
OnHideCustomView
,因此您有责任为您的应用定义什么是“全屏”,因为它不必是由设备的屏幕尺寸定义。

注意:您的 iFrame html 源代码中仍然需要

allowfullscreen
的 HTML5 标签

所以我们假设您有这种类型的布局:

LinearLayout (id = linearLayout)
   LinearLayout (id = contentLayout)
      Button
      WebView

您可以子类

WebChromeClient
并定义您希望如何显示“全屏”内容,在本示例中,我们假设最外面的
LinearLayout
是我们要显示 YouTube 视频的位置,内部
LinearLayout
包含所有内容全屏视频播放时您希望隐藏的活动内容。

WebChromeClient 实现:

public class FullScreenClient : WebChromeClient
{
    readonly FrameLayout.LayoutParams matchParentLayout = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent,
                                                                                                     ViewGroup.LayoutParams.MatchParent);
    readonly ViewGroup content;
    readonly ViewGroup parent;
    View customView;

    public FullScreenClient(ViewGroup parent, ViewGroup content)
    {
        this.parent = parent;
        this.content = content;
    }

    public override void OnShowCustomView(View view, ICustomViewCallback callback)
    {
        customView = view;
        view.LayoutParameters = matchParentLayout;
        parent.AddView(view);
        content.Visibility = ViewStates.Gone;
    }

    public override void OnHideCustomView()
    {
        content.Visibility = ViewStates.Visible;
        parent.RemoveView(customView);
        customView = null;
    }
}

SetWebChromeClient 实现:

webView.SetWebChromeClient(new FullScreenClient(linearLayout, contentLayout));

4
投票

我知道答案已经被接受,但我花了一些时间来完成所提供的代码。我从来没有见过“readonly”关键字,“override”并不完全在它应该在的地方,看起来他是在回答LinearLayout并提供FrameLayout.LayoutParams。我希望这只是很好的伪代码哈哈。如果没有,请告诉我代码语法!

我在一个非常有用的YouTube 视频的评论部分发布了这个链接。如果您来自那里,则需要向 VideoAdapter 类构造函数添加 2 个参数。一种用于 LinearLayout(父级),另一种用于 RecyclerView(内容)。

感谢@SushiHangover 提供代码。您将在这方面帮助很多人!别忘了给他的答案点赞哦


我的父布局是 LinearLayout (父),子布局是 RecyclerView (内容),可以容纳多个视频。

// Custom Web View Class to allow for full screen
private class CustomWebView extends WebChromeClient {

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                                                           ViewGroup.LayoutParams.MATCH_PARENT);

    ViewGroup parent;
    ViewGroup content;
    View customView;

    public CustomWebView(ViewGroup parent, ViewGroup content){
        this.parent = parent;
        this.content = content;
    }

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        super.onShowCustomView(view, callback);

        customView = view;
        view.setLayoutParams(layoutParams);
        parent.addView(view);
        content.setVisibility(View.GONE);
    }

    @Override
    public void onHideCustomView() {
        super.onHideCustomView();

        content.setVisibility(View.VISIBLE);
        parent.removeView(customView);
        customView = null;
    }

}

0
投票

webView.webChromeClient = 对象:WebChromeClient() {

        private var customViewCallback: CustomViewCallback? = null
        var layoutParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )

        var parent: ViewGroup? = findViewById(android.R.id.content)
        var content: ViewGroup? = null
        var customView: View? = null
        private var originalOrientation = 0

        

        override fun onShowCustomView(view: View?, callback: CustomViewCallback?) {
            super.onShowCustomView(view, callback)
            [email protected] {
                clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
                addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)


                if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
                    val v: View = getWindow().getDecorView()
                    v.systemUiVisibility = View.GONE
                } else if (Build.VERSION.SDK_INT >= 19) {
                    //for new api versions.
                    val decorView = window.decorView
                    val uiOptions =
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    decorView.systemUiVisibility = uiOptions
                }


                statusBarColor = Color.TRANSPARENT
            }
            originalOrientation = [email protected];
            customView = view;
            view?.setLayoutParams(layoutParams);
            parent?.addView(view);
            content?.setVisibility(View.GONE);
          
        }

        override fun onHideCustomView() {
            super.onHideCustomView()
            content?.visibility = View.VISIBLE;
            parent?.removeView(customView);
            customView = null;
           
            [email protected] = View.SYSTEM_UI_FLAG_VISIBLE;
            [email protected] = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
           
        }

        
    }
© www.soinside.com 2019 - 2024. All rights reserved.