5

我正在尝试使用 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];
} 
4

2 回答 2

20

发现问题:

NSURL *url = [NSURL URLWithString:path];

必须替换为:

NSURL *url = [NSURL fileURLWithPath:path];

很难发现那个...

于 2011-05-20T12:11:21.110 回答
0

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

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

});
于 2015-05-30T13:37:30.963 回答