iOS使用原生AVCapture系列
概述:
可用于音頻、二維碼、拍照、錄制視頻 (均可自定義界面)
常見(jiàn)的輸出信號(hào):
- AVCaptureAudioDataOutput 音頻輸出
- AVCaptureFileOutput 文本輸出
- AVCaptureMetadataOutput 二維碼 條形碼…
- AVCaptureStillImageOutput 拍照
- AVCaptureMovieFileOutput 錄制視頻(不能實(shí)現(xiàn)暫停錄制和定義視頻文件類(lèi)型)
- AVCaptureVideoDataOutput + AVCaptureAudioDataOutput 錄制視頻的靈活性更強(qiáng)(能實(shí)現(xiàn)暫停錄制和定義視頻文件類(lèi)型)
AVCaptureMovieFileOutput輸出流實(shí)現(xiàn)視頻錄制
初始化會(huì)話(huà)層
-(void)sessionConfiguration{
//初始化一個(gè)會(huì)話(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ì)話(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)文章
手把手教你實(shí)現(xiàn)微信小視頻iOS代碼實(shí)現(xiàn)
這篇文章主要手把手教你實(shí)現(xiàn)微信小視頻,iOS代碼實(shí)現(xiàn)微信小視頻功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
iOS開(kāi)發(fā)KVO實(shí)現(xiàn)細(xì)節(jié)解密
這篇文章主要為大家介紹了iOS開(kāi)發(fā)KVO實(shí)現(xiàn)細(xì)節(jié)解密,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
關(guān)于iOS GangSDK的使用 為App快速集成社群公會(huì)模塊
這篇文章主要介紹了iOS GangSDK的使用為App快速集成社群公會(huì)模塊功能的實(shí)現(xiàn)過(guò)程。2017-11-11
iOS實(shí)現(xiàn)帶有縮放效果的自動(dòng)輪播圖
這篇文章主要為大家詳細(xì)介紹了iOS帶有縮放效果的自動(dòng)輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
iOS swift實(shí)現(xiàn)轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的方法示例
在平時(shí)的iOS開(kāi)發(fā)中,我們進(jìn)行界面跳轉(zhuǎn)時(shí)一般都是采用系統(tǒng)默認(rèn)的轉(zhuǎn)場(chǎng)動(dòng)畫(huà),而下面這篇文章主要給大家介紹了關(guān)于iOS利用swift實(shí)現(xiàn)轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-07-07
詳解iOS App中UISwitch開(kāi)關(guān)組件的基本創(chuàng)建及使用方法
UISwitch組件就是我們平時(shí)在iOS設(shè)置菜單中開(kāi)到的那種左右滑動(dòng)的開(kāi)關(guān)按鈕,當(dāng)然我們?cè)陂_(kāi)發(fā)時(shí)可以進(jìn)行更多的自定義,這里我們就來(lái)詳解iOS App中UISwitch開(kāi)關(guān)組件的基本創(chuàng)建及使用方法2016-05-05
iOS通過(guò)block在兩個(gè)頁(yè)面間傳值的方法
不知道大家有沒(méi)有發(fā)現(xiàn),在實(shí)際開(kāi)發(fā)中使用block的地方特別多,block比delegate和notification有著更簡(jiǎn)潔的優(yōu)勢(shì),下面這篇文章我們來(lái)簡(jiǎn)單了解一下block在兩個(gè)頁(yè)面之間的傳值。有需要的朋友們可以參考借鑒,下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11
iOS中使用JSPatch框架使Objective-C與JavaScript代碼交互
有了JSPatch,我們便可以在iOS App開(kāi)發(fā)中令JavaScript代碼調(diào)用原生的Objective-C屬性和方法等,下面就來(lái)詳細(xì)看一下如何在iOS中使用JSPatch框架使Objective-C與JavaScript代碼交互2016-06-06
iOS中實(shí)現(xiàn)檢測(cè)Zoombie對(duì)象的具體方法
這篇文章主要給大家介紹了關(guān)于iOS中實(shí)現(xiàn)檢測(cè)Zoombie對(duì)象的具體方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01

