如何在UIWebView中自动播放YouTube视频 [英] How to autoplay a YouTube video in a UIWebView

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

问题描述

我在这里看到很多关于这个问题的帖子,但仍然无法找到这个问题的完美答案。

I have seen a lot of posts in here about this issue, but still couldn't find a perfect answer for this problem.

所以我有一个tableview,每个单元格里面都有一个播放按钮。当用户点击播放按钮时,我会向此单元格添加 UIWebView ,并播放YouTube视频。

So I have a tableview, and each cell has a play button inside it. When the user tap the play button, I add a UIWebView to this cell, and play a YouTube video.

static NSString *youTubeVideoHTML = @"<html>\
    <body style=\"margin:0;\">\
        <iframe class=\"youtube-player\" type=\"text/html\" width=\"%0.0f\" height=\"%0.0f\" src=\"http://www.youtube.com/embed/%@\" frameborder=\"0\">\
        </iframe>\
    </body>\
    </html>";


- (void)playVideoWithId:(NSString *)videoId {
    NSString *html = [NSString stringWithFormat:youTubeVideoHTML, self.frame.size.width, self.frame.size.height, videoId];

    [self loadHTMLString:html baseURL:nil];
}

问题:

此代码实际上并不像我想要的那样播放视频,只是启动YouTube播放器并使用YouTube红色播放按钮进行显示。只有当用户点击红色按钮时,视频才会开始播放。

因此,用户必须点击两个按钮,直到视频开始 - 不是最好的用户体验...

This code doesn't actually play the video like I want, it just initiate the YouTube player and show it with the YouTube red play button. Only when user tap the red button, the video will start playing.
So user has to tap two buttons until the video starts - not the best user experience...

就像我说的那样,我看到很多关于这个问题的帖子,有些根本不起作用,有些工作但有一些问题让我感到困扰。

Like I said I saw many posts about this issue, some not work at all, and some works but with some issues that bugs me.

我发现的其中一个有用的解决方案是这篇文章,他展示了如何让这个从文件中加载HTML(而不是像我这样的字符串),这个approche的问题是对于我播放的每个视频,我需要:

加载htm文件 - >将视频ID嵌入其中 - >将文件写入光盘 - >现在我只能播放视频。

One of the working solutions I found was in this post by @ilias, he shows how to get this working with loading the HTML from a file (instead of a string like I do), problem with this approche is that for every video I play I need to:
load the htm file -> embed the video Id in it -> write the file to disc -> only now I can play the video.

奇怪的是,此解决方案仅在您从文件加载Web视图请求时起作用,如果我尝试从等于文件内容的字符串加载请求,这不起作用。

Strange thing is that this solution only work when you load the web view request from a file, if I try to load the request from a string equal to the file content, that doesn't work.

推荐答案

显然问题出在nil base url上,当我把它更改为resourceURL时,autoplay工作了。

Apparently the problem was with the nil base url, when I changed it to resourceURL the autoplay worked.

[self loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];  

自动播放youtube视频的完整代码(此代码主要基于这篇文章我刚刚将其更改为从字符串而不是文件加载:

The full code for autoplay youtube videos (again this code mostly based on this post I just changed it to load from a string instead of a file):

static NSString *youTubeVideoHTML = @"<!DOCTYPE html><html><head><style>body{margin:0px 0px 0px 0px;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = \"http://www.youtube.com/player_api\"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:'%0.0f', height:'%0.0f', videoId:'%@', events: { 'onReady': onPlayerReady, } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>";  

- (void)playVideoWithId:(NSString *)videoId {
    NSString *html = [NSString stringWithFormat:youTubeVideoHTML, self.frame.size.width, self.frame.size.height, videoId];

    [self loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];
}

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

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