在仅限 iPhone 的应用程序中播放 YouTube 视频 - 失去控制 [英] Playing YouTube video in iPhone only app - loss of controls

查看:20
本文介绍了在仅限 iPhone 的应用程序中播放 YouTube 视频 - 失去控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码用于在 View 上放置一个小的 WebView,以便用户可以点击它,视频以全屏模式打开并播放.一切正常,但在播放 4 秒后,控件消失并且不会重新出现(点击、旋转......).视频播放完毕后,控件会重新出现并且完成"按钮可用.但是,一旦 WebView 被处理掉并加载了一个新视图,该新视图最多有 6 分钟没有响应.

The code below is used to put a small WebView on a View so that the user can tap it and the video opens in full screen mode and plays. All that works, but after 4 seconds of play the controls disappear and will not reappear (tapping, rotating...). Once the video finishes, the controls reappear and the 'Done' button becomes available. However once the WebView is disposed of and a new view loaded, that new view is unresponsive for up to 6 minutes.

[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 的处理方式如下:

Here is how the WebView is disposed of:

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

谢谢,瑞克

推荐答案

https://github.com/nishanil/YouTubePlayeriOS希望这个样本对你有帮助.对我来说效果很好.

https://github.com/nishanil/YouTubePlayeriOS Hope this sample helps you. It worked well for me.

这篇关于在仅限 iPhone 的应用程序中播放 YouTube 视频 - 失去控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