我可以使用以下代码录制视频并在我的应用上播放:
- (IBAction)recordVideo:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
[self presentViewController:picker animated:YES completion:NULL];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.videoURL = info[UIImagePickerControllerMediaURL];
[picker dismissViewControllerAnimated:YES completion:NULL];
self.videoController = [[MPMoviePlayerController alloc] init];
[self.videoController setContentURL:self.videoURL];
[self.videoController.view setFrame:CGRectMake (0, 40, 320, 385)];
[self.view addSubview:self.videoController.view];
[self.videoController play];
}
我也知道在播放时可以使用以下方式保存视频:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.movie"]){
// Saving the video / // Get the new unique filename
NSString *sourcePath = [[info objectForKey:@"UIImagePickerControllerMediaURL"]relativePath];
UISaveVideoAtPathToSavedPhotosAlbum(sourcePath,nil,nil,nil);
}
但我如何使用不同的按钮操作保存视频(例如:“保存视频”)?
答案 0 :(得分:1)
完成视频录制或拣选过程后,- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
的{{1}}方法会为您提供视频路径,即临时路径(您的UIImagePickerController
字符串实现)。所以,你要做的是,
1)删除sourcePath
以避免在录制后立即保存视频;
2)将视频文件复制或移动到另一个路径,可能是应用程序沙箱中的UISaveVideoAtPathToSavedPhotosAlbum(sourcePath,nil,nil,nil);
或Documents
目录,并存储/保存该路径;
3)当用户点按“保存”按钮时,使用temp
将视频保存到照片中(UISaveVideoAtPathToSavedPhotosAlbum(videoPath,nil,nil,nil);
是您复制/移动视频文件的路径)。
希望这有帮助!