使用YOUTUBE网址如何在ios中播放视频播放器

时间:2014-05-01 07:13:14

标签: ios objective-c

我希望能够点击表格视图项目并播放视频。我尝试使用MPMoviePlayerController来播放一些YouTube视频,但我遇到了一些问题。我正在使用json解析器从webserver检索数据。我的问题是,当用户点击UITableViewCell时,视频播放器会全屏显示,并且只是说'#34;正在加载..."整个时间和视频都没有加载,调试器中也没有报告错误。

-(void)viewDidLoad

{

urlsArray=[NSMutableArray alloc]init];

}

-(void)loadData

{

url=[NSURL URLWithString:@"urls”];

NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:url];

[[NSURLConnection alloc]initWithRequest:request delegate:self];

[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{

[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;

NSError *jsonError = nil;

id jsonObject = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&jsonError];

if ([jsonObject isKindOfClass:[NSArray class]])      

{

}

else if ([jsonObject isKindOfClass:[NSDictionary class]])

{

NSDictionary *jsonDictionary = (NSDictionary *)jsonObject;

NSArray *array=[[NSArray alloc]init];

array=[jsonDictionary objectForKey:@"video-urls"];        

dataDictionary=[array objectAtIndex:0];

NSLog(@"%@",dataDictionary);    

}

[urlsArray addObject:[dataDictionary objectForKey:@“Key1”]]; 

[urlsArray addObject:[dataDictionary objectForKey:@“Key2”]]; 

[urlsArray addObject:[dataDictionary objectForKey:@“Key3”]];

[urlsArray addObject:[dataDictionary objectForKey:@“Key4”]];

[urlsArray addObject:[dataDictionary objectForKey:@“Key6”]];       

NSLog(@"%@",urlsArray); 

} 


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

selectedIndex=indexPath.row;

NSLog(@"%@ %d",urlsArray,selectedIndex);

NSString *currentUrlsArr=[urlsArray objectAtIndex:indexPath.row];

NSLog(@"urlstring %@",currentUrlsArr);

NSString *youTubeString=[[NSString alloc]initWithFormat:@"https://www.youtube.com/watch?v="];

NSString *stringAppend=[youTubeString stringByAppendingString:currentUrlsArr];

stringAppend =[stringAppend stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

NSURL *urlString=[NSURL URLWithString:stringAppend];

NSLog(@"%@",urlString);

moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:urlString];

[self.view addSubview:moviePlayer.view];

moviePlayer.shouldAutoplay=YES;

[moviePlayer prepareToPlay];

moviePlayer.fullscreen = YES;

[moviePlayer play];

}

1 个答案:

答案 0 :(得分:1)

您可以使用[LBYoutubeURLExtractor](https://github.com/larcus94/LBYouTubeView/blob/master/LBYouTubeView/LBYouTubeExtractor.h)播放Youtube视频。

下载此课程并使用-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法中的以下代码:

LBYouTubeExtractor *extractor = [[LBYouTubeExtractor alloc] initWithURL: urlString quality:LBYouTubeVideoQualityLarge];
[extractor extractVideoURLWithCompletionBlock:^(NSURL *videoURL, NSError *error) {
        [appDelegate hideActivityIndicator];
        if(!error) {
            MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
           [self.view addSubview:moviePlayer.view];
           moviePlayer.shouldAutoplay=YES;
           [moviePlayer prepareToPlay];
           moviePlayer.fullscreen = YES;
           [moviePlayer play];
       }
}];

使用此方法,可以直接在videoURL中播放给定的MPMoviePlayerController

希望这有帮助。