Webview和iFrame视频全屏问题

时间:2018-04-30 13:47:41

标签: android xamarin xamarin.android

我一直在寻找解决方案几周,同时继续把它放在我的积压工作上。

我有一个简单的webview,如下所示

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

  • LoadDataWithBaseURL与https://

我还有allowfullscreen,例如以下iframe

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

对此有何解决方案?

2 个答案:

答案 0 :(得分:5)

要启用YouTube播放器上的全屏按钮,WebChromeClient必须实施OnShowCustomViewOnHideCustomView,因此您有责任为您的“全屏”定义应用程序,因为它不必由设备的屏幕大小定义。

注意:您的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));

答案 1 :(得分:2)

我知道答案已经被接受,但是我花了一些时间来完成提供的代码。我从未见过“ readonly”关键字,“ override”并不是应该的位置,看起来他在为LinearLayout回答并提供了FrameLayout.LayoutParams。我希望这真的是很好的psudocode大声笑。如果没有,请让我知道代码语法!

我将此链接发布在非常有用的YouTube video的评论部分中。如果您来自那里,则需要向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;
    }

}