objective-c - 在 Objective C 中获取并播放 YouTube URL

标签 objective-c ios ipad youtube

我正在开发一个应用程序,为用户提供播放保存在数据库中的 Youtube 视频的功能。如果用户愿意,他可以更新视频。

假设我保存了一个类似http://www.youtube.com/watch?v=1Ziku4FLka4 的链接在 DB 中,我在单击按钮时播放相同的链接,为此我实现了如下代码:

   UIWebView *wv;

strUTube = @"http://www.youtube.com/watch?v=1Ziku4FLka4";

        wv = [[UIWebView alloc] initWithFrame:CGRectMake(0,10,viewBG.frame.size.width,viewBG.frame.size.height)];


    NSMutableString *html = [[NSMutableString alloc] initWithCapacity:1];
    [html appendString:@"<html><head>"];
    [html appendString:@"<style type=\"text/css\">"];
    [html appendString:@"body {"];
    [html appendString:@"background-color: transparent;"];
    [html appendString:@"color: brown;"];
    [html appendString:@"}"];
    [html appendString:@"</style>"];
    [html appendString:@"</head><body style=\"margin:0\">"];
    [html appendFormat:@"<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\"", strUTube];

    [html appendFormat:@"width=\"%0.0f\" height=\"%0.0f\"></embed>", viewBG.frame.size.width,viewBG.frame.size.height];
    [html appendString:@"</body></html>"];

    NSLog(@"html:%@",html);
    [wv loadHTMLString:html baseURL:nil];
    [viewBG addSubview:wv];

    [self.view addSubview:viewBG];

然后在另一个按钮单击时,我调用 Youtube 的主页并使用以下代码获取在按钮单击事件上打开的 URL:

NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"window.location.href"];
NSLog(@"currentURL2 : %@",currentURL);

但是 URL 结果变成了 http://m.youtube.com/watch?feature=m-trends&v=to7uIG8KYhg 这样的结果以后不能播放。

我也尝试在我的 Mac Book 上打开该 URL,但没有成功。

最佳答案

首先使用新的 iFrame 方法嵌入您的电子管视频。下面是一个模板 url,您可以在其中填写宽度、高度、视频 ID 和代码中的 isAutoplay。

<html>
<head>
    <meta name="viewport" content="initial-scale = 1.0,maximum-scale = 1.0" />
</head>
    <body style="margin:0;padding:0;" bgcolor="#000000">
        <iframe class="youtube-player" background-color:#000000 allowtransparency= "true" type="text/html" width=%f height=%f src="http://www.youtube.com/embed/%@?fs=1&autoplay=%d&loop=%d&rel=0&version=3&enablejsapi=1&showinfo=0" frameborder="0" allowfullscreen>
        </iframe>
    </body>
</html>

NSString   *youTubeHTMLTemplate = [NSString stringWithContentsOfFile: path 
                                                         encoding: NSUTF8StringEncoding 
                                                            error: &error];
finalHtml = [NSString stringWithFormat:youTubeHTMLTemplate, htmlFrameWidth, htmlFrameHeight, videoID, isAutoplay];

您可以使用内置的 NSString 方法轻松地从 youtube 视频 url 中提取视频 ID。

关于objective-c - 在 Objective C 中获取并播放 YouTube URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12225081/

相关文章:

ios - UIButton 在 UIScrollView 的初始框架外不可点击

ios - 不执行任何 SQLite 查询

objective-c - 无法实现俄罗斯方 block 移动方法

ios - 第二次调用 textFieldDidEndEditing 时 NSFilemanager 不保存文件

ios - 在Light模式下获取PKDrawing的图像

ios - 将点击事件传递给 subview iOS

ios - 返回的 UITextField 框架宽度太短?

iphone - 游戏中心cocos2d问题

ios - Apple Mach-O 链接器错误 - ld : 1 duplicate symbol for architecture armv7

iphone - CGAffineTransformRotate 应用于 UIView 后宽度和高度增加