我允许用户在我的应用程序中录制视频,然后再播放。当用户录制视频时,我会保存视频的 URL,然后稍后从保存的 URL 播放视频。我将视频保存在照片应用程序和我的应用程序中。如果我在照片应用程序中删除视频,它仍然会播放。大约 7 天后,视频被删除。我想我保存在我的 tmp 目录中,但我不确定。这是我正在做的事情:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
// Handle a movie capture
if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
NSString *moviePath = [NSString stringWithFormat:@"%@",[[info objectForKey:UIImagePickerControllerMediaURL] path]];
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
_justRecordedVideoURL = [NSString stringWithFormat:@"%@",videoURL];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
_managedObjectContext = [appDelegate managedObjectContext];
Video *video = [NSEntityDescription insertNewObjectForEntityForName:@"Video" inManagedObjectContext:_managedObjectContext];
[video setVideoData:videoData];
[video setVideoURL:[NSString stringWithFormat:@"%@",videoURL]];
NSError *error = nil;
if(![_managedObjectContext save:&error]){
//handle dat error
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *tempPath = [documentsDirectory stringByAppendingFormat:@"/vid1.mp4"];
BOOL success = [videoData writeToFile:tempPath atomically:NO];
if(success == FALSE){
NSLog(@"Video was not successfully saved.");
}
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath)) {
UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self,
@selector(video:didFinishSavingWithError:contextInfo:), nil);
}
}
}
我保存不正确吗?当我去播放视频时,它工作正常,几天后视频将播放没有音频,然后最终它就会消失。任何想法为什么?