2

I want to play a youtube video in my app. How can I do this? I only have the playing local video code:

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[[player view] setFrame:[self.view bounds]]; // Frame must match parent view
[self.view addSubview:[player view]];
[player play];
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83

2 Answers2

0

Like this?

    NSString *exampleUrlString = @"https://www.youtube.com/embed/2bfP3TZHUzY?wmode=opaque&rel=0&autohide=1&showinfo=0&wmode=transparent";
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:exampleUrlString]]];
    [self.view addSubview:webView];
Ty Lertwichaiworawit
  • 2,950
  • 2
  • 23
  • 42
0

The YouTube URL scheme is used to connect to the YouTube website to play the specified video. If your app links to YouTube content, you can use this scheme to play videos from your app.

Unlike some schemes, YouTube URLs do not start with a “youtube” scheme identifier. Instead, they are specified as regular http links but are targeted at the YouTube server. The following examples show the basic strings you would use in Safari and in an app to show a YouTube video. In each example, you would need to replace the VIDEO_IDENTIFIER value with the identifier of the video you wanted to display:

  • HTML links:

<a href="http://www.youtube.com/watch?v=VIDEO_IDENTIFIER">Play Video</a>

<a href="http://www.youtube.com/v/VIDEO_IDENTIFIER">Play Video</a>

  • Native app URL strings:

    http://www.youtube.com/watch?v=VIDEO_IDENTIFIER

    http://www.youtube.com/v/VIDEO_IDENTIFIER

The easiest way to play a video from YouTube (or any other website) is to write a single line of code. If you copy/paste the YouTube video link into an NSString literal, you can create the URL and open it.

Play a YouTube Video URL from your APP like this:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.youtube.com/watch?v=VIDEO_IDENTIFIER"]];
Ezimet
  • 5,058
  • 4
  • 23
  • 29