如何在ios中播放视频播放器

时间:2014-04-28 05:07:36

标签: ios objective-c

我希望能够点击一个tableview项目并播放视频。所以我写了一些视频播放器的代码。我正在使用MPMediaplayer框架。我正在使用json解析器。使用json解析器从中检索数据网络服务器。我的问题是,当用户点击UITableView单元格时,视频播放器会全屏显示,并且只是说“#34; loading ..."整个时间和视频都没有加载,调试器中也没有报告任何错误。请帮我任何身体。谢谢你提前。

-(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];
    NSURL *urlString=[NSURL URLWithString:currentUrlsArr];
    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:urlString];
    [self.view addSubview:moviePlayer.view];
    moviePlayer.fullscreen = YES;
    [moviePlayer play];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

2 个答案:

答案 0 :(得分:0)

您不需要设置电影播放器​​视图的帧。正如Apple show themselves

MPMoviePlayerController *player =
        [[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player prepareToPlay];
[player.view setFrame: myView.bounds];  // player's frame must match parent's
[myView addSubview: player.view];
// ...
[player play];

答案 1 :(得分:0)

这是问题

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

之后你错过了这个方法来调用

[moviePlayer prepareToPlay];