AVFoundation 第一步,拿到我想要的數(shù)據(jù)

AVCaptureDevice

枚舉說明部分
AVCaptureDeviceType 枚舉說明
AVCaptureDeviceTypeBuiltInMicrophone 一個內(nèi)置的麥克風(fēng)
AVCaptureDeviceTypeBuiltInWideAngleCamera 內(nèi)置廣角相機,這些裝置適用于一般用途
AVCaptureDeviceTypeBuiltInTelephotoCamera 內(nèi)置長焦相機冤留,比廣角相機的焦距長。這種類型只是將窄角設(shè)備與配備兩種類型的攝像機的硬件上的寬角設(shè)備區(qū)分開來
AVCaptureDeviceTypeBuiltInDualCamera 廣角相機和長焦相機的組合遮婶,創(chuàng)建了一個拍照 (常用類型)
AVCaptureDeviceTypeBuiltInTelephotoCamera 創(chuàng)建比廣角相機更長的焦距剥险。只有使用 AVCaptureDeviceDiscoverySession 可以使用
AVCaptureDeviceTypeBuiltInDuoCamera iOS 10.2 被 AVCaptureDeviceTypeBuiltInDualCamera 替換

接下來是AVMediaType的枚舉說明.未補充完整后續(xù)查閱資料來補齊.

AVMediaType 枚舉說明
AVMediaTypeVideo 視頻
AVMediaTypeAudio 音頻
AVMediaTypeText 文本
AVMediaTypeClosedCaption 電影
AVMediaTypeSubtitle AVMediaTypeSubtitle
AVMediaTypeTimecode AVMediaTypeTimecode
AVMediaTypeMetadata AVMediaTypeMetadata
AVMediaTypeMuxed 音頻 + 視頻

還有一個攝像頭的枚舉部分.

AVCaptureDevicePosition 枚舉說明
AVCaptureDevicePositionUnspecified 未指定的設(shè)備
AVCaptureDevicePositionBack 后攝像頭 手機屏幕背面的攝像頭
AVCaptureDevicePositionFront 前攝像頭 手機屏幕上邊的攝像頭

攝像頭的權(quán)限的枚舉說明

AVAuthorizationStatus 攝像頭權(quán)限說明
AVAuthorizationStatusNotDetermined 用戶暫時沒有授權(quán)
AVAuthorizationStatusRestricted 沒有改變媒體類型
AVAuthorizationStatusDenied 用戶允許
AVAuthorizationStatusAuthorized 用戶 拒絕

思路 : 拿到攝像頭->開啟攝像頭->捕捉畫面->拿到數(shù)據(jù) 下面開始寫代碼

//
//  XJCaptureSesion.m
//  AVFoundation
//
//  Created by 張孝江 on 2021/4/1.
//

#import "XJCaptureManager.h"
#import <AVFoundation/AVFoundation.h>


@interface XJCaptureManager()<AVCaptureVideoDataOutputSampleBufferDelegate>
/**最主要的一個類**/
@property (nonatomic,strong) AVCaptureSession *session;
@property (nonatomic,strong) dispatch_queue_t sessionQueue;        //線程的隊列

/*設(shè)備的抽象的類  輸入*/
@property (nonatomic, strong) AVCaptureDeviceInput *frontCamera;
@property (nonatomic, strong) AVCaptureDeviceInput *backCamera;
@property (nonatomic, weak) AVCaptureDeviceInput   *currentCamera;
/**抽象類,  輸出**/
@property (nonatomic,strong) AVCaptureVideoDataOutput *videoDataOutput;  // 這個是視頻的輸出的類.
@property (nonatomic, strong) AVCaptureConnection *videoConnection;
/**抽象類預(yù)覽測**/
@property (nonatomic,strong) AVCaptureVideoPreviewLayer *preLayer;       //預(yù)覽層

@end

@implementation XJCaptureManager

-(instancetype)init{
    if (self = [super init]) {
        [self initVideo];
    }
    return self;
}

