您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

零基礎學 iOS 直播之採集篇

編輯:iOS 開發綜合

直播的採集依設備(攝像頭、話筒)不同,分為視訊採集與音訊採集,本文將分別介紹。

1. 採集步驟

  1. 建立捕捉會話(AVCaptureSession):iOS 在調用相機與話筒之前都需要先建立捕捉會話,把輸入輸出設備添加進會話中。
  2. 往會話中添加視訊輸入物件(AVCaptureDeviceInput)。
  3. 往會話中添加音訊輸入物件(AVCaptureDeviceInput)。
  4. 往會話中添加視訊輸出物件(AVCaptureVideoDataOutput)。
  5. 往會話中添加音訊輸出物件(AVCaptureAudioDataOutput)。
  6. 添加視訊預覽圖層(AVCaptureVideoPreviewLayer)。
  7. 開啟會話。
  8. 推流(以後再講)。

2. 效果圖

這是後置攝像頭採集的效果,直播一般用前置攝像頭,但我實在沒勇氣放出自己的自拍。

3. 程式碼示範

 // 采集 - (void)setupCaputureVideo { // 1.創建捕獲對話,必須要強引用,否則會釋放 _captureSession = [[AVCaptureSession alloc] init]; // 2.捕獲攝像頭設備,默認前置攝像頭 AVCaptureDevice *videoDevice = [self getVideoDevice:AVCaptureDevicePositionFront]; // 3.獲取聲音設備 AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio]; // 4.創建對應視頻設備輸入對象 _currentVideoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil]; // 5.創建對應音頻設備輸入對象 AVCaptureDeviceInput *audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil]; // 6.添加到會話中 注意:最好要判斷是否能添加輸入,會話不能添加空的 // 6.1 添加視頻 if ([_captureSession canAddInput:_currentVideoDeviceInput]) { [_captureSession addInput:_currentVideoDeviceInput]; } // 6.2 添加音頻 if ([_captureSession canAddInput:audioDeviceInput]) { [_captureSession addInput:audioDeviceInput]; } // 7.捕獲視頻數據輸出設備 AVCaptureVideoDataOutput *videoOutput = [[AVCaptureVideoDataOutput alloc] init]; //7.1 設置代理, 捕獲視頻樣品數據 dispatch_queue_t videoQueue = dispatch_queue_create("Video Capure Queue", DISPATCH_QUEUE_SERIAL); [videoOutput setSampleBufferDelegate: self queue:videoQueue]; // 8.設置音頻數據輸出設備 AVCaptureAudioDataOutput *audioOutput = [[AVCaptureAudioDataOutput alloc] init]; // 8.1 設置代理,捕獲音頻樣品數據 注意:必須是串行隊列才能捕獲到數據,而且不能為空 dispatch_queue_t audioQueue = dispatch_queue_create("Audio Capure Queue", DISPATCH_QUEUE_SERIAL); [audioOutput setSampleBufferDelegate:self queue:audioQueue]; // 9.添加到會話中 注意:最好要判斷是否能添加輸入,會話不能添加空的 if ([_captureSession canAddOutput:videoOutput]) { [_captureSession addOutput:videoOutput]; } if ([_captureSession canAddOutput:audioOutput]) { [_captureSession addOutput:audioOutput]; } // 10.獲取視屏輸入與輸出連接,用於分辨音視頻數據 _videoConnection = [videoOutput connectionWithMediaType:AVMediaTypeVideo]; // 11.添加視屏預覽圖層 _previewdLayer = [AVCaptureVideoPreviewLayer layerWithSession:_captureSession]; _previewdLayer.frame = [UIScreen mainScreen].bounds; [self.view.layer insertSublayer:_previewdLayer atIndex:0]; // 12.開啟會話 [_captureSession startRunning]; } // 根據攝像頭方向獲取攝像頭 - (AVCaptureDevice *)getVideoDevice: (AVCaptureDevicePosition)position { NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; for (AVCaptureDevice *device in devices) { if (device.position == position) { return device; } } return nil; } 

分析:

(1)攝像頭:每支手機都有且僅有兩個攝像頭——前置與後置,包括 iPhone 7 Plus,它後面的兩個攝像頭統稱為後置攝像頭。因此取得的是攝像頭陣列,我們根據方向取得指定的攝像頭。會話裡只能有一個攝像頭設備。

(2)攝像頭方向(AVCaptureDevicePosition):是一個枚舉,有三個值可選。不過若選擇 AVCaptureDevicePositionUnspecified,則無法調用攝像頭。

 AVCaptureDevicePositionUnspecified,那麼是不能調用攝像頭的。 typedef NS_ENUM(NSInteger, AVCaptureDevicePosition) { AVCaptureDevicePositionUnspecified = 0, // 不指定 AVCaptureDevicePositionBack = 1, //後置 AVCaptureDevicePositionFront = 2 // 前置 } 

(3)代理:AVCaptureVideoDataOutputSampleBufferDelegate 與 AVCaptureAudioDataOutputSampleBufferDelegate,分別為視訊與音訊輸出設備物件的代理,兩個代理都有下面的方法:

 // 獲取輸出設備數據,有可能是音頻,有可能是視頻 - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection { if (_videoConnection == connection) { NSLog(@"采集到視屏數據"); }else { NSLog(@"采集到音頻數據"); } }

4. 切換攝像頭

 #pragma mark - 切換攝像頭 - (IBAction)toggleCapture:(id)sender { // 1.獲取當前設備方向 AVCaptureDevicePosition cureentPosition = _currentVideoDeviceInput.device.position; // 2.獲取需要改變的方向 AVCaptureDevicePosition togglePosition = (cureentPosition == AVCaptureDevicePositionFront ? AVCaptureDevicePositionBack : AVCaptureDevicePositionFront); // 3.獲取需要改變的攝像頭設備 AVCaptureDevice *toggleDevice = [self getVideoDevice:togglePosition]; // 4.獲取需要改變的攝像頭輸入設備 AVCaptureDeviceInput *toggleDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:toggleDevice error:nil]; // 5.停止會話,否則會有一瞬間的白屏 [_captureSession stopRunning]; // 6.移除之前的攝像頭輸入設備,否則會崩潰,因為會話裡只能有一個攝像頭設備 [_captureSession removeInput:_currentVideoDeviceInput]; // 7.添加新的攝像頭輸入設備 [_captureSession addInput:toggleDeviceInput]; // 8.重新開始會話 [_captureSession startRunning]; // 記錄當前攝像頭輸入設備 //9.重新開始 _currentVideoDeviceInput = toggleDeviceInput; }

Demo 裡還有其他功能,但感覺沒太大用處,就不細講了,有興趣的可以去我的 GitHub 下載看看。

Demo 下載

Demo 下載後運行,發現報錯。

那是因為工程裡沒有上傳 ijkplayer 視訊直播框架。我把 ijkplayer 視訊直播框架放到了網雲上,沒有密碼,下載後放到 LiveAppDemo-master 文件夾裡,重新打開就可以運行了。

以上就是本文的全部內容,希望對大家的學習或工作帶來一些幫助。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved