在Swift中使用WebRTC中的后置摄像头

时间:2018-02-07 04:28:08

标签: swift webrtc

要切换WebRTC中的摄像头,我想选择正确的摄像设备(后置摄像头),然后使用RTCVideoCapturer定义视频源。

在Objective C中,它如下所示:

RTCVideoCapturer *capturer = [RTCVideoCapturer capturerWithDeviceName:cameraID];
RTCMediaConstraints *mediaConstraints = [self defaultMediaStreamConstraints];
RTCVideoSource *videoSource = [_factory videoSourceWithCapturer:capturer constraints:mediaConstraints];
localVideoTrack = [_factory videoTrackWithID:@"ARDAMSv0" source:videoSource];

似乎RTCVideoCapurer可用的唯一构造函数需要代表,即

let capturer = RTCVideoCapturer(delegate: <#T##RTCVideoCapturerDelegate#>)

那么我该如何翻译代码?

1 个答案:

答案 0 :(得分:1)

Swift 3中的翻译

var capturer = RTCVideoCapturer(deviceName: cameraID)
var mediaConstraints: RTCMediaConstraints? = defaultMediaStreamConstraints()
var videoSource: RTCVideoSource? = factory.videoSource(with: capturer, constraints: mediaConstraints)
localVideoTrack = factory.videoTrack(withID: "ARDAMSv0", source: videoSource)

有一个&#34; bool&#34;被称为&#34; useBackCamera&#34;在RTCAVFoundationVideoSource类中( RTCAVFoundationVideoSource.h )。您可以使用此属性在前/后相机之间切换。

//In  RTCAVFoundationVideoSource.h

/** Returns whether rear-facing camera is available for use. */
@property(nonatomic, readonly) BOOL canUseBackCamera;

/** Switches the camera being used (either front or back). */
@property(nonatomic, assign) BOOL useBackCamera;

/** Returns the active capture session. */
@property(nonatomic, readonly) AVCaptureSession *captureSession;