Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

ios - Play YouTube videos in iPhone app without using UIWebView?

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);
}

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];

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just use XCDYouTubeVideoPlayerViewController. It seamlessly adds youtube video to your app without the need of UIWebView.

It uses progressive download + MoviePlayer and AVFoundation frameworks. Just supply it with a youtube video ID and you are good to go.

It supports both full-screen & non full-screen and WORKS ON iOS SIMULATOR!!!

Example:

XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:@"9bZkp7q19f0"];
[self presentMoviePlayerViewControllerAnimated:videoPlayerViewController];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...