【问题标题】:How to play video player in ios如何在ios中播放视频播放器
【发布时间】:2014-04-28 05:07:36
【问题描述】:

我希望能够单击 tableview 项目并播放视频。所以我为视频播放器编写了一些代码。我正在使用 MPMediaplayer 框架。我正在使用 json 解析器。使用 json 解析器从中检索数据网络服务器。我的问题是,当用户单击 UITableView 单元格时,视频播放器会全屏打开,并且一直说“正在加载...”并且视频没有加载,调试器中也没有报告错误。请帮助我的任何身体..提前谢谢。

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

【问题讨论】:

  • 检查是否获取到 currentUrlsArr
  • @user3419311 确保您是通过 iOS 设备而不是在模拟器上检查它
  • urlString 创建了吗?

标签: ios objective-c


【解决方案1】:

您没有设置电影播放器​​视图的框架。作为苹果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];

【讨论】:

    【解决方案2】:

    问题来了

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

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

    [moviePlayer prepareToPlay];
    

    【讨论】:

    • 兄弟我还是有同样的问题兄弟
    • 兄弟我收到了 json 响应兄弟
    • 您可以纠正直到将 currentUrlsArr 值作为 JSON 获取,但是创建 MPMoviePlayer 您需要传递有效的 NSUrl,交叉检查 NSUrl 创建的组织不是..
    猜你喜欢
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    • 2017-07-19
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    相关资源
    最近更新 更多