Play YouTube videos with MPMoviePlayerController instead of UIWebView

76,950

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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

LBYouTubeView

Solution 5

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

Share:
76,950
jmurphy
Author by

jmurphy

Updated on March 06, 2020

Comments

  • jmurphy
    jmurphy about 4 years

    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.

  • samvermette
    samvermette over 14 years
    Yup, that's the only way to play YouTube videos in your app. Spent 2 looking for alternatives. Quality is kinda bad, too.
  • fabb
    fabb over 12 years
    Is there still no other way - after more than 2 years?
  • Roshit
    Roshit about 12 years
    Is there a possibility to get the PlaybackFinishReason and PlaybackState notifications in case of playing an youtube video as embeded ?
  • hellozimi
    hellozimi about 12 years
    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.
  • Lvsti
    Lvsti almost 12 years
    @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.
  • Tatvamasi
    Tatvamasi almost 12 years
    @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 ?"
  • Lvsti
    Lvsti over 11 years
    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;"
  • Peter DeWeese
    Peter DeWeese over 11 years
    Thanks! BTW, the updates fixed the issue with recent YouTube changes.
  • Jaspreet Singh
    Jaspreet Singh over 11 years
    @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
  • sebrock
    sebrock over 11 years
    The problem with this is that is you have a portrait only app, the video will show in portrait only too.
  • Khalid Usman
    Khalid Usman about 11 years
    is the github link also play the audio in background?
  • Mathew
    Mathew about 11 years
    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
    Mathew about 11 years
    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
    Mathew about 11 years
    @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
    Mathew about 11 years
    @MicRO See my above comment. This is the only allowed way to play YouTube videos within an iOS application.
  • Mathew
    Mathew about 11 years
    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
    Mathew about 11 years
    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.
  • DD_
    DD_ about 11 years
    great! I can make an edit and request you to remove your downvote then! ...But...no need..let it be there!
  • W.S
    W.S almost 11 years
    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 ?
  • rmp251
    rmp251 about 10 years
    @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
    rmp251 about 10 years
    @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
    rmp251 about 10 years
    @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?