-(void)startLive{
    [self.session startRunning];
}

#pragma mark - 開始捕捉..
-(void)initVideo{
    /**第一步獲取攝像頭**/
    AVCaptureDevice * front = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInWideAngleCamera mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionFront];
    self.frontCamera = [AVCaptureDeviceInput deviceInputWithDevice: front error:NULL];
    AVCaptureDevice *back = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInWideAngleCamera mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack];
    self.backCamera = [AVCaptureDeviceInput deviceInputWithDevice:back error:NULL];
    //設(shè)置默認選項為前置攝像頭
    self.currentCamera = self.frontCamera;
    //設(shè)置視頻的輸出..
    [self.videoDataOutput setSampleBufferDelegate:self queue:self.sessionQueue];
    //會丟失處理不過來的幀數(shù)據(jù).
    [self.videoDataOutput setAlwaysDiscardsLateVideoFrames:YES];
    //設(shè)置輸出格式..
    NSDictionary *dictionary = @{
        (__bridge  NSString *)kCVPixelBufferPixelFormatTypeKey:@(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)
    };
    //當(dāng)你不知所錯的時候 記得點開看一下
    /*
     On iOS, the only supported key is kCVPixelBufferPixelFormatTypeKey. Supported pixel formats are kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, kCVPixelFormatType_420YpCbCr8BiPlanarFullRange and kCVPixelFormatType_32BGRA.
     */
    [self.videoDataOutput setVideoSettings:dictionary];
    //配置輸入
    [self.session beginConfiguration];
    
    if ([self.session canAddInput:self.currentCamera]) {
        [self.session addInput:self.currentCamera];
    }
    //配置輸出
    if ([self.session canAddOutput:self.videoDataOutput]) {
        [self.session addOutput:self.videoDataOutput];
    }
    //設(shè)置分辨率
    if ([self.session canSetSessionPreset:AVCaptureSessionPreset1920x1080]) {
        self.session.sessionPreset = AVCaptureSessionPreset1920x1080;
    }else if ([self.session canSetSessionPreset:AVCaptureSessionPreset1280x720]){
        self.session.sessionPreset = AVCaptureSessionPreset1280x720;
    }else if ([self.session canSetSessionPreset:AVCaptureSessionPreset640x480]){
        self.session.sessionPreset = AVCaptureSessionPreset640x480;
    }else{
        self.session.sessionPreset = AVCaptureSessionPreset352x288;
    }
    //結(jié)尾
    [self.session commitConfiguration];
    self.videoConnection  = [self.videoDataOutput connectionWithMediaType:AVMediaTypeVideo];
    //設(shè)置視頻的輸出方向
    self.videoConnection.videoOrientation =  AVCaptureVideoOrientationPortrait;
    //設(shè)置分辨率..
    [self setUpdataFps:30];
    //設(shè)置預(yù)覽層...
    [self.preview.layer addSublayer:self.preLayer];
}

#pragma mark - 根據(jù)代理取獲取的文件的地址. 這就是當(dāng)前獲取的樣本...
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection{
    NSLog(@"%@",sampleBuffer);
}




#pragma mark - 設(shè)置分辨率......
-(void)setUpdataFps:(NSInteger)fps{
    AVCaptureDevice *front = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInWideAngleCamera mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionFront];
    AVCaptureDevice *back = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInWideAngleCamera mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack];
    NSArray *array = @[front,back];
    
    for (AVCaptureDevice *device in array) {
        //獲取當(dāng)前支持的最大的FPS
        Float64 a = device.activeFormat.videoSupportedFrameRateRanges.firstObject.maxFrameRate;
        if (a >= fps) {
            if ([device lockForConfiguration:NULL]) {
                device.activeVideoMinFrameDuration = CMTimeMake(10, (int)(fps * 10));
                device.activeVideoMaxFrameDuration = device.activeVideoMinFrameDuration;
                [device unlockForConfiguration];
            }
        }
    }
}

