- (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];
}