1

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

//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
4

1 回答 1

1

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

简而言之,这是因为下载的电影没有正确保存(我目前正在调查可能的原因)。正因为如此,播放器试图播放一个存在的文件,格式和名称都正确,但它是空的。我通过在下载、传输和播放后记录所有文件大小发现了这一点。

现在更具描述性的问题是我正在将电影下载到NSCachesDirectory然后将其保存到NSDocumentDirectory. 我发现这个是因为我开始怀疑它是否真的找到了该文件以及该文件是否“可食用”。它现在可以很好地播放电影,因为我将它直接下载到NSDocumentDirectory. 现在我必须解决以防万一连接中断。作为保存NSCachesDirectory自动解决。我对这方面的建议持开放态度。这是无法将数据从 to 传输的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]; 
于 2012-10-09T14:20:39.403 回答