-(void)dealloc{
    NSLog(@"我銷毀了");
}



/*簡單的初始化*/
-(AVCaptureSession *)session{
    if (!_session) {
        _session = [[AVCaptureSession alloc]init];
    }
    return _session;
}

/*初始化輸出的類*/
-(AVCaptureVideoDataOutput *)videoDataOutput{
    if (!_videoDataOutput) {
        _videoDataOutput = [[AVCaptureVideoDataOutput alloc]init];
    }
    return _videoDataOutput;
}
-(dispatch_queue_t)sessionQueue{
    if (!_sessionQueue) {
        _sessionQueue = dispatch_queue_create("XJ.SESSION", NULL);
    }
    return _sessionQueue;
}

-(AVCaptureVideoPreviewLayer *)preLayer{
    if (!_preLayer) {
        _preLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
        _preLayer.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        _preLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    }
    return _preLayer;
}
-(UIView *)preview{
    if (!_preview) {
        _preview = [[UIView alloc] init];
    }
    return _preview;
}


@end

在代理回調(diào)的地方就可以拿到這個視頻的數(shù)據(jù)..

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末需曾,一起剝皮案震驚了整個濱河市够傍,隨后出現(xiàn)的幾起案子形娇,更是在濱河造成了極大的恐慌,老刑警劉巖示姿,帶你破解...
    沈念sama閱讀 218,640評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件甜橱,死亡現(xiàn)場離奇詭異,居然都是意外死亡栈戳,警方通過查閱死者的電腦和手機岂傲,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來荧琼,“玉大人譬胎,你說我怎么就攤上這事差牛∶” “怎么了?”我有些...
    開封第一講書人閱讀 165,011評論 0 355
  • 文/不壞的土叔 我叫張陵偏化,是天一觀的道長脐恩。 經(jīng)常有香客問我,道長侦讨,這世上最難降的妖魔是什么驶冒? 我笑而不...
    開封第一講書人閱讀 58,755評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮韵卤,結(jié)果婚禮上骗污,老公的妹妹穿的比我還像新娘。我一直安慰自己沈条,他們只是感情好需忿,可當(dāng)我...
    茶點故事閱讀 67,774評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蜡歹,像睡著了一般屋厘。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上月而,一...
    開封第一講書人閱讀 51,610評論 1 305
  • 那天汗洒,我揣著相機與錄音,去河邊找鬼父款。 笑死溢谤,一個胖子當(dāng)著我的面吹牛瞻凤,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播世杀,決...
    沈念sama閱讀 40,352評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼鲫构,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了玫坛?” 一聲冷哼從身側(cè)響起结笨,我...
    開封第一講書人閱讀 39,257評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎湿镀,沒想到半個月后炕吸,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,717評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡勉痴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,894評論 3 336
  • 正文 我和宋清朗相戀三年赫模,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蒸矛。...
    茶點故事閱讀 40,021評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡瀑罗,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出雏掠,到底是詐尸還是另有隱情斩祭,我是刑警寧澤,帶...
    沈念sama閱讀 35,735評論 5 346
  • 正文 年R本政府宣布乡话,位于F島的核電站摧玫,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏绑青。R本人自食惡果不足惜诬像,卻給世界環(huán)境...
    茶點故事閱讀 41,354評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望闸婴。 院中可真熱鬧坏挠,春花似錦、人聲如沸邪乍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,936評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽溺欧。三九已至喊熟,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間姐刁,已是汗流浹背芥牌。 一陣腳步聲響...
    開封第一講書人閱讀 33,054評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留聂使,地道東北人壁拉。 一個月前我還...
    沈念sama閱讀 48,224評論 3 371
  • 正文 我出身青樓谬俄,卻偏偏與公主長得像,于是被迫代替她去往敵國和親弃理。 傳聞我的和親對象是個殘疾皇子溃论,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,974評論 2 355

推薦閱讀更多精彩內(nèi)容