views:

2652

answers:

3

Hello 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.

Thanks in advance for any help!

+1  A: 

First result in google - http://iphoneincubator.com/blog/audio-video/how-to-play-youtube-videos-within-an-application looks relevant to me.

Muxecoid
Unfortunately this link doesn't help me. I've tried inserting the YouTube link into the MPMoviePlayerController and the movie player opens then closes without showing the video. Thanks for your help though.
jmurphy
In the post at that link you can find out that you won't see anything in Simulator, just on device
realsugar
+11  A: 

The only way to have a youtube video play inside your own app is to create a UIWebView with the embed tag from Youtube for the movie you want to play as the UIWebView's content. UIWebView will detect that the embedded object is a Youtube link, and the web view's content will be the youtube preview for the video. When your user clicks the preview, the video will be shown in an MPMoviePlayerController. This is the technique described at the link that Muxecoid provided (http://iphoneincubator.com/blog/audio-video/how-to-play-youtube-videos-within-an-application), and this is (as far as I know of) the only way to have a youtube video play within an application. You can still have the Youtube application launch by passing the youtube URL to -[UIApplication openURL:], but of course this closes your own application which is often undesirable.

Unfortunately, there's no way to directly play a youtube video with MPMoviePlayerController because youtube does not expose direct links to the video files.

Sbrocket
Yup, that's the only way to play YouTube videos in your app. Spent 2 looking for alternatives. Quality is kinda bad, too.
Sam V
+1  A: 
Jimit