Play YouTube videos using MPMoviePlayerController instead of UIWebView

I'm trying to transfer some youTube videos using MPMoviePlayerController but I am having some problems. The code I'm using is pretty simple, and I can play the .m4v video by passing the initWithContentURL URL. When I start the player, the player appears, but just leaves after about 20 seconds. When I try in the simulator, I get a warning saying that the server is configured incorrectly. Is there an argument I need to pass with a url in order to get a specific type of video stream 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 also tried using the following URL http://www.youtube.com/watch?v=HGd9qAfpZio

I also saw the argument & format = 1 and tried to add this to the end of both lines, but no luck.

+85
ios youtube video mpmovieplayercontroller streaming
Nov 22 '09 at 18:27
source share
7 answers

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

Unfortunately, there is no way to directly play videos from youtube using MPMoviePlayerController , because youtube does not provide direct links to video files.

+77
Nov 23 '09 at 5:06
source share

If you use 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 follow this link

Make sure you test it on the device, not on the simulator. Since the simulator will always display a question mark with a blue field (it does not have a live player).

+25
Jul 13 '10 at 21:33
source share

As I wrote above in my comment, I had to do this in a project that I was working on. I solved this problem and introduced the code on github.

You can check the source and make it here .

He basically takes the youtube URL and gets all the iOS videos compatible.

Hope this helps! Greetings

+22
Jun 08 2018-12-12T00:
source share

I just stumbled upon someone who was able to host a YouTube video inside MPMoviePlayerController . Now it is possible.

LBYouTubeView

+13
Jun 01 2018-12-12T00:
source share

To play videos with pipes, you need to extract the URL before passing the URL to MPMoviePlayer . You cannot play it directly.

My github repo has a demo implementation of this.

Cm:

https://github.com/DpzAtMicRO/IOSYoutubePlayer

Try it, it allows you to download and play YouTube videos directly in MPMoviePlayer , like any other video, and this is also a very good approach.

EDIT: Before using this in your project, make sure you read Readme.md well.

+2
Dec 05
source share

It's as simple as taking an src iframe element for the new insert code and calling

 [self.webView loadRequest:[NSURLRequest requestWithURL:self.url]]; 
+1
Oct 21 '12 at 15:49
source share

Check out the following link. I think this will help you.

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

0
Sep 17 '16 at 6:15
source share



All Articles