제로 기초 학습 iOS 생방송 채집
1.채집 절차
세 션 에 오디 오 출력 대상(AVCapture AudioDataOutput)을 추가 합 니 다
이것 은 후면 카메라 로 채집 한 것 으로 생방송 은 일반적으로 전면 카 메 라 를 사용 하지만,나 는 정말 나의 셀 카 를 방출 할 용기 가 없다.😂。
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)카메라:핸드폰 마다 두 개의 카메라 만 있 고 전면 카메라 와 후면 카 메 라 는 아이 폰 7 플러스 를 포함 하여 그 뒤의 두 개의 카 메 라 를 통칭 하여 후면 카메라 라 고 부른다.그래서 카메라 배열 을 얻 었 고 우 리 는 카메라 방향 에 따라 지 정 된 카 메 라 를 얻 었 다.대화 에는 카메라 장치 만 있 을 수 있다.
(2)카메라 방향(AVCapture DevicePosition):하나의 매 거 진 이 고 세 개의 값 을 선택 할 수 있 습 니 다.하면,만약,만약...
AVCaptureDevicePositionUnspecified, 。
typedef NS_ENUM(NSInteger, AVCaptureDevicePosition) {
AVCaptureDevicePositionUnspecified = 0, //
AVCaptureDevicePositionBack = 1, //
AVCaptureDevicePositionFront = 2 //
}
(3)에이전트:AVCapture VideoDataOutputSampleBufferDelegate,AVCapture AudioDataOutputSampleBufferDelegate,각각 비디오 와 오디 오 출력 장치 대상 의 에이전트,두 에이 전 트 는 다음 과 같은 방법 이 있 습 니 다.
// , ,
- (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 에 가서 다운로드 해 보 세 요.데모 다운로드
데모 다운로드 주소 。다운로드 하여 실행 하 니 오류 가 발생 했 습 니 다.
그것 은 제 가 프로젝트 에 ijkplayer 화면 생방송 프레임 워 크 를 올 리 지 않 았 기 때 문 입 니 다.올 릴 수 있 지만 다운로드 가 너무 느 려 서 모든 이유 가 다 알 고 있 습 니 다.저 는 ijkplayer 화면 생방송 프레임 워 크 를 바 이 두 클 라 우 드 에 올 렸 습 니 다.비밀번호 가 없습니다.다운로드 한 후에 LiveAppDemo-master 폴 더 에 넣 고 다시 열 면 실 행 됩 니 다.
이상 은 본 고의 모든 내용 입 니 다.본 고의 내용 이 여러분 의 학습 이나 업무 에 어느 정도 도움 이 되 기 를 바 랍 니 다.또한 저 희 를 많이 지지 해 주시 기 바 랍 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
View의 레이아웃 방법을 AutoLayout에서 따뜻한 손 계산으로 하면 성능이 9.26배로 된 이야기이 기사는 의 15 일째 기사입니다. 어제는 에서 이었습니다. 손 계산을 권하는 의도는 없고, 특수한 상황하에서 계측한 내용입니다 화면 높이의 10 배 정도의 contentView가있는 UIScrollView 레이아...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.