MPMoviePlayerViewController无法播放

时间:2013-03-11 15:36:42

标签: ios objective-c video-streaming mpmovieplayercontroller mpmoviewcontroller

我会保持简单,继承代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
VideoEntry *entry = [videoEntries objectAtIndex:indexPath.row];

[HCYoutubeParser h264videosWithYoutubeURL:[NSURL URLWithString:entry.url] completeBlock:^(NSDictionary *videoDictionary, NSError *error) {
    NSArray *urls = [videoDictionary allValues];
    NSURL *url = [NSURL URLWithString:[urls objectAtIndex:0]];
    [mp.moviePlayer setAllowsAirPlay:YES];
    [mp.moviePlayer setContentURL:url];
    [mp.moviePlayer prepareToPlay];
    [mp.moviePlayer play];
    [self presentMoviePlayerViewControllerAnimated:mp];
}];

}

mp是一个MPMoviePlayerViewController。显示视图控制器,但电影没有启动,它只是说“正在加载...”,然后在你问我是否100%确定链接有效。

谢谢!

1 个答案:

答案 0 :(得分:1)

它不起作用,因为未在主线程上调用完成块。 您可以通过在主线程上强制执行代码来解决:

[HCYoutubeParser h264videosWithYoutubeURL:[NSURL URLWithString:entry.url]
                            completeBlock:^(NSDictionary *videoDictionary, NSError *error) {
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        NSArray *urls = [videoDictionary allValues];
        NSURL *url = [NSURL URLWithString:[urls objectAtIndex:0]];
        [mp.moviePlayer setAllowsAirPlay:YES];
        [mp.moviePlayer setContentURL:url];
        [mp.moviePlayer prepareToPlay];
        [mp.moviePlayer play];
        [self presentMoviePlayerViewControllerAnimated:mp];
    });
}];