在不使用UIWebView的情况下在iPhone应用中播放YouTube视频? [英] Play YouTube videos in iPhone app without using 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视频可能不支持/不在Simulator中工作。请检查一下。谢谢。
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.
这篇关于在不使用UIWebView的情况下在iPhone应用中播放YouTube视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!