2012-06-17 28 views
13

Tôi muốn hiển thị luồng của mặt trước và mặt sau của máy ảnh iPad2 trong hai UIViews cạnh nhau. Để dòng hình ảnh của một thiết bị tôi sử dụng đoạn mã sauChạy nhiều AVCaptureSessions hoặc thêm nhiều đầu vào

AVCaptureDeviceInput *captureInputFront = [AVCaptureDeviceInput deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] error:nil]; 

AVCaptureSession *session = [[AVCaptureSession alloc] init]; 
session addInput:captureInputFront]; 
session setSessionPreset:AVCaptureSessionPresetMedium]; 
session startRunning]; 

AVCaptureVideoPreviewLayer *prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:session]; 
prevLayer.frame = self.view.frame; 
[self.view.layer addSublayer:prevLayer]; 

mà hoạt động tốt cho cả hai camera. Để hiển thị luồng song song, tôi đã cố gắng tạo một phiên khác, nhưng ngay khi phiên thứ 2 được thiết lập thì đóng băng đầu tiên.

Sau đó, tôi đã cố gắng thêm hai AVCaptureDeviceInput vào phiên nhưng có vẻ như nhiều nhất một đầu vào được hỗ trợ tại thời điểm này.

Bất kỳ ý tưởng hữu ích nào về cách phát trực tiếp từ cả hai máy ảnh?

+0

bản sao có thể có của [Làm thế nào tôi có thể lấy nét tự động để làm việc trong AVCaptureSession thứ hai mà không cần tạo lại các phiên?] (Http://stackoverflow.com/questions/5427561/how-can-i-get-autofocus-to-work -in-a-giây-avcapturesession-without-tái tạo) –

Trả lời

13

thể để có được CMSampleBufferRef s từ nhiều thiết bị video trên hệ điều hành MacOS X. Bạn phải thiết lập các AVCaptureConnection đối tượng bằng tay. Ví dụ, giả sử bạn có các đối tượng:

AVCaptureSession *session; 
AVCaptureInput *videoInput1; 
AVCaptureInput *videoInput2; 
AVCaptureVideoDataOutput *videoOutput1; 
AVCaptureVideoDataOutput *videoOutput2; 

Do KHÔNG thêm các đầu ra như thế này:

[session addOutput:videoOutput1]; 
[session addOutput:videoOutput2]; 

Thay vào đó, thêm chúng và nói với phiên giao dịch không thực hiện bất kỳ kết nối:

[session addOutputWithNoConnections:videoOutput1]; 
[session addOutputWithNoConnections:videoOutput2]; 

Sau đó, đối với mỗi cặp đầu vào/đầu ra, hãy thực hiện kết nối từ cổng video đầu vào đến đầu ra bằng tay:

for (AVCaptureInputPort *port in [videoInput1 ports]) { 
    if ([[port mediaType] isEqualToString:AVMediaTypeVideo]) { 
     AVCaptureConnection* cxn = [AVCaptureConnection 
      connectionWithInputPorts:[NSArray arrayWithObject:port] 
      output:videoOutput1 
     ]; 
     if ([session canAddConnection:cxn]) { 
      [session addConnection:cxn]; 
     } 
     break; 
    } 
} 

Cuối cùng, hãy chắc chắn để thiết lập các đại biểu đệm mẫu cho cả hai kết quả đầu ra:

[videoOutput1 setSampleBufferDelegate:self queue:someDispatchQueue]; 
[videoOutput2 setSampleBufferDelegate:self queue:someDispatchQueue]; 

và bây giờ bạn sẽ có thể xử lý các khung hình từ cả hai thiết bị:

- (void)captureOutput:(AVCaptureOutput *)captureOutput 
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
     fromConnection:(AVCaptureConnection *)connection 
{ 
    if (captureOutput == videoOutput1) 
    { 
     // handle frames from first device 
    } 
    else if (captureOutput == videoOutput2) 
    { 
     // handle frames from second device 
    } 
} 

Xem cũng là AVVideoWall sample project cho ví dụ về kết hợp xem trước trực tiếp từ nhiều thiết bị video.

+0

Cảm ơn điều này đã làm việc cho tôi với một bổ sung. Tôi cũng phải làm: [session addInputWithNoConnections: videoInput1]; [session addInputWithNoConnections: videoInput2]; –

+6

Không hoạt động trên iOS 10 - thêm đầu vào thứ hai vào phiên không thành công: Chấm dứt ứng dụng do ngoại lệ không xác định 'NSInvalidArgumentException', lý do: '*** - [AVCaptureSession addInputWithNoConnections:] Nhiều âm thanh/video AVCaptureInputs hiện không được hỗ trợ –

Các vấn đề liên quan