【问题标题】:iOS6 play videos with webView (works in iOS5)iOS6 使用 webView 播放视频(适用于 iOS5)
【发布时间】:2012-09-21 22:40:05
【问题描述】:

我曾经使用带有以下代码的 uiwebviews 在我的 iOS 应用程序中播放视频:

NSString *embedHTML = @"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";

NSString *html = [NSString stringWithFormat:embedHTML, urlToOpen, frame.size.width, frame.size.height];
self.videoView = [[UIWebView alloc] initWithFrame:frame];
[videoView loadHTMLString:html baseURL:nil];
[self.videoView setDelegate:self];
[self.view addSubview:videoView];
[videoView release];

iOS6 就不行了。

有没有人找到任何解决方案来播放用 iOS6 编译的 youtube 视频?

谢谢

【问题讨论】:

    标签: youtube ios6


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      我刚刚用 iOS6 模拟器和 iOS5 设备进行了测试,如果你使用新的嵌入代码 (iframe) 就可以了。

      <iframe class="youtube-player" type="text/html" width="320" height="200" src="http://www.youtube.com/embed/VIDEOID" frameborder="0"></iframe>
      

      唯一的缺点是 iframe 包含大量下载的 dom 元素。

      【讨论】:

        【解决方案3】:

        用下面的代码替换你的代码

           NSString *embedHTML = @"\
            <html><head>\
            <style type=\"text/css\">\
            body {\
            background-color: transparent;\
            color: white;\
            }\
            </style>\
            </head><body style=\"margin:0\">\
            <iframe title=\"YouTube Video\" class=\"youtube-player\" type=\"text/html\"\
            width=\"%0.0f\" height=\"%0.0f\" src=\"%@\"\
            frameborder=\"0\" allowFullScreen ></iframe>";
            NSString *urlToOpen = @"http://www.youtube.com/embed/u1zgFlCw8Aw?autoplay=1";
            NSString *html = [NSString stringWithFormat:embedHTML, urlToOpen, frame.size.width,     frame.size.height];
            self.videoView = [[UIWebView alloc] initWithFrame:frame];
            [videoView loadHTMLString:html baseURL:nil];
            [self.videoView setDelegate:self];
            [self.view addSubview:videoView];
            [videoView release];
        

        由于 Youtube 已经放弃了 webview 视频并引入了 iframe,所以你必须在 ios 6 上使用这个 html,而在 ios 5 上使用旧的。 请记住,您的 youtube 视频 url 链接应该像 http://www.youtube.com/embed/video_Id?autoplay=1 您可以将 video_Id 替换为您自己的视频 ID。

        【讨论】:

          【解决方案4】:

          如果您使用 Phonegap 中的嵌入,您已在 Phonegap 设置中将 OpenAllWhitelistURLsInWebView 设置为 YES

          【讨论】:

            【解决方案5】:

            在 iOS6 中带有 Webviews 的 Youtube 需要在 WebView 中启用 cookie。为此,您需要在 AppDelegate 中添加以下指令:

            - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
            {
                  [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
                  ...
            }
            

            【讨论】:

              【解决方案6】:

              我在模拟器中试过这个

              [_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.youtube.com/embed/b22cH2sfVcc"]]];

              它的工作原理

              直接将youtube视频的embed src提供给web view就可以了。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2012-05-24
                • 1970-01-01
                • 2013-10-02
                • 1970-01-01
                • 2012-09-09
                • 1970-01-01
                • 2021-11-27
                相关资源
                最近更新 更多