iOS視訊錄製
- (void)createrVideoRecodr
{
//建立視訊裝置(攝像頭前,後)
NSArray*devices = [AVCaptureDevicedevicesWithMediaType:AVMediaTypeVideo];
//2.初始化一個攝像頭輸入裝置(first是後置攝像頭,last是前置攝像頭)
if(self.sendkBtn.selected==YES) {
self.inputVideo= [AVCaptureDeviceInputdeviceInputWithDevice:[deviceslastObject]error:NULL];
}else{
self.inputVideo= [AVCaptureDeviceInputdeviceInputWithDevice:[devicesfirstObject]error:NULL];
}
//3.建立麥克風裝置
AVCaptureDevice*deviceAudio = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeAudio];
//4.初始化麥克風輸入裝置
AVCaptureDeviceInput*inputAudio = [AVCaptureDeviceInputdeviceInputWithDevice:deviceAudioerror:NULL];
//5.初始化一個movie的檔案輸出
AVCaptureMovieFileOutput*output = [[AVCaptureMovieFileOutputalloc]init];
self.output= output;//儲存output,方便下面操作
//6.初始化一個會話
self.session= [[AVCaptureSessionalloc]init];
//7.將輸入輸出裝置新增到會話中
if([self.sessioncanAddInput:self.inputVideo]) {
[self.sessionaddInput:self.inputVideo];
}
if([self.sessioncanAddInput:inputAudio]) {
[self.sessionaddInput:inputAudio];
}
if([self.sessioncanAddOutput:output]) {
[self.sessionaddOutput:output];
}
//8.建立一個預覽塗層
AVCaptureVideoPreviewLayer*preLayer = [AVCaptureVideoPreviewLayerlayerWithSession:self.session];
//設定圖層的大小
[preLayersetVideoGravity:AVLayerVideoGravityResizeAspectFill];
preLayer.frame=self.view.frame;
preLayer.frame=CGRectMake(0,100,WIDTH,HEIGHT-300);
//新增到view上
[self.view.layeraddSublayer:preLayer];
self.recordkBtn= [UIButtonbuttonWithType:UIButtonTypeCustom];
self.recordkBtn.frame=CGRectMake(50,HEIGHT-100,WIDTH-100,50);
self.recordkBtn.backgroundColor= [UIColorredColor];
[self.recordkBtnsetTitle:@"按下錄製"forState:UIControlStateNormal];
[self.viewaddSubview:self.recordkBtn];
UILongPressGestureRecognizer*guesture = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(recordSpeakBtnPressed:)];
guesture.delegate=self;
[self.sessionstartRunning];
guesture.minimumPressDuration=0.01f;
[self.recordkBtnaddGestureRecognizer:guesture];
}- (void)recordSpeakBtnPressed:(UILongPressGestureRecognizer*)gestureRecognizer {
if(gestureRecognizer.state==UIGestureRecognizerStateBegan) {
[self.sessionstartRunning];
[self.recordkBtnsetTitle:@"鬆開結束"forState:UIControlStateNormal];
}
if(gestureRecognizer.state==UIGestureRecognizerStateEnded) {
[self.sessionstopRunning];
[self.recordkBtnsetTitle:@"按下錄製"forState:UIControlStateNormal];
}
////判斷是否在錄製,如果在錄製,就停止,並設定按鈕title
//if ([self.output isRecording]) {
//[self.output stopRecording];
//[self.recordkBtn setTitle:@"按下錄製" forState:UIControlStateNormal];
//
//return;
//}
////設定按鈕的title
//[self.session startRunning];
//[self.recordkBtn setTitle:@"鬆開結束" forState:UIControlStateNormal];
//設定錄製視訊儲存的路徑
NSString*path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).lastObjectstringByAppendingPathComponent:@"myVidio.mov"];
//轉為視訊儲存的url
NSURL*url = [NSURLfileURLWithPath:path];
//開始錄製,並設定控制器為錄製的代理
[self.outputstartRecordingToOutputFileURL:urlrecordingDelegate:self];
}
- (void)captureOutput:(AVCaptureFileOutput*)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL*)outputFileURL fromConnections:(NSArray*)connections error:(NSError*)error
{
PlayVideoController*playVideo = [[PlayVideoControlleralloc]init];
NSString*str=[outputFileURLabsoluteString];
playVideo.videoUrl= str;//視訊路勁
playVideo.isRecord=YES;
[self.navigationControllerpushViewController:playVideoanimated:YES];
}