生活随笔
收集整理的這篇文章主要介紹了
iOS:app直播---采集篇
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
【如何快速的開發(fā)一個(gè)完整的iOS直播app】(采集篇)
轉(zhuǎn)載自簡(jiǎn)書@袁崢Seemygo:http://www.jianshu.com/p/c71bfda055fa 前言
開發(fā)一款直播app,首先需要采集主播的視頻和音頻,然后傳入流媒體服務(wù)器,本篇主要講解如何采集主播的視頻和音頻,當(dāng)前可以切換前置后置攝像頭和焦點(diǎn)坐標(biāo)。
基本知識(shí)介紹
- AVCaptureOutput:硬件輸出對(duì)象,用于接收各類輸出數(shù)據(jù),通常使用對(duì)應(yīng)的子類AVCaptureAudioDataOutput(聲音數(shù)據(jù)輸出對(duì)象)、 AVCaptureVideoDataOutput(視頻數(shù)據(jù)輸出對(duì)象)
- AVCaptureConnection:當(dāng)把一個(gè)輸入和輸出添加到AVCaptureSession之后,AVCaptureSession就會(huì)在輸入、輸出設(shè)備之間建立連接,而且通過(guò)AVCaptureOutput可以獲取這個(gè)連接對(duì)象。
- AVCaptureVideoPreviewLayer:相機(jī)拍攝預(yù)覽圖層,能實(shí)時(shí)查看拍照或視頻錄制效果,創(chuàng)建該對(duì)象需要指定對(duì)應(yīng)的AVCaptureSession對(duì)象,因?yàn)锳VCaptureSession包含視頻輸入數(shù)據(jù),有視頻數(shù)據(jù)才能展示。
- AVCaptureSession:協(xié)調(diào)輸入和輸出之間傳遞數(shù)據(jù)?
- 系統(tǒng)作用:可以操作硬件設(shè)備
- 工作原理:讓App與系統(tǒng)之間產(chǎn)生一個(gè)捕獲會(huì)話,相當(dāng)于App與硬件設(shè)備有聯(lián)系了, 我們只需要把硬件輸入對(duì)象和輸出對(duì)象添加到會(huì)話中,會(huì)話就會(huì)自動(dòng)把硬件輸入對(duì)象和輸出產(chǎn)生連接,這樣硬件輸入與輸出設(shè)備就能傳輸音視頻數(shù)據(jù)。
- 現(xiàn)實(shí)生活場(chǎng)景:租客(輸入錢),中介(會(huì)話),房東(輸出房),租客和房東都在中介登記,中介就會(huì)讓租客與房東之間產(chǎn)生聯(lián)系,以后租客就能直接和房東聯(lián)系了。
捕獲音視頻步驟:官方文檔
- 1.創(chuàng)建AVCaptureSession對(duì)象
- 2.獲取AVCaptureDevicel錄像設(shè)備(攝像頭),錄音設(shè)備(麥克風(fēng)),注意不具備輸入數(shù)據(jù)功能,只是用來(lái)調(diào)節(jié)硬件設(shè)備的配置。
- 3.根據(jù)音頻/視頻硬件設(shè)備(AVCaptureDevice)創(chuàng)建音頻/視頻硬件輸入數(shù)據(jù)對(duì)象(AVCaptureDeviceInput),專門管理數(shù)據(jù)輸入。
- 4.創(chuàng)建視頻輸出數(shù)據(jù)管理對(duì)象(AVCaptureVideoDataOutput),并且設(shè)置樣品緩存代理(setSampleBufferDelegate)就可以通過(guò)它拿到采集到的視頻數(shù)據(jù)
- 5.創(chuàng)建音頻輸出數(shù)據(jù)管理對(duì)象(AVCaptureAudioDataOutput),并且設(shè)置樣品緩存代理(setSampleBufferDelegate)就可以通過(guò)它拿到采集到的音頻數(shù)據(jù)
- 6.將數(shù)據(jù)輸入對(duì)象AVCaptureDeviceInput、數(shù)據(jù)輸出對(duì)象AVCaptureOutput添加到媒體會(huì)話管理對(duì)象AVCaptureSession中,就會(huì)自動(dòng)讓音頻輸入與輸出和視頻輸入與輸出產(chǎn)生連接.
- 7.創(chuàng)建視頻預(yù)覽圖層AVCaptureVideoPreviewLayer并指定媒體會(huì)話,添加圖層到顯示容器layer中
- 8.啟動(dòng)AVCaptureSession,只有開啟,才會(huì)開始輸入到輸出數(shù)據(jù)流傳輸。
// 捕獲音視頻
- (
void)setupCaputureVideo
{// 1.創(chuàng)建捕獲會(huì)話,必須要強(qiáng)引用,否則會(huì)被釋放AVCaptureSession *captureSession =
[[AVCaptureSession alloc] init];_captureSession =
captureSession;// 2.獲取攝像頭設(shè)備,默認(rèn)是后置攝像頭AVCaptureDevice *videoDevice =
[self getVideoDevice:AVCaptureDevicePositionFront];// 3.獲取聲音設(shè)備AVCaptureDevice *audioDevice =
[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];// 4.創(chuàng)建對(duì)應(yīng)視頻設(shè)備輸入對(duì)象AVCaptureDeviceInput *videoDeviceInput =
[AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];_currentVideoDeviceInput =
videoDeviceInput;// 5.創(chuàng)建對(duì)應(yīng)音頻設(shè)備輸入對(duì)象AVCaptureDeviceInput *audioDeviceInput =
[AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];// 6.添加到會(huì)話中// 注意“最好要判斷是否能添加輸入,會(huì)話不能添加空的// 6.1 添加視頻if ([captureSession canAddInput:videoDeviceInput]) {[captureSession addInput:videoDeviceInput];}// 6.2 添加音頻if ([captureSession canAddInput:audioDeviceInput]) {[captureSession addInput:audioDeviceInput];}// 7.獲取視頻數(shù)據(jù)輸出設(shè)備AVCaptureVideoDataOutput *videoOutput =
[[AVCaptureVideoDataOutput alloc] init];// 7.1 設(shè)置代理,捕獲視頻樣品數(shù)據(jù)// 注意:隊(duì)列必須是串行隊(duì)列,才能獲取到數(shù)據(jù),而且不能為空dispatch_queue_t videoQueue = dispatch_queue_create(
"Video Capture Queue", DISPATCH_QUEUE_SERIAL);[videoOutput setSampleBufferDelegate:self queue:videoQueue];if ([captureSession canAddOutput:videoOutput]) {[captureSession addOutput:videoOutput];}// 8.獲取音頻數(shù)據(jù)輸出設(shè)備AVCaptureAudioDataOutput *audioOutput =
[[AVCaptureAudioDataOutput alloc] init];// 8.2 設(shè)置代理,捕獲視頻樣品數(shù)據(jù)// 注意:隊(duì)列必須是串行隊(duì)列,才能獲取到數(shù)據(jù),而且不能為空dispatch_queue_t audioQueue = dispatch_queue_create(
"Audio Capture Queue", DISPATCH_QUEUE_SERIAL);[audioOutput setSampleBufferDelegate:self queue:audioQueue];if ([captureSession canAddOutput:audioOutput]) {[captureSession addOutput:audioOutput];}// 9.獲取視頻輸入與輸出連接,用于分辨音視頻數(shù)據(jù)_videoConnection =
[videoOutput connectionWithMediaType:AVMediaTypeVideo];// 10.添加視頻預(yù)覽圖層AVCaptureVideoPreviewLayer *previedLayer =
[AVCaptureVideoPreviewLayer layerWithSession:captureSession];previedLayer.frame =
[UIScreen mainScreen].bounds;[self.view.layer insertSublayer:previedLayer atIndex:0];_previedLayer =
previedLayer;// 11.啟動(dòng)會(huì)話
[captureSession startRunning];
}// 指定攝像頭方向獲取攝像頭
- (AVCaptureDevice *
)getVideoDevice:(AVCaptureDevicePosition)position
{NSArray *devices =
[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];for (AVCaptureDevice *device
in devices) {if (device.position ==
position) {return device;}}return nil;
}#pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate
// 獲取輸入設(shè)備數(shù)據(jù),有可能是音頻有可能是視頻
- (
void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *
)connection
{if (_videoConnection ==
connection) {NSLog(@"采集到視頻數(shù)據(jù)");} else {NSLog(@"采集到音頻數(shù)據(jù)");}
} 視頻采集額外功能一(切換攝像頭)
- 切換攝像頭步驟
- 1.獲取當(dāng)前視頻設(shè)備輸入對(duì)象
- 2.判斷當(dāng)前視頻設(shè)備是前置還是后置
- 3.確定切換攝像頭的方向
- 4.根據(jù)攝像頭方向獲取對(duì)應(yīng)的攝像頭設(shè)備
- 5.創(chuàng)建對(duì)應(yīng)的攝像頭輸入對(duì)象
- 6.從會(huì)話中移除之前的視頻輸入對(duì)象
- 7.添加新的視頻輸入對(duì)象到會(huì)話中
// 切換攝像頭
- (IBAction)toggleCapture:(
id)sender {// 獲取當(dāng)前設(shè)備方向AVCaptureDevicePosition curPosition =
_currentVideoDeviceInput.device.position;// 獲取需要改變的方向AVCaptureDevicePosition togglePosition = curPosition == AVCaptureDevicePositionFront?
AVCaptureDevicePositionBack:AVCaptureDevicePositionFront;// 獲取改變的攝像頭設(shè)備AVCaptureDevice *toggleDevice =
[self getVideoDevice:togglePosition];// 獲取改變的攝像頭輸入設(shè)備AVCaptureDeviceInput *toggleDeviceInput =
[AVCaptureDeviceInput deviceInputWithDevice:toggleDevice error:nil];// 移除之前攝像頭輸入設(shè)備
[_captureSession removeInput:_currentVideoDeviceInput];// 添加新的攝像頭輸入設(shè)備
[_captureSession addInput:toggleDeviceInput];// 記錄當(dāng)前攝像頭輸入設(shè)備_currentVideoDeviceInput =
toggleDeviceInput;} 視頻采集額外功能二(聚焦光標(biāo))
- 聚焦光標(biāo)步驟
- 1.監(jiān)聽屏幕的點(diǎn)擊
- 2.獲取點(diǎn)擊的點(diǎn)位置,轉(zhuǎn)換為攝像頭上的點(diǎn),必須通過(guò)視頻預(yù)覽圖層(AVCaptureVideoPreviewLayer)轉(zhuǎn)
- 3.設(shè)置聚焦光標(biāo)圖片的位置,并做動(dòng)畫
- 4.設(shè)置攝像頭設(shè)備聚焦模式和曝光模式(注意:這里設(shè)置一定要鎖定配置lockForConfiguration,否則報(bào)錯(cuò))
// 點(diǎn)擊屏幕,出現(xiàn)聚焦視圖
- (
void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)
event
{// 獲取點(diǎn)擊位置UITouch *touch =
[touches anyObject];CGPoint point =
[touch locationInView:self.view];// 把當(dāng)前位置轉(zhuǎn)換為攝像頭點(diǎn)上的位置CGPoint cameraPoint =
[_previedLayer captureDevicePointOfInterestForPoint:point];// 設(shè)置聚焦點(diǎn)光標(biāo)位置
[self setFocusCursorWithPoint:point];// 設(shè)置聚焦
[self focusWithMode:AVCaptureFocusModeAutoFocus exposureMode:AVCaptureExposureModeAutoExpose atPoint:cameraPoint];
}/*** 設(shè)置聚焦光標(biāo)位置** @param point 光標(biāo)位置*/
-(
void)setFocusCursorWithPoint:(CGPoint)point{self.focusCursorImageView.center=
point;self.focusCursorImageView.transform=CGAffineTransformMakeScale(
1.5,
1.5);self.focusCursorImageView.alpha=
1.0;[UIView animateWithDuration:1.0 animations:^
{self.focusCursorImageView.transform=
CGAffineTransformIdentity;} completion:^
(BOOL finished) {self.focusCursorImageView.alpha=
0;}];
}/*** 設(shè)置聚焦*/
-(
void)focusWithMode:(AVCaptureFocusMode)focusMode exposureMode:(AVCaptureExposureMode)exposureMode atPoint:(CGPoint)point{AVCaptureDevice *captureDevice =
_currentVideoDeviceInput.device;// 鎖定配置
[captureDevice lockForConfiguration:nil];// 設(shè)置聚焦if ([captureDevice isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {[captureDevice setFocusMode:AVCaptureFocusModeAutoFocus];}if ([captureDevice isFocusPointOfInterestSupported]) {[captureDevice setFocusPointOfInterest:point];}// 設(shè)置曝光if ([captureDevice isExposureModeSupported:AVCaptureExposureModeAutoExpose]) {[captureDevice setExposureMode:AVCaptureExposureModeAutoExpose];}if ([captureDevice isExposurePointOfInterestSupported]) {[captureDevice setExposurePointOfInterest:point];}// 解鎖配置
[captureDevice unlockForConfiguration];
} ?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/XYQ-208910/p/5859337.html
總結(jié)
以上是生活随笔為你收集整理的iOS:app直播---采集篇的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。