YouTube 嵌入 UIWebViews 不再适用于 iOS [英] YouTube embeds in UIWebViews no longer working in iOS

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

问题描述

我推出了一个应用程序,它使用 UIWebViews 在某些视图上显示 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];
}

将生成的代码放入 JSFiddle 嵌入显示和播放正常.我还使用了以下具有相同结果的替代格式字符串(来自 StackOverflow answer 此处):

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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