最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用AVFoundation實(shí)現(xiàn)視頻錄制詳解

 更新時(shí)間:2022年09月05日 08:44:44   作者:reyzhang  
這篇文章主要介紹了使用AVFoundation實(shí)現(xiàn)視頻錄制詳解的相關(guān)資料,需要的朋友可以參考下

一、前言

AVCaptureSession 是 AVFoundation 的核心類,用于管理捕獲對(duì)象 AVCaptureInput 的視頻和音頻的輸入,協(xié)調(diào)捕獲的輸出 AVCaptureOutput。

AVCaptureOutput 的輸出有兩種方法:

  • 一種是直接以 movieFileUrl 方式輸出;
  • 一種是以原始數(shù)據(jù)流 data 的方式輸出

流程對(duì)比圖如下:

下面詳細(xì)講解錄制視頻的方案:

二、AVCaptureSession + AVCaptureMovieFileOutput

1.創(chuàng)建AVCaptureSession

//導(dǎo)入 AVFoundation.framework?
#import <AVFoundation/AVFoundation.h>

//聲明屬性
@property (nonatomic, strong) AVCaptureSession *captureSession;

//懶加載 AVCapturesession
- (AVCaptureSession *)captureSession {
? ? if (!_captureSession) {
? ? ? ? _captureSession = [[AVCaptureSession alloc] init];
? ? ? ??
? ? ? ? //設(shè)置分辨率
? ? ? ? if ([_captureSession canSetSessionPreset:AVCaptureSessionPresetHigh]) {
? ? ? ? ? ? [_captureSession setSessionPreset:AVCaptureSessionPresetHigh];
? ? ? ? }
? ? }
? ? return _captureSession;
}

注意:AVCaptureSession 的調(diào)用是會(huì)阻塞線程的,建議單獨(dú)開(kāi)辟子線程處理。2.設(shè)置音頻、視頻輸入

//聲明屬性
@property (nonatomic, strong) AVCaptureDeviceInput *videoInput;
@property (nonatomic, strong) AVCaptureDeviceInput *audioInput;


//設(shè)置視頻,音頻輸入源
- (void)setCaptureDeviceInput {
? ? //1. 視頻輸入源
? ? //獲取視頻輸入設(shè)備, 默認(rèn)后置攝像頭
? ? AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
? ??
? ? NSError *error = nil;
? ? self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];
? ??
? ? if ([self.captureSession canAddInput:self.videoInput]) {
? ? ? ? [self.captureSession addInput:self.videoInput];
? ? }
? ??
? ??
? ? //2. 音頻輸入源
? ? AVCaptureDevice *audioCaptureDevice = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio] firstObject];
? ? self.audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error];
? ? if ([self.captureSession canAddInput:self.audioInput]) {
? ? ? ? [self.captureSession addInput:self.audioInput];
? ? }
? ??
}

3.設(shè)置文件輸出源

//聲明屬性
@property (nonatomic, strong) AVCaptureMovieFileOutput *movieFileOutput;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;


//設(shè)置文件輸出源
- (void)setDeviceFileOutput {
? ??
? ? //初始化文件輸出對(duì)象
? ? self.movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
? ??
? ? //捕獲會(huì)話中特定捕獲輸入對(duì)象和捕獲輸出對(duì)象之間的連接
? ? AVCaptureConnection *captureConnection = [self.movieFileOutput connectionWithMediaType:AVMediaTypeVideo];
? ??
? ? //設(shè)置防抖
? ? if ([captureConnection isVideoStabilizationSupported]) {
? ? ? ? captureConnection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto;
? ? }
? ??
? ? //預(yù)覽圖層和視頻方向保持一致
? ? captureConnection.videoOrientation = [self.previewLayer connection].videoOrientation;
? ??
? ? //添加文件輸出源
? ? if ([self.captureSession canAddOutput:self.movieFileOutput]) {
? ? ? ? [self.captureSession addOutput:self.movieFileOutput];
? ? }
? ??
}

4.添加視頻預(yù)覽層

- (void)setVideoPreviewLayer {
? ? self.previewLayer.frame = [UIScreen mainScreen].bounds;
? ??
? ? [self.superView.layer addSubLayer:self.previewLayer];
}


- (AVCaptureVideoPreviewLayer *)previewLayer {
? ? if (!_previewLayer) {
? ? ? ? _previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
? ? ? ? _previewLayer.masksToBounds = YES;
? ? ? ? _previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;//填充模式
? ? }
? ? return _previewLayer;
}

5. 開(kāi)始采集

//聲明屬性
@property (nonatomic, strong) dispatch_queue_t sessionQueue;

//開(kāi)始采集
- (void)startCapture {
?? ?self.sessionQueue = dispatch_queue_create("com.capturesession.queue", DISPATCH_QUEUE_CONCURRENT);

? ? if (![self.captureSession isRunning]) {
? ? ? ? __weak __typeof(self) weakSelf = self;
? ? ? ??
? ? ? ? dispatch_async(self.sessionQueue, ^{
? ? ? ? ? ? [weakSelf.captureSession startRunning];
? ? ? ? });
? ? ? ??
? ? }
}

6. 開(kāi)始錄制

//開(kāi)始錄制
- (void)startRecord {
? ??
? ? [self.movieFileOutput startRecordingToOutputFileURL:[self createVideoPath] recordingDelegate:self];
}

