제로 기초 학습 iOS 생방송 채집

6277 단어 iOS생 중계
생방송 채집 은 채집 장비(카메라,마이크)에 따라 영상 채집 과 오디 오 채집 으로 나 뉘 는데,이 글 은 각각 소개 한다.
1.채집 절차
  • 캡 처 세 션(AVCapture Session)을 만 들 고 iOS 가 카메라 와 마 이 크 를 호출 하기 전에 캡 처 대 화 를 만 들 고 입 출력 장 치 를 대화 에 추가 해 야 합 니 다
  • 세 션 에 비디오 입력 대상(AVCapture DeviceInput)을 추가 합 니 다세 션 에 오디 오 입력 대상(AVCapture DeviceInput)을 추가 합 니 다세 션 에 비디오 출력 대상(AVCapture VideoDataOutput)을 추가 합 니 다.
    세 션 에 오디 오 출력 대상(AVCapture AudioDataOutput)을 추가 합 니 다
  • 화면 미리 보기 그림 층(AVCapture Video Preview Layer)을 추가 합 니 다.
  • 세 션 을 시작 합 니 다4.567917.추 류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)카메라:핸드폰 마다 두 개의 카메라 만 있 고 전면 카메라 와 후면 카 메 라 는 아이 폰 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 폴 더 에 넣 고 다시 열 면 실 행 됩 니 다.

    이상 은 본 고의 모든 내용 입 니 다.본 고의 내용 이 여러분 의 학습 이나 업무 에 어느 정도 도움 이 되 기 를 바 랍 니 다.또한 저 희 를 많이 지지 해 주시 기 바 랍 니 다!

    좋은 웹페이지 즐겨찾기