在 iPhone 应用程序中播放 YouTube 视频而不使用 UIWebView?

Play YouTube videos in iPhone app without using UIWebView?(在 iPhone 应用程序中播放 YouTube 视频而不使用 UIWebView?)

本文介绍了在 iPhone 应用程序中播放 YouTube 视频而不使用 UIWebView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过我的 iPhone 应用播放 YouTube 视频.我必须尝试使用​​以下代码在我的 iPhone 应用中播放 YouTube 视频,

I want to play YouTube videos from my iPhone app. I have to tried play YouTube videos in my iPhone app with the following codes,

[self playVideo:@"http://www.youtube.com/watch?v=WL2l_Q1AR_Q" frame:CGRectMake(20, 70, 280, 250)];

- (void)playVideo:(NSString *)urlString frame:(CGRect)frame
{
    NSString *embedHTML = @"
    <html><head>
    <style type="text/css">
    body {
    background-color: transparent;
    color: white;
    }
    </style>
    </head><body style="margin:0">
    <embed id="yt" src="%@" type="application/x-shockwave-flash" 
    width="%0.0f" height="%0.0f"></embed>
    </body></html>";
    NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height];
    UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
    [videoView loadHTMLString:html baseURL:nil];
    [self.view addSubview:videoView];
    [videoView release];
    NSLog(@"%@",html);
}

但是,这段代码对我不起作用.我也试过 MPMoviePlayerController 代码是,

But, this code is not working for me. And also i have tried with MPMoviePlayerController the code is,

NSURL *fileURL = [NSURL URLWithString:@"http://www.youtube.com/watch?v=WL2l_Q1AR_Q"];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)]; 
[self.view addSubview:moviePlayerController.view];  
moviePlayerController.fullscreen = YES;  
[moviePlayerController play];

这对我也不起作用.谁能告诉我我在哪里做错并建议我任何想法?我想在 MPMoviewPlayerController 中播放 YouTube 视频,而不是使用 UIWebView.提前致谢.

This is also not working for me. Can anyone please tell me where i did wrong and suggest me any ideas? I want to play the YouTube videos in MPMoviewPlayerController rather than using UIWebView. Thanks in advance.

推荐答案

Mr.Yuvaraj.M 你上面的代码是正确的.请在您的 iPhone/iPod touch 设备中运行该项目.youtube 视频可能不支持/无法在模拟器中使用.请检查一下.谢谢.

Mr.Yuvaraj.M your above code is correct. Please run the project in your iPhone/iPod touch device. The youtube videos may not support/not work in Simulator. Please check on this. Thanks.

这篇关于在 iPhone 应用程序中播放 YouTube 视频而不使用 UIWebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 iPhone 应用程序中播放 YouTube 视频而不使用 UIWebView?

基础教程推荐