Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play YouTube videos with MPMoviePlayerController instead of UIWebView

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.

like image 223
jmurphy Avatar asked Nov 22 '09 18:11

jmurphy


4 Answers

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.

like image 66
Bryan Henry Avatar answered Oct 17 '22 14:10

Bryan Henry


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

like image 20
Jimit Avatar answered Oct 17 '22 15:10

Jimit


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

like image 23
hellozimi Avatar answered Oct 17 '22 14:10

hellozimi


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

LBYouTubeView

like image 12
TPoschel Avatar answered Oct 17 '22 15:10

TPoschel