- (void)createrVideoRecodr
{
//創(chuàng)建視頻設(shè)備(攝像頭前勺远,后)
NSArray*devices = [AVCaptureDevicedevicesWithMediaType:AVMediaTypeVideo];
//2.初始化一個(gè)攝像頭輸入設(shè)備(first是后置攝像頭,last是前置攝像頭)
if(self.sendkBtn.selected==YES) {
self.inputVideo= [AVCaptureDeviceInputdeviceInputWithDevice:[deviceslastObject]error:NULL];
}else{
self.inputVideo= [AVCaptureDeviceInputdeviceInputWithDevice:[devicesfirstObject]error:NULL];
}
//3.創(chuàng)建麥克風(fēng)設(shè)備
AVCaptureDevice*deviceAudio = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeAudio];
//4.初始化麥克風(fēng)輸入設(shè)備
AVCaptureDeviceInput*inputAudio = [AVCaptureDeviceInputdeviceInputWithDevice:deviceAudioerror:NULL];
//5.初始化一個(gè)movie的文件輸出
AVCaptureMovieFileOutput*output = [[AVCaptureMovieFileOutputalloc]init];
self.output= output;//保存output钞护,方便下面操作
//6.初始化一個(gè)會(huì)話
self.session= [[AVCaptureSessionalloc]init];
//7.將輸入輸出設(shè)備添加到會(huì)話中
if([self.sessioncanAddInput:self.inputVideo]) {
[self.sessionaddInput:self.inputVideo];
}
if([self.sessioncanAddInput:inputAudio]) {
[self.sessionaddInput:inputAudio];
}
if([self.sessioncanAddOutput:output]) {
[self.sessionaddOutput:output];
}
//8.創(chuàng)建一個(gè)預(yù)覽涂層
AVCaptureVideoPreviewLayer*preLayer = [AVCaptureVideoPreviewLayerlayerWithSession:self.session];
//設(shè)置圖層的大小
[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:@"松開結(jié)束"forState:UIControlStateNormal];
}
if(gestureRecognizer.state==UIGestureRecognizerStateEnded) {
[self.sessionstopRunning];
[self.recordkBtnsetTitle:@"按下錄制"forState:UIControlStateNormal];
}
////判斷是否在錄制,如果在錄制挡逼,就停止,并設(shè)置按鈕title
//if ([self.output isRecording]) {
//[self.output stopRecording];
//[self.recordkBtn setTitle:@"按下錄制" forState:UIControlStateNormal];
//
//return;
//}
////設(shè)置按鈕的title
//[self.session startRunning];
//[self.recordkBtn setTitle:@"松開結(jié)束" forState:UIControlStateNormal];
//設(shè)置錄制視頻保存的路徑
NSString*path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).lastObjectstringByAppendingPathComponent:@"myVidio.mov"];
//轉(zhuǎn)為視頻保存的url
NSURL*url = [NSURLfileURLWithPath:path];
//開始錄制,并設(shè)置控制器為錄制的代理
[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];
}