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