當(dāng)實(shí)際的錄制開(kāi)始或停止時(shí),系統(tǒng)會(huì)有代理回調(diào)。當(dāng)開(kāi)始錄制之后,這時(shí)可能還沒(méi)有真正寫入,真正開(kāi)始寫入會(huì)回調(diào)下面代理,停止錄制也是如此,所以如果你需要對(duì)錄制視頻起始點(diǎn)操作,建議通過(guò)系統(tǒng)的回調(diào)代理:

//實(shí)現(xiàn)協(xié)議 <AVCaptureFileOutputRecordingDelegate>中的方法


#pragma mark _ AVCaptureFileOutputRecordingDelegate

//起始點(diǎn) - 開(kāi)始錄制
- (void)captureOutput:(AVCaptureFileOutput *)output didStartRecordingToOutputFileAtURL:(NSURL *)fileURL fromConnections:(NSArray<AVCaptureConnection *> *)connections {
? ??
}

//結(jié)束錄制
-(void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
{
? ? NSLog(@"視頻錄制完成. 文件路徑:%@",[outputFileURL absoluteString]);
}

7.停止錄制

//停止錄制
- (void)stopRecord {
? ? if ([self.movieFileOutput isRecording]) {
? ? ? ? [self.movieFileOutput stopRecording];
? ? }
}

8.停止采集

//停止采集
- (void)stopCapture {
? ? if ([self.captureSession isRunning]) {
? ? ? ? __weak __typeof(self) weakSelf = self;
? ? ? ? dispatch_async(self.sessionQueue, ^{
? ? ? ? ? ? [weakSelf.captureSession stopRunning];
? ? ? ? ? ? weakSelf.captureSession = nil;
? ? ? ? });
? ? }
}

到此這篇關(guān)于使用AVFoundation實(shí)現(xiàn)視頻錄制詳解的文章就介紹到這了,更多相關(guān)AVFoundation實(shí)現(xiàn)視頻錄制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • iOS開(kāi)發(fā)技巧之自定義相機(jī)

    iOS開(kāi)發(fā)技巧之自定義相機(jī)

    這篇文章主要為大家詳細(xì)介紹了iOS開(kāi)發(fā)技巧之自定義相機(jī),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • 分享一個(gè)關(guān)于Storyboard 跳轉(zhuǎn)與傳值

    分享一個(gè)關(guān)于Storyboard 跳轉(zhuǎn)與傳值

    近日不忙,給大家分享一個(gè)關(guān)于storyboard跳轉(zhuǎn)傳值的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2015-12-12
  • iOS中valueForKeyPath的常用方法法示例

    iOS中valueForKeyPath的常用方法法示例

    這篇文章主要給大家介紹了關(guān)于iOS中valueForKeyPath的常用方法法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • iOS圖片壓縮、濾鏡、剪切及渲染等詳解

    iOS圖片壓縮、濾鏡、剪切及渲染等詳解

    這篇文章主要給大家介紹了關(guān)于iOS圖片壓縮、濾鏡、剪切及渲染等的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • 設(shè)計(jì)模式中的迭代器模式在Cocoa Touch框架中的使用

    設(shè)計(jì)模式中的迭代器模式在Cocoa Touch框架中的使用

    這篇文章主要介紹了設(shè)計(jì)模式中的迭代器模式在Cocoa Touch框架中的使用,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2016-03-03
  • Objective-C中關(guān)于實(shí)例所占內(nèi)存的大小詳解

    Objective-C中關(guān)于實(shí)例所占內(nèi)存的大小詳解

    這篇文章主要給大家介紹了關(guān)于Objective-C中實(shí)例所占內(nèi)存的大小的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位iOS開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • iOS UITableView 與 UITableViewController實(shí)例詳解

    iOS UITableView 與 UITableViewController實(shí)例詳解

    這篇文章主要介紹了iOS UITableView 與 UITableViewController實(shí)例詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-09-09
  • iOS自帶動(dòng)畫效果的實(shí)例代碼

    iOS自帶動(dòng)畫效果的實(shí)例代碼

    本文給大家分享ios自帶動(dòng)畫效果的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2016-12-12
  • iOS仿AirPods彈出動(dòng)畫

    iOS仿AirPods彈出動(dòng)畫

    這篇文章主要為大家詳細(xì)介紹了iOS仿AirPods彈出動(dòng)畫的實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • iOS 控制器自定義動(dòng)畫跳轉(zhuǎn)方法(模態(tài)跳轉(zhuǎn))

    iOS 控制器自定義動(dòng)畫跳轉(zhuǎn)方法(模態(tài)跳轉(zhuǎn))

    下面小編就為大家分享一篇iOS 控制器自定義動(dòng)畫跳轉(zhuǎn)方法(模態(tài)跳轉(zhuǎn)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01

最新評(píng)論

开阳县| 吴川市| 祥云县| 卢龙县| 鄢陵县| 鹿泉市| 定日县| 海安县| 句容市| 通海县| 垣曲县| 横峰县| 东兴市| 长治县| 阿城市| 沂源县| 理塘县| 从江县| 竹山县| 潼南县| 隆回县| 始兴县| 邻水| 肇庆市| 开平市| 姜堰市| 达尔| 阜新市| 临清市| 毕节市| 东方市| 城口县| 崇阳县| 霍州市| 罗田县| 淳安县| 精河县| 楚雄市| 墨江| 古蔺县| 苍山县|