Play YouTube videos with MPMoviePlayerController instead of UIWebView

Viewed 77106

I'm trying to stream some youTube videos using the MPMoviePlayerController but I'm having some problems. The code I'm using is pretty simple and I can play .m4v videos by passing a URL to initWithContentURL. When I launch the movie player the player comes up but just goes away after about 20 seconds. When I try it in the simulator I get an alert view that says the server is not configured correctly. Is there an argument I need to pass with the URL to get a specific type of video feed from google?

NSURL *videoURL = [NSURL URLWithString:@"http://www.youtube.com/v/HGd9qAfpZio&hl=en_US&fs=1&"];
MPMoviePlayerController *moviePlayer;
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];

[moviePlayer play];

I've also tried the following URL's http://www.youtube.com/watch?v=HGd9qAfpZio

I have also seen the argument &format=1 and tried to add that to the end of both of the strings but no luck.

8 Answers

There is an official way to embed YouTube video in iOS application which is provided by Google.

Install library via cocoapods: pod "youtube-ios-player-helper"

import by: #import “YTPlayerView.h” Use UIView and set class to YTPlayerView or create object of YTPlayerView programatically.

Load video: [self.playerView loadWithVideoId:@"M7lc1UVf-VE"];

There is also playVideo and stopVideo methods available. For more info visit this Link

Related