user3195388
user3195388

Reputation: 129

Playing youtube video with MPMoviePlayerController

i've been using an html string for a long period for playing youtube videos through an UIWebView, the problem is i want to get notifications with playbackstate changed. i've decided to create an MPMoviePlayerController and play the youtube video through this, but cant seem to make it work. i'm using following code in viewdidload:

NSString *urlAddress = @"http://www.youtube.com/watch?v=m01MYOpbdIk";
NSURL *url = [NSURL URLWithString:urlAddress];
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = [UIScreen mainScreen].bounds.size.height;
movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
movie.scalingMode=MPMovieScalingModeAspectFill;
movie.view.frame = CGRectMake(0.0, 0.0, width, height);
[self.view addSubview:movie.view];
[movie play];

Gives me this error:

_itemFailedToPlayToEnd: {
    kind = 1;
    new = 2;
    old = 0;
 }

Upvotes: 2

Views: 8048

Answers (3)

Diken Shah
Diken Shah

Reputation: 1189

For me this library did work perfectly! https://github.com/hellozimi/HCYoutubeParser

moviePlayer = [[MPMoviePlayerController alloc] init];    
moviePlayer.shouldAutoplay = YES;
moviePlayer.fullscreen = YES;
moviePlayer.repeatMode = MPMovieRepeatModeNone;
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

After this call method.

[self callYouTubeURL:[NSString stringWithFormat:@"http://www.youtube.com/embed/%@",_urlcode]];

In this method parse youtube link.

- (void)callYouTubeURL:(NSString *)urlLink
{

    NSURL *url = [NSURL URLWithString:urlLink];
    actvity.hidden = NO;
    [HCYoutubeParser thumbnailForYoutubeURL:url thumbnailSize:YouTubeThumbnailDefaultHighQuality completeBlock:^(UIImage *image, NSError *error) {

        if (!error) {

            [HCYoutubeParser h264videosWithYoutubeURL:url completeBlock:^(NSDictionary *videoDictionary, NSError *error) {


                NSDictionary *qualities = videoDictionary;

                NSString *URLString = nil;
                if ([qualities objectForKey:@"small"] != nil) {
                    URLString = [qualities objectForKey:@"small"];
                }
                else if ([qualities objectForKey:@"live"] != nil) {
                    URLString = [qualities objectForKey:@"live"];
                }
                else {
                    [[[UIAlertView alloc] initWithTitle:@"Error" message:@"Couldn't find youtube video" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles: nil] show];
                    return;
                }
                _urlToLoad = [NSURL URLWithString:URLString];


                [self urlLoadintoPlayer];

            }];
        }
        else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
            [alert show];
        }
    }];
}

After this load newly parse url into player.

-(void)urlLoadintoPlayer
{
    moviePlayer.contentURL = _urlToLoad;
}

Upvotes: 4

Aadi007
Aadi007

Reputation: 247

Official way to play youtube videos: 1) UIWebView with the embed tag from Youtube the UIWebView's content.2) Using youtube-ios-player-helper[Google official way]

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

There are library which runs youtube videos through MPMoviePlayerController, but they are against the TOC of youtube. Hence simplest method is to go for youtube-ios-player-helper.

In case youtube-ios-player-helper pod doesn't work you can add YTPlayer.h/.m and assets folder to your project and write a bridge header with #import "YTPlayerView.h" and rest procedure you can follow on https://github.com/youtube/youtube-ios-player-helper This definitely worked for me!

Upvotes: 0

Shamsudheen TK
Shamsudheen TK

Reputation: 31311

Make use of custom LBYouTubePlayerViewController

It is a subclass of MPMoviePlayerViewController.

LBYouTubeView is just a small view that is able to display YouTube videos in a MPMoviePlayerController. You even have the choice between high-quality and standard quality stream.

It just loads the HTML code of YouTube's mobile website and looks for the data in the script tag.

LBYouTubeView doesn't use UIWebView which makes it faster and look cleaner.

Upvotes: 2

Related Questions