在仅限iPhone应用程序中播放YouTube视频-失去控件

3
下面的代码用于在视图上放置一个小型 WebView,以便用户可以点击它并在全屏模式下播放视频。所有这些都有效,但是播放 4 秒后,控件就会消失,无法重新出现(点击、旋转...)。一旦视频播放结束,控件就会重新出现,并且“完成”按钮变为可用。然而,一旦 WebView 被处理并加载了新视图,那个新视图会在最多 6 分钟内无响应。
[Preserve (AllMembers=true)]
public class YouTubeViewer :  UIWebView
{
    public static AppDelegate appDelegate = (AppDelegate) UIApplication.SharedApplication.Delegate;

    public YouTubeViewer(string url, RectangleF frame)
    {
        Log.WriteLog("loading YouTubeView");
        appDelegate.firstViewing = true;
        this.UserInteractionEnabled = true;
        this.BackgroundColor = UIColor.Clear;
        this.Frame = frame;
        string youTubeVideoHTML = @"<object width=""{1}"" height=""{2}""><param name=""movie""
                            value=""{0}""></param><embed
                            src=""{0}"" type=""application/x-shockwave-flash"" 
                            width=""{1}"" height=""{2}""</embed></object>"; 

        string html = string.Format(youTubeVideoHTML, url, frame.Size.Width, frame.Size.Height);
        this.LoadHtmlString(html, null);

    }
}

以下是WebView的销毁方式:

public void RemoveWebView(UIWebView inView)
    {
        try
        {
            Log.WriteLog("RemoveWebView");
            NSUrlCache.SharedCache.RemoveAllCachedResponses();
            NSUrlCache.SharedCache.DiskCapacity = 0;
            NSUrlCache.SharedCache.MemoryCapacity = 0;

            inView.LoadHtmlString("",null);
            inView.EvaluateJavascript("var body=document.getElementsByTagName('body')[0];body.style.backgroundColor=(body.style.backgroundColor=='')?'white':'';");
            inView.EvaluateJavascript("document.open();document.close()");
            inView.StopLoading();
            inView.Delegate = null;
            inView.RemoveFromSuperview();
            inView.Dispose();
        }
        catch(Exception ex)
        {
            Log.LogError("RemoveWebView",ex);
        }
    }

谢谢,Rick。

Rick,你解决了这个问题吗? - cesarislaw
Xamarin已将此记录为错误 - 我还没有听到任何解决方案。 - Rick
Xamarin确认问题是他们自己的,而不是苹果公司的。他们将我的测试应用程序转换为Obj-C,问题消失了。然后他们追踪到了GetSupportedInterfaceOrientations覆盖的问题。如果将其移除,问题就会消失。只希望我可以没有它。 - Rick
2个回答

0

我和 Xamarin 进行了交流,他们建议在 AppDelegate 中删除方向管理的覆盖。

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, UIWindow forWindow)
    {  /*... code ...*/  }

在我移除了这个覆盖之后,我的应用程序在加载YouTube视频时按预期工作。 这为我解决了问题。您仍然可以通过单个ViewController覆盖和全局Info.plist文件来控制支持的方向。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接