由于項目需求:在聊天中需在添加一個錄制和播放小視頻的功能宫蛆。下面主要講的使用蘋果提供的UIImagePickerController進行視頻的錄制。
第一步:需要在plist文件中添加3個權(quán)限
1、Privacy - Camera Usage Description 使用相機
2逊抡、Privacy - Microphone Usage Description 使用麥克風(fēng)
3荡含、Privacy - Photo Library Usage Description 使用相冊
第二步:導(dǎo)入靜態(tài)庫 如下圖所示
screenshot.png
第三步:代碼
1、需要導(dǎo)入的三個頭文件 并為當(dāng)前控制器添加代理UIImagePickerControllerDelegate,UINavigationControllerDelegate
#import <Photos/Photos.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <AssetsLibrary/AssetsLibrary.h>
2甚垦、需要判斷當(dāng)前設(shè)備是否可以拍攝和擁有拍攝權(quán)限以及對拍攝的一些參數(shù)進行的設(shè)置
UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
//判斷是否可以拍攝
if ( [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//判斷是否擁有拍攝權(quán)限
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
return;
}
//拍攝
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
//錄制的類型 下面為視頻
imagePicker.mediaTypes=@[(NSString*)kUTTypeMovie];
//錄制的時長
imagePicker.videoMaximumDuration=10.0;
//模態(tài)視圖的彈出效果
imagePicker.modalPresentationStyle=UIModalPresentationOverFullScreen;
[self presentViewController:imagePicker animated:YES completion:nil];
}
3茶鹃、視頻錄制完成進入代理方法創(chuàng)建路徑獲得視頻
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
//返回的媒體類型是照片或者視頻
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
//照片的處理
[picker dismissViewControllerAnimated:YES completion:^{
UIImage *img = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}];
}else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]){
//視頻的處理
[picker dismissViewControllerAnimated:YES completion:^() {
//文件管理器
NSFileManager* fm = [NSFileManager defaultManager];
//創(chuàng)建視頻的存放路徑
NSString * path = [NSString stringWithFormat:@"%@/tmp/video%.0f.merge.mp4", NSHomeDirectory(), [NSDate timeIntervalSinceReferenceDate] * 1000];
NSURL *mergeFileURL = [NSURL fileURLWithPath:path];
videoUrl = mergeFileURL;
//通過文件管理器將視頻存放的創(chuàng)建的路徑中
[fm copyItemAtURL:[info objectForKey:UIImagePickerControllerMediaURL] toURL:mergeFileURL error:nil];
AVURLAsset * asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:path]];
//根據(jù)AVURLAsset得出視頻的時長
CMTime time = [asset duration];
int seconds = ceil(time.value/time.timescale);
NSString *videoTime = [NSString stringWithFormat:@"%d",seconds];
//可以根據(jù)需求判斷是否需要將錄制的視頻保存到系統(tǒng)相冊中
// ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// NSURL *recordedVideoURL= [info objectForKey:UIImagePickerControllerMediaURL];
//
// if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:recordedVideoURL]) {
// [library writeVideoAtPathToSavedPhotosAlbum:recordedVideoURL
// completionBlock:^(NSURL *assetURL, NSError *error){
//
// }];
// }
}];
}
}