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.

upvote
  flag
I found some more information but it still doesn't work. Somebody suggested using this for a URL: youtube.com/get_video?video_id=ID&t=SIGNATURE&fmt=18 I put in my video ID but I don't know what signature is? And the video player still behaves the same. It opens the player then it closes after 10 to 20 seconds. – jmurphy

7 Answers 11

up vote 77 down vote accepted

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

1 upvote
  flag
Yup, that's the only way to play YouTube videos in your app. Spent 2 looking for alternatives. Quality is kinda bad, too. – samvermette
upvote
  flag
Is there still no other way - after more than 2 years? – fabb
upvote
  flag
Is there a possibility to get the PlaybackFinishReason and PlaybackState notifications in case of playing an youtube video as embeded ? – Roshit
7 upvote
  flag
This isn't the only way. You can get the mp4 file from Youtube. I do that in my AirPlayer Python Project, which enables you to play youtube videos on your AppleTV. I will port this and use it in my current iOS project and post the code here when It's ready. – hellozimi
upvote
  flag
@hellozimi: You rock! Btw, what's the status of the iOS port? I'll soon need it in a project and if it's far from being ready I might do the porting myself. – Lvsti
upvote
  flag
ah nevermind, just saw your answer below. – Lvsti
upvote
  flag
@samvermette #Sbrocket # hellozim It is sure that there are alternate ways to play youtube videos. Tried and tested, what needs to be confirmed is " Is watching/streaming Youtube video using its mp4 url directly allowed ? Does it violate copyrights ? If yes why are there so many apps (in app store) doing it ?" – Tatvamasi
7 upvote
  flag
Unfortunately using a direct link is prohibited. Section II/10. in developers.google.com/youtube/terms: "Your API Client will not [...] access any portion of any YouTube audiovisual content by any means other than use of a YouTube player or other video player expressly authorized by YouTube;" – Lvsti
upvote
  flag
The problem with this is that is you have a portrait only app, the video will show in portrait only too. – sebrock
upvote
  flag
@hellozimi Unfortunately the method you are suggesting is against YouTube's ToS, so it may stop working at any point (all YouTube needs to do is change the embed code they generate slightly in order to break such disallowed playback). – Mathew
upvote
  flag
@MicRO See my above comment. This is the only allowed way to play YouTube videos within an iOS application. – Mathew
upvote
  flag
Nice answer!! What if we want to disable video downloading option so that 3rd party downloader apps do not show alerts to download this video?? Is it possible to stream video and prevent the video from downloading/saving ? – W.S

If you are using Code:

- (void)embedYouTube:(NSString*)url 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, url, frame.size.width, frame.size.height];  
   if(videoView == nil) {  
      videoView = [[UIWebView alloc] initWithFrame:frame];  
     [self.view addSubview:videoView];  
    }  
    [videoView loadHTMLString:html baseURL:nil];  
  }

Source:

Just refer this link

Make sure that you test it on device and not on the simulator. Since simulator will always display question mark with blue box (it doesn't have quick-time player).

3 upvote
  flag
Use video url with format: youtube.com/v/XXXXXXX – Morten Holmgaard

I just stumbled across someone who was able to place a YouTube video inside an MPMoviePlayerController. It does appear possible now.

LBYouTubeView

upvote
  flag
@Thanx its working fine but when i use [mpplayer stop]; to stop this video then the video is running.but i want to stop video – Jaspreet Singh
upvote
  flag
is the github link also play the audio in background? – Khalid Usman

As I wrote above in my comment I had to do this in a project I was working on. I've solved this problem and submitted the code on github.

You can check the source and fork it here.

It basically takes a youtube url and gets all the iOS compatible video urls.

Hope this is to any help!
Cheers

2 upvote
  flag
is it allowed ? – Tatvamasi
1 upvote
  flag
Thanks! BTW, the updates fixed the issue with recent YouTube changes. – Peter DeWeese
1 upvote
  flag
This is not allowed under YouTube's ToS. Use it at your own risk, because your app may stop working at any point if YouTube notices you are not following the ToS OR if YouTube changes the embed code it generates. – Mathew

It's as simple as taking the src element of the iframe for the new embed code and calling

[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];

To play you tube videos you need to extract the url before passing the url into MPMoviePlayer. You can't play it directly.

My github repo has a demo implementation of this.

See:

https://github.com/DpzAtMicRO/IOSYoutubePlayer

Try it, this makes it possible to load and play youtube video directly in MPMoviePlayerlike any other video and is pretty good approach too.

EDIT: Make sure that you go through the Readme.md well before using this in your project

2 upvote
  flag
Against YouTube's ToS, unfortunately. If you do this, you can only expect your app to work for as long as YouTube is unaware of what you are doing. – Mathew
upvote
  flag
I emailed the YouTube API guys a while back as well to ask if it would be ok for me to parse the video URL out myself; this is a quote from the reply I got: "The only mechanism supported is the iframe embed. Raw stream access is not supported by our ToS other than the low res RTSP streams we provide for feature phones" – Mathew
1 upvote
  flag
You've convinced me on the downvoting point; I agree that it should be a tool to mark incorrectness, and that upvoting is enough to show which of these answers I think are most relevant to people trying to solve the YouTube embed problem. Damnit, Apparently StackOverflow locks my downvote in and I cannot remove it anymore unless your answer is edited. – Mathew
upvote
  flag
great! I can make an edit and request you to remove your downvote then! ...But...no need..let it be there! – D_D
upvote
  flag
@Mathew A lot of legit iOS apps are doing this kind of thing (bypassing YouTube's API) and although it violates the ToS as you've been pointing out, Google doesn't seem to mind in many cases since it allows their product to reach more viewers. – rmp251
upvote
  flag
@Mathew That's interesting. Does that mean that highly customized video players which use YouTube content, such as Swift Player and Epoch (by Deja.io) for e.g., will basically be shut down? I know there are official ways of using YT content (and this is preferred) but it is still restrictive and doesn't allow for customization to the degree that using raw video does. – rmp251
upvote
  flag
@Mathew I'm not clear on something. Does YouTube actually have the ability to block direct access to its raw videos (from anyone, on a technical level), or is it just that they will legally go after those who violate its ToS? – rmp251

Check the following link. I think it will help you.

https://mobiletechfeast.blogspot.in/2014/08/youtube-player-for-ios.html

Not the answer you're looking for? Browse other questions tagged or ask your own question.