将来自youtube.com的视频嵌入到iphone应用中

Nik*_*nko 6 youtube iphone

我正在尝试将YouTube视频嵌入到我的iphone应用程序中.我正在使用UIWebView并从youtube加载嵌入代码作为html字符串.所以我有一个基本的html标记布局,我正在放置这个代码.

<embed id="yt" src="http://www.youtube.com/watch?v=L9szn1QQfas&fs=0" type="application/x-shockwave-flash" width="%width%" height="%height%"></embed>
Run Code Online (Sandbox Code Playgroud)

问题是视频始终以全屏模式打开.我已将我的webview属性allowInlineMediaPlayback更改为YES,_webview.allowsInlineMediaPlayback = YES;但它也不起作用.有没有办法在没有全屏的情况下从youtube播放视频?

我也试着像这样嵌入

<iframe title="YouTube video player" id="videoframe" width="%width%" height="%height%" src="http://www.youtube.com/embed/L9szn1QQfas?rel=0" frameborder="0"></iframe>
Run Code Online (Sandbox Code Playgroud)

还有这个

<object type="application/x-shockwave-flash" data="http://www.youtube.com/v/L9szn1QQfas" width="%width%" height="%height%">
<param name="movie" value="http://www.youtube.com/v/L9szn1QQfas" />
<param name="quality" value="high" />
<param name="allowFullScreen" value="false" />
<embed id="yt" src="http://www.youtube.com/v/L9szn1QQfas" type="application/x-shockwave-flash" width="%width%" height="%height%"></embed>
</object>
Run Code Online (Sandbox Code Playgroud)

谢谢.

Jas*_*ien 10

据我所知,内联媒体播放仅支持iPad,而不支持iPhone.这可能是由于屏幕的尺寸限制.

编辑:

我用一个UIWebView和代码设置了一个测试项目:

[webView setAllowsInlineMediaPlayback:YES];
[webView loadHTMLString:@"<embed id=\"yt\" src=\"http://www.youtube.com/watch?v=L9szn1QQfas&fs=0\" type=\"application/x-shockwave-flash\" width=\"300\" height=\"300\"></embed>"
                    baseURL:nil];
Run Code Online (Sandbox Code Playgroud)

我在iPhone和iPad上都运行了相同的代码,两者都运行iOS 4.2.1.

结果是iPhone只能以全屏模式播放视频,无论将内嵌媒体播放设置为何,YESiPad 都会内嵌播放视频.这是一张图片:

在此输入图像描述

  • 是的,你是对的 - 这就是我错的原因.然而,我确实记得有人说当iPad推出时,只能在iPad上支持内联播放...随着iPhone 4,视网膜显示器和iOS 4的发布,这可能会有所改变.我会鞭打一个测试项目看. (2认同)
  • YouTube不允许您访问他们的`<video>`标签,因此无法为其添加`webkit-playsinline`属性.如果需要内联播放,请提供您自己的`<video>`标签. (2认同)
  • Youtube最近更新了他们的嵌入式标签以支持webkit-playsinline属性,请参阅iForests的回答. (2认同)

iFo*_*sts 10

现在有一个未记录的参数"playinline".我在iPhone 4S(iOS 6.1.2)上测试过它.

player = new YT.Player('player', {
    height: '390',
    width: '640',
    videoId: 'u1zgFlCw8Aw',
    playerVars: {
        'playsinline': 1
    }
});
Run Code Online (Sandbox Code Playgroud)

你也应该设置webview.

[webView setAllowsInlineMediaPlayback:YES];
Run Code Online (Sandbox Code Playgroud)


Sha*_*lam 7

对于iOS 5或新应用程序使用官方语法,请参阅下面的链接它非常好

http://apiblog.youtube.com/2010/07/new-way-to-embed-youtube-videos.html


Kes*_*ava 5

如果有人仍然面临这个问题,下面是我见过的最好的解决方案.奇迹般有效

        self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10,300, 200)];
        [self.webView setAllowsInlineMediaPlayback:YES];
        [self.webView setMediaPlaybackRequiresUserAction:NO];

        [self.view addSubview:self.webView];

        NSString* embedHTML = [NSString stringWithFormat:@"\
                               <html>\
                                    <body style='margin:0px;padding:0px;'>\
                                        <script type='text/javascript' src='http://www.youtube.com/iframe_api'></script>\
                                        <script type='text/javascript'>\
                                            function onYouTubeIframeAPIReady()\
                                            {\
                                                ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})\
                                            }\
                                            function onPlayerReady(a)\
                                            { \
                                                a.target.playVideo(); \
                                            }\
                                        </script>\
                                        <iframe id='playerId' type='text/html' width='%d' height='%d' src='http://www.youtube.com/embed/%@?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'>\
                                    </body>\
                               </html>", 300, 200, @"JW5meKfy3fY"];
        [self.webView loadHTMLString:embedHTML baseURL:[[NSBundle mainBundle] resourceURL]];
Run Code Online (Sandbox Code Playgroud)

来源:https://code.google.com/p/gdata-issues/issues/detail?id = 5204