YouTube嵌入UIWebViews不再适用于iOS

YouTube嵌入UIWebViews不再适用于iOS

本文介绍了YouTube嵌入UIWebViews不再适用于iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段时间的应用程序使用 UIWebView 来显示某些视图上的YouTube视频。今天,在没有对代码进行任何更改的情况下,我的用户开始抱怨YouTube嵌入不起作用:嵌入静止帧和播放按钮显示正常,但是点击播放按钮时嵌入屏幕变黑,没有任何加载,没有声音或者视频。

I've had an app out for a while which uses UIWebViews to display YouTube videos on certain views. Today, without any changes to the code, my users started complaining about the YouTube embeds not working: the embed with the still frame and play button displays fine, but upon clicking the play button the embed screen just goes black and nothing loads, no sound or video.

这是我的功能,用于生成嵌入代码以从YouTube ID放入 UIWebView

Here is my function for generating an embed code to put into the UIWebView from a YouTube ID:

+ (NSString*)codeForYouTubeID:(NSString*)videoID width:(NSUInteger)width height:(NSUInteger)height
{
    static NSString *fmt =
    @"<iframe width='%d' height='%d' src='http://www.youtube.com/embed/%@?showinfo=0&modestbranding=1&rel=0&showsearch=0' frameborder='0' scrolling='0' allowfullscreen></iframe>";

    return [NSString stringWithFormat:fmt, width, height, videoID];
}

将结果代码放入嵌入显示并播放正常。我还使用了以下替代格式字符串,结果相同(来自StackOverflow答案):

Putting the resulting code into JSFiddle the embed displays and plays fine. I have also used the following alternative format string with the same results (from a StackOverflow answer here):

static NSString *fmt =
@"<object>\
    <param name=\"movie\" value=\"http://www.youtube.com/v/%@\"></param>\
    <embed src=\"http://www.youtube.com/v/%@\" type=\"application/x-shockwave-flash\"></embed>\
</object>";

这具有相同的结果,但具有笨拙的自动调整大小的嵌入。行为是相同的:用户可以单击播放按钮,然后嵌入框架变黑,没有其他任何事情发生。对我来说,这发生在我的测试手机(iPhone 4,iOS 7)和模拟器(iOS6和7)上。这在今天之前没有发生,并且没有代码被更改。最近有没有人经历过这个?

This has the same results, but with an awkwardly auto-sized embed. The behaviour is the same: the user can click the play button, then the embed frame just goes black and nothing else happens. For me, this is happening on my test phone (iPhone 4, iOS 7) and on the simulator (both iOS6 and 7). This didn't happen before today, and no code has been changed. Has anyone else experienced this recently?

推荐答案

有人找到了解决方法,在某种程度上。不知道如何标记这样的答案,但为了方便,我会将其复制到这里。

Someone found a workaround here, to an extent. Not sure how to mark an answer like that, but I'll copy it over to here for convenience.

基本上问题似乎与实施的变化有关YouTube上的 playsinline 参数。无论您提供什么价值,或者您是否使用默认值,都无关紧要,但它似乎打破了以这种方式嵌入的每个 Youtube视频。

Essentially the issue seems to relate to a change in implementation of the playsinline parameter on YouTube's side. It doesn't matter what value you provide, or whether you use the default, but it seems to break every Youtube video embedded in this way.

解决方案似乎是在调用 [myWebView loadHTMLString:string] 之前添加以下行:

The solution seems to be adding the following line before you call [myWebView loadHTMLString:string]:

[myWebView setMediaPlaybackRequiresUserAction:NO];

我不知道为什么要修复它,但非常感谢Kyokook Hwang如此迅速地提供答案。它对我有用,它似乎也适用于其他许多人。

I have no idea why this fixes it, but many thanks to Kyokook Hwang for providing that answer so quickly. It worked for me, and it seemed to work for many others too.

这篇关于YouTube嵌入UIWebViews不再适用于iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 01:05