如何让YouTube自动播放在UIWebView中工作? [英] How to get YouTube autoplay to work in UIWebView?

查看:110
本文介绍了如何让YouTube自动播放在UIWebView中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

平台:iOS 6.1。 Xcode 4.6。

Platform: iOS 6.1. Xcode 4.6.

我正在使用YouTube Javascript API和UIWebView。我可以手动点击视频缩略图,开始播放就好了。但是,自动播放不起作用。我正在为许多人建议的Web视图设置mediaPlaybackRequiresUserAction = NO。没有运气。

I am using YouTube Javascript API and a UIWebView. I am able to manually tap on the video thumbnail to begin playback just fine. But, autoplay is not working. I am setting mediaPlaybackRequiresUserAction = NO for the web view as many have suggested. No luck.

以下是代码:

@interface VideoPlayerController : UIViewController <UIWebViewDelegate>
    @property(strong, nonatomic) IBOutlet UIWebView* webView;
@end

- (void) viewDidLoad
{
    [super viewDidLoad];    

    self.webView.delegate = self;

    NSString *template = [NSString stringWithContentsOfFile:
                       [[NSBundle mainBundle]
                        pathForResource:@"YouTubeTemplate" ofType:@"txt"]];
    NSString *htmlStr = [NSString stringWithFormat: template,
        200, 200,
        @"3G4exdlsRkY"];
    self.webView.mediaPlaybackRequiresUserAction = NO;
    [self.webView loadHTMLString:htmlStr baseURL:nil];
}

- (void) webViewDidFinishLoad:(UIWebView *)wv {
    self.webView.mediaPlaybackRequiresUserAction = NO;
}

YouTubeTemplate.txt文件是:

The YouTubeTemplate.txt file is:

<html>
<head>
<script src="https://www.youtube.com/player_api"></script>
<style>
body, div {
    margin: 0px;
    padding: 0px;
}
</style>
</head>
<body>
  <div id="media_area"></div>
</body>
<script>
var ytPlayer = null;

function onYouTubePlayerAPIReady() {
    ytPlayer = new YT.Player('media_area', {height: '%d', width: '%d', videoId: '%@',
        events: {'onReady': onPlayerReady}
    });
}

function onPlayerReady(e) {
    e.target.playVideo();
}
</script>
</html>

因此,基本上,UIWebView会显示视频缩略图。然后我必须点击它开始播放。自动播放无效。知道我可能做错了吗?

So, basically, the UIWebView shows the video thumbnail. Then I have to tap on it to begin playback. Autoplay is not working. Any idea what I might be doing wrong?

推荐答案

是的,您可以通过将mediaPlaybackRequiresUserAction设置为NO来使其自动播放。这是一个基于您的HTML文件的项目,它会在应用启动后自动播放。

Yes, you can make it autoplay by setting mediaPlaybackRequiresUserAction to NO. This is a project based on your HTML file, and it does autoplay after the app launch.

https://dl.dropbox.com/u/17350105/YoutubeAutoPlay.zip

修改

作为评论中提到的@RajV,您应该使用 loadRequest:而不是 loadHTMLString:由于某些原因我无法解释。下面是一个工作示例:

Edit
As @RajV mentioned in the comment, you should use loadRequest: instead of loadHTMLString: for some reason I can't explain. Here is a working example:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
    [webView setMediaPlaybackRequiresUserAction:NO];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"YT_Player" ofType:@"html"] isDirectory:NO]]];
    [self.view addSubview:webView];
}

顺便说一句,如果你想在线播放youtube视频,你可以关注我几天前发布的这个答案。 (哦,我这些天在Youtube iFrame API上花了这么多时间......)

By the way, if you want to play youtube video inline, you can follow this answer I posted few days ago. (Oh, I just spent so many hours on Youtube iFrame API these days...)

这篇关于如何让YouTube自动播放在UIWebView中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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