我想从我的iPhone应用程序播放YouTube视频。我必须尝试使用以下代码在我的iPhone应用中播放YouTube视频,在iPhone应用程序中播放YouTube视频而不使用UIWebView?
[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
的代码是,
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
。提前致谢。
相关:http://stackoverflow.com/questions/1779511/play-youtube-videos-with-mpmovieplayercontroller-instead-of-uiwebview – 2012-02-03 16:45:21