Playing youtube video with MPMoviePlayerController

后端 未结 3 472
挽巷
挽巷 2021-01-03 12:39

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\'

3条回答
  •  温柔的废话
    2021-01-03 13:03

    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;
    }
    

提交回复
热议问题