我在IOS原生应用中配置了WebRTC,但遇到了一个问题: 当我说话时,我听到了自己。我试图关闭所有RTCEAGLVideoView,我仍然听到自己。只有当我评论下一个代码的和平时,我才会听到自己的声音:
let audioTrack = pcFactory?.audioTrack(withTrackId: "localAudioTrack")
localStream?.addAudioTrack(audioTrack!)
另外,我尝试过使用audioTrack?.isEnabled = false,它可以工作,但是当我这样做时,对话的对话网站听不到我的声音。
那么,有没有办法在本地静音音轨,但是要让它为远程RTCMediaStream取消静音?谢谢。我的PeerConnection设置如下所示:
init(peerId:String, sender:RtcDataSender, isPublisher:Bool, isVideo:Bool = true, isAudio:Bool = true) {
super.init()
self.audio = isAudio
self.video = isVideo
self.rtcDataSender = sender
self.peerId = peerId
let decoderFactory = ARDVideoDecoderFactory.init()
let encoderFactory = ARDVideoEncoderFactory.init()
rtcDictionaryFactory = RtcDictionaryFactory()
// pcFactory = RTCPeerConnectionFactory.init(encoderFactory: encoderFactory, decoderFactory: decoderFactory)
pcFactory = RTCPeerConnectionFactory.init()
let config = RTCConfiguration.init()
config.iceServers = servers
let constraints:[String:String] = ["OfferToReceiveAudio":"\(isAudio)", "OfferToReceiveVideo":"\(isVideo)"]
let constraintsConnection:[String:String] = ["kRTCMediaConstraintsMinWidth" : "640",
"kRTCMediaConstraintsMinHeight" : "480",
"kRTCMediaConstraintsMinFrameRate" : "15",
"kRTCMediaConstraintsMaxWidth" : "1280",
"kRTCMediaConstraintsMaxHeight" : "720",
"kRTCMediaConstraintsMaxFrameRate" : "30"]
let mediaConnectionConstraints = RTCMediaConstraints.init(mandatoryConstraints: constraintsConnection, optionalConstraints: nil)
rtcMediaConstaints = RTCMediaConstraints.init(mandatoryConstraints: constraints, optionalConstraints: nil)
peerConnection = pcFactory?.peerConnection(with: config, constraints: rtcMediaConstaints!, delegate: self)
if (isPublisher) {
localStream = pcFactory?.mediaStream(withStreamId: "localStream")
if isVideo {
let track = self.createLocalVideoTrack()
localStream?.addVideoTrack(track!)
}
if isAudio {
let audioTrack = pcFactory?.audioTrack(withTrackId: "localAudioTrack")
localStream?.addAudioTrack(audioTrack!)
audioTrack?.isEnabled = false
// audioTrack?.isEnabled = false
// audioTrack?.source.volume = 0
}
rtcDataSender?.localStreamAdded(peerId: peerId, stream: localStream!, isVideo: isVideo)
peerConnection?.add(localStream!)
if isVideo {
didCreateCapturer(capturer: capturer!)
}
}
}