Viral Narshana
Viral Narshana

Reputation: 1877

how to play youtube video in iphone application?

I want to play Youtube video within my application with url.I tried the following code, but it doesn't work.

   NSURL *url = [NSURL URLWithString:@"http://www.youtube.com/v/pGqraZN5U0k&amp"];
   MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
   if (moviePlayer)
   {
     [self.moviePlayer play];
   }

Please help me.

Thanks in advance.

Upvotes: 1

Views: 4376

Answers (2)

Viral Narshana
Viral Narshana

Reputation: 1877

NSString *youTubeVideoHTML = @"<html><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:youTubeVideoHTML, youtube video link, self.webView.frame.size.width, self.webView.frame.size.height];
NSLog(@"html %@",html);
[self.webView loadHTMLString:html baseURL:nil];

Upvotes: 1

Josh Brown
Josh Brown

Reputation: 53073

YouTube videos can't be played directly in MPMoviePlayerController. You'll have to launch the YouTube app or create a UIWebView to show the video. See the following question:

Play youtube videos with MPMoviePlayerController

Upvotes: 2

Related Questions