xamarin Webview和iFrame视频全屏问题

0s7z1bwu  于 2023-08-01  发布在  其他
关注(0)|答案(3)|浏览(168)

我一直在寻找一个解决这个问题的几个星期,同时不断把它放在我的积压。
我有一个简单的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
  • 使用https://的LoadDataWithBaseURL

我也有allowfullscreen例如下面的iframe

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


有什么解决办法吗?

js4nwp54

js4nwp541#

要在YouTube播放器上启用全屏按钮,WebChromeClient必须实现OnShowCustomViewOnHideCustomView,因此您有责任为您的应用定义什么是“全屏”,因为它不必由设备的屏幕大小定义。
注意:您仍然需要在iFrame html源代码中使用allowfullscreen的HTML5标记
假设你有这样的布局:

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

字符串
您可以子类化WebChromeClient并定义您希望如何显示“全屏”内容,在此示例中,我们将假设最外部的LinearLayout是我们希望显示YouTube视频的位置,内部的LinearLayout包含您希望在全屏视频播放时隐藏的所有Activity内容。

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));

3duebb1j

3duebb1j2#

我知道答案已经被接受了,但我花了一些时间来完成所提供的代码。我从来没有见过“readonly”关键字,“override”不太应该在那里,看起来他回答了LinearLayout并提供了FrameLayout. LayoutParams。我希望这只是真的很好的psudocode笑。如果没有,请告诉我代码语法!
我在一个非常有用的YouTube video的评论部分发布了这个链接。如果你是从那里来的,你需要向VideoAdapter类构造函数添加2个参数。一个用于LinearLayout(parent),一个用于RecyclerView(content)。
感谢@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;
    }

}

字符串

9q78igpj

9q78igpj3#

webView.webChromeClient = object:public void run(){

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)
            this@MainActivity.window.apply {
                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 = this@MainActivity.requestedOrientation;
            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;
           
            this@MainActivity.window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE;
            this@MainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
           
        }

        
    }

字符串

相关问题