iOS使用原生AVCapture系列
概述:
可用于音頻、二維碼、拍照、錄制視頻 (均可自定義界面)
常見(jiàn)的輸出信號(hào):
- AVCaptureAudioDataOutput 音頻輸出
- AVCaptureFileOutput 文本輸出
- AVCaptureMetadataOutput 二維碼 條形碼…
- AVCaptureStillImageOutput 拍照
- AVCaptureMovieFileOutput 錄制視頻(不能實(shí)現(xiàn)暫停錄制和定義視頻文件類型)
- AVCaptureVideoDataOutput + AVCaptureAudioDataOutput 錄制視頻的靈活性更強(qiáng)(能實(shí)現(xiàn)暫停錄制和定義視頻文件類型)
AVCaptureMovieFileOutput輸出流實(shí)現(xiàn)視頻錄制
初始化會(huì)話層
-(void)sessionConfiguration{
//初始化一個(gè)會(huì)話
session = [[AVCaptureSession alloc] init];
[session setSessionPreset:AVCaptureSessionPresetMedium];
//創(chuàng)建視頻設(shè)備
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//根據(jù)設(shè)備創(chuàng)建輸入信號(hào)
deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];
//添加 輸出設(shè)備 movieFile
self.deviceMovieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
[session beginConfiguration];
//session添加設(shè)備輸入信號(hào)
if ([session canAddInput:deviceInput]) {
[session addInput:deviceInput];
}
//session添加設(shè)備輸出信號(hào)
if ([session canAddOutput:self.deviceMovieFileOutput]) {
[session addOutput:self.deviceMovieFileOutput];
}
[session commitConfiguration];
}
創(chuàng)建預(yù)覽圖層
-(void)embedLayerWithView:(UIView *)view{
if (session == nil) {
return;
}
videoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
//設(shè)置圖層的大小
videoPreviewLayer.frame = view.bounds;
videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[view.layer addSublayer:videoPreviewLayer];
[session startRunning];
}
錄制視頻
-(void)takePhoto:(NSURL *)fileURL{
[self.deviceMovieFileOutput startRecordingToOutputFileURL:fileURL recordingDelegate:self];
}
結(jié)束錄制
-(UIImageView *)finishRecord:(UIView *)view isAnewRecording:(BOOL)anewRecording{
gifImageView = [[UIImageView alloc] initWithFrame:view.bounds];
[view addSubview:gifImageView];
isAnewRecording = anewRecording; //存儲(chǔ)是否重新錄制
//停止錄制(停止錄制后做代理方法)
[self.deviceMovieFileOutput stopRecording];
return gifImageView;
}
拍攝視頻保存路徑
+(NSString *)getVideoSaveFilePath{
NSString*documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentPath stringByAppendingPathComponent:@"video.mp4"];
return filePath;
}
會(huì)話層啟動(dòng)和關(guān)閉
-(void)startCamera{
[session startRunning];
}
-(void)stopCamera{
[session stopRunning];
}
代理方法
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error{
NSLog(@"完成錄制");
NSLog(@"outputFileURL = %@",outputFileURL);
//**重新錄制**//
if (isAnewRecording) {
//**刪除視頻文件**//
NSFileManager *manager = [NSFileManager defaultManager];
[manager removeItemAtPath:outputFileURL.absoluteString error:nil];
}
//**不取消錄制**//
else{
//**獲取視頻時(shí)長(zhǎng)**//
AVURLAsset *avUrl = [AVURLAsset URLAssetWithURL:outputFileURL options:nil];
CMTime time = [avUrl duration];
int seconds = ceil(time.value/time.timescale);
NSLog(@"seconds = %d",seconds);
if ([self.delegate respondsToSelector:@selector(videoDuration:)]) {
[self.delegate videoDuration:seconds];
}
if ([self.delegate respondsToSelector:@selector(playerVideo:)]) {
[self.delegate playerVideo:outputFileURL.absoluteString];
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS打開(kāi)照相機(jī)與本地相冊(cè)選擇圖片實(shí)例詳解
這篇文章主要介紹了IOS打開(kāi)照相機(jī)與本地相冊(cè)選擇圖片實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
iOS的客戶端菜單功能仿百度糯米/美團(tuán)二級(jí)菜單
我剛好最近在開(kāi)發(fā)一個(gè)商城項(xiàng)目,實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的控件,控件的效果就是類似百度糯米或者美團(tuán)的二級(jí)菜單,非常不錯(cuò)具有參考借鑒價(jià)值,對(duì)百度糯米 美團(tuán)二級(jí)菜單功能感興趣的朋友一起看看吧2016-11-11
IOS微信端confirm以及alert去掉網(wǎng)址的實(shí)例代碼
下面小編就為大家分享一篇IOS微信端confirm以及alert去掉網(wǎng)址的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
手把手教你實(shí)現(xiàn)微信小視頻iOS代碼實(shí)現(xiàn)
這篇文章主要手把手教你實(shí)現(xiàn)微信小視頻,iOS代碼實(shí)現(xiàn)微信小視頻功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
iOS UITextView 首行縮進(jìn) 撤銷輸入 反撤銷輸入的實(shí)現(xiàn)代碼
本文是腳本之家小編給大家分享的iOS UITextView 首行縮進(jìn) 撤銷輸入 反撤銷輸入的實(shí)現(xiàn)代碼,需要的朋友參考下吧2017-09-09
iOS中定位當(dāng)前位置坐標(biāo)及轉(zhuǎn)換為火星坐標(biāo)的方法
這篇文章主要介紹了iOS中獲取當(dāng)前位置坐標(biāo)及轉(zhuǎn)換為火星坐標(biāo)的方法,這里的火星坐標(biāo)指的是我國(guó)專門研制的一種加密的坐標(biāo)系統(tǒng)...需要的朋友可以參考下2016-02-02
IOS 開(kāi)發(fā)之操作圖庫(kù)自定義控制器
這篇文章主要介紹了IOS 開(kāi)發(fā)之操作圖庫(kù)自定義控制器的相關(guān)資料,需要的朋友可以參考下2017-02-02

