MPMoviePlayerViewController无法正常工作

时间:2012-10-08 08:34:38

标签: objective-c automatic-ref-counting mpmovieplayercontroller modalviewcontroller

我一直在互联网上漫游寻求解决方案我真的不知道我做错了什么。我几天来一直在处理这个问题。我将视频保存在应用程序可访问的以下路径中(对吗?)

//NSDocumentDirectory doesn't work either.
NSArray *newPath = NSSearchPathForDirectoriesInDomains(NSMoviesDirectory,
                                                       NSUserDomainMask, YES);
NSString *moviesDirectory = [NSString stringWithFormat:@"%@/WebSurg", 
                                                     [newPath objectAtIndex:0]];
// Check if the directory already exists
if (![[NSFileManager defaultManager] fileExistsAtPath:moviesDirectory]) {
    // Directory does not exist so create it
    [[NSFileManager defaultManager] createDirectoryAtPath:moviesDirectory 
                      withIntermediateDirectories:YES attributes:nil error:nil];
}

我在应用程序的tableView中显示了该目录的内容。点击一行时,它应该播放视频。但事实并非如此。它向我展示了MPMoviePlayerViewController模式视图,然后隐藏它可能是1秒。这是我用来播放它的代码: 我尝试了两种方法让这条路无济于事。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *moviesPath = NSSearchPathForDirectoriesInDomains(NSMoviesDirectory, 
                                                              NSUserDomainMask, YES);
    NSString *moviesDirectory = [NSString stringWithFormat:@"%@/WebSurg", 
                                                       [moviesPath objectAtIndex:0]];
    NSString *movie = [self.tableData objectAtIndex:indexPath.row];
    NSString *moviePath = [NSString stringWithFormat:@"%@/%@", 
                                                         moviesDirectory, movie];
    NSURL *movieURL = [NSURL fileURLWithPath:moviePath]; 
    NSLog(@"MOVIEPATH: %@", moviePath);

    NSString *alternatePath = [NSString stringWithFormat:@"%@/%@", 
                                            [[NSBundle mainBundle] resourcePath], movie];
    NSURL *alternateMoviePath = [NSURL fileURLWithPath:moviePath];
    movieViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:alternateMoviePath];
    movieViewController.moviePlayer.movieSourceType= MPMovieSourceTypeFile;
    NSLog(@"Movie Load State: %d", [[movieViewController moviePlayer] loadState]);
    NSLog(@"Alternate movie Path: %@", alternatePath);
    [self presentMoviePlayerViewControllerAnimated:movieViewController];
    [movieViewController.moviePlayer play];
    [self checkAndPlay];
}

- (void) checkAndPlay {
    if ([[self.movieViewController moviePlayer] loadState] == MPMovieLoadStateUnknown) { 
        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:
                               @selector(checkAndPlay) userInfo:nil repeats:NO];
    } else {
        [self.movieViewController setModalTransitionStyle:
                                   UIModalTransitionStyleCrossDissolve];
        [self presentModalViewController:movieViewController animated:YES];
    }
}

这些是控制台的结果:

2012-10-08 10:14:52.392 WebsurgTemplates[3722:17903] MOVIEPATH: /Users/THISISME/Library/Application Support/iPhone Simulator/5.0/Applications/E075DBE3-BFEA-4F6A-9DFA-2CC912E14863/Movies/WebSurg/FirstVideo.mp4
2012-10-08 10:14:52.459 WebsurgTemplates[3722:17903] Movie Load State: 0
2012-10-08 10:14:52.460 WebsurgTemplates[3722:17903] Alternate movie Path: /Users/THISISME/Library/Application Support/iPhone Simulator/5.0/Applications/E075DBE3-BFEA-4F6A-9DFA-2CC912E14863/WebsurgTemplates.app/FirstVideo.mp4

我非常感谢任何建议和帮助!!

更新

到目前为止我没有取得任何进展。我设法将一些其他数据记录到控制台,一些信息可能有助于解决此问题。我试图制作一个空白项目,直接下载视频作为播放视频的链接,但它不起作用。发生的事情完全相同。 jaydee3说可能是因为我可能无法访问NSMoviesDirectory。所以我改为NSDocumentDirectory,但没有解决问题。我检查了文件是否存在以及保存的格式,以便播放器可以读取。它仍然无效。我不知道我做错了什么。再次感谢您的建议/帮助。

这里是调试的结果。更完整:

if ([[NSFileManager defaultManager] fileExistsAtPath:moviePath]) {
    NSLog(@"FILE EXISTS");
    CFStringRef fileExtension = (__bridge CFStringRef) [moviePath pathExtension];
    CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);

    if (UTTypeConformsTo(fileUTI, kUTTypeImage)) NSLog(@"It's an image");
    else if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) NSLog(@"It's a movie");
    else if (UTTypeConformsTo(fileUTI, kUTTypeText)) NSLog(@"It's text");
}

RESULTS
[6343:17903] MOVIEPATH: /Users/myname/Library/Application Support/iPhone Simulator/5.0/Applications/E075DBE3-BFEA-4F6A-9DFA-2CC912E14863/Documents/FirstVideo.mp4
[6343:17903] FILE EXISTS
[6343:17903] It's a movie

1 个答案:

答案 0 :(得分:1)

好的,所以我设法解决了问题并找到了罪魁祸首。

简要说明是因为下载的电影没有正确保存(我目前正在调查可能的原因)。因此,玩家正在尝试播放存在的文件,格式正确且名称正确,但这是空的。我通过在下载,传输和播放后记录所有文件大小来找到它。

现在更具描述性的问题是我正在将电影下载到NSCachesDirectory,然后将其保存到NSDocumentDirectory。我发现这是因为我开始怀疑它是否真的找到了文件以及文件是否“可食用”。它现在播放电影很好,因为我直接下载到NSDocumentDirectory。现在我必须解决以防万一连接断开。由于NSCachesDirectory中的保存自动解决了。我对这方面的建议持开放态度。以下代码无法将数据从NSCachesDirectory传输到NSDocumentDirectory

NSArray *paths = NSSearchPathForDirectoriesInDomains
                // HERE I changed NSCachesDirectory to NSDocumentDirectory fixed it
                            (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];
NSString *downloadPath = [cachesDirectory stringByAppendingPathComponent:
                                                  @"DownloadedVideo.mp4"];
NSArray *newPath = NSSearchPathForDirectoriesInDomains
                                  (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *moviesDirectory = [NSString stringWithFormat:@"%@", 
                                                [newPath objectAtIndex:0]];
self.downloadOperation = [ApplicationDelegate.mainDownloader downloadVideoFrom:
                                    @"http://www.blablabla.web/iphone/download.php"                                    
                                            toFile:downloadPath];