MPMoviePlayerController“正在加载电影...”

时间:2011-05-20 11:02:21

标签: iphone objective-c video mpmovieplayercontroller iphone-video

我正在尝试使用iOS 4.1在iPhone上的资源文件夹中播放视频文件。 MPMoviePlayerViewController显示视图,但它只会在活动指示器旋转时显示“正在加载电影...”。谁知道这个的原因?我尝试过各种适用于iPhone的视频格式,每次都有相同的结果。

电影播放器​​会响应显示和隐藏控件等操作。

我的代码:

-(void) playVideoNamed:(NSString *)videoName
{
    // I get the path like this so the user can enter extension in the filename
    NSString *path = [NSString stringWithFormat:@"%@/%@", 
                      [[NSBundle mainBundle] resourcePath], videoName];
    NSLog(@"Path of video file: %@", path);

    RootViewController *rootController = [(DerpAppDelegate *)[[UIApplication sharedApplication] delegate] viewController];

    NSURL *url = [NSURL URLWithString:path];

    MPMoviePlayerViewController *vc = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    vc.moviePlayer.movieSourceType = MPMovieSourceTypeFile;

    [rootController presentMoviePlayerViewControllerAnimated:vc];
    [vc.moviePlayer prepareToPlay]; // Not sure about this... I've copied this code from various others that claims this works
    [vc.moviePlayer play];
} 

2 个答案:

答案 0 :(得分:20)

发现问题:

NSURL *url = [NSURL URLWithString:path];

必须替换为:

NSURL *url = [NSURL fileURLWithPath:path];

很难发现那个......

答案 1 :(得分:0)

我在不同的线程上执行代码行,首先调度到主线程修复它:

dispatch_async(dispatch_get_main_queue(), ^(void){
        MPMoviePlayerViewController* theMovie = [[MPMoviePlayerViewController alloc] initWithContentURL: videoURL];
        [self presentMoviePlayerViewControllerAnimated:theMovie];

});