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

查看:24
本文介绍了如何在 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.

我在这篇文章中找到了一个可行的解决方案 by @ilias,他展示了如何通过从文件(而不是像我那样加载字符串)加载 HTML 来实现这一点,这种方法的问题在于,对于我播放的每个视频,我都需要:
加载 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 基本 url 上,当我将其更改为 resourceURL 时,自动播放起作用了.

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天全站免登陆