iOS SWIFT - WebRTC 从前置摄像头更改为后置摄像头 [英] iOS SWIFT - WebRTC change from Front Camera to back Camera
问题描述
WebRTC 视频默认使用前置摄像头,效果很好.但是,我需要将它切换到后置摄像头,但我找不到任何代码来做到这一点.我需要编辑哪个部分?是 localView 或 localVideoTrack 还是捕获器?
WebRTC video by default uses Front Camera, which works fine. However, i need to switch it to back camera, and i have not been able to find any code to do that. Which part do i need to edit? Is it the localView or localVideoTrack or capturer?
推荐答案
Swift 3.0
对等连接只能有一个RTCVideoTrack"用于发送视频流.
Peer connection can have only one 'RTCVideoTrack' for sending video stream.
首先,要更改前/后摄像头,您必须删除对等连接上的当前视频轨道.之后,您在需要的相机上创建新的RTCVideoTrack",并将其设置为对等连接.
At first, for change camera front/back you must remove current video track on peer connection. After then, you create new 'RTCVideoTrack' on camera which you need, and set this for peer connection.
我用过这种方法.
func swapCameraToFront() {
let localStream: RTCMediaStream? = peerConnection?.localStreams.first as? RTCMediaStream
localStream?.removeVideoTrack(localStream?.videoTracks.first as! RTCVideoTrack)
let localVideoTrack: RTCVideoTrack? = createLocalVideoTrack()
if localVideoTrack != nil {
localStream?.addVideoTrack(localVideoTrack)
delegate?.appClient(self, didReceiveLocalVideoTrack: localVideoTrack!)
}
peerConnection?.remove(localStream)
peerConnection?.add(localStream)
}
func swapCameraToBack() {
let localStream: RTCMediaStream? = peerConnection?.localStreams.first as? RTCMediaStream
localStream?.removeVideoTrack(localStream?.videoTracks.first as! RTCVideoTrack)
let localVideoTrack: RTCVideoTrack? = createLocalVideoTrackBackCamera()
if localVideoTrack != nil {
localStream?.addVideoTrack(localVideoTrack)
delegate?.appClient(self, didReceiveLocalVideoTrack: localVideoTrack!)
}
peerConnection?.remove(localStream)
peerConnection?.add(localStream)
}
这篇关于iOS SWIFT - WebRTC 从前置摄像头更改为后置摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!