如何在不更改框架的情况下在 webrtc 移动应用程序(ios)中禁用音频

How to disable audio in webrtc mobile app(ios) without changing in framework

我正在使用 webrtc mobile(ios)。我无法在 webrtc(ios) 中禁用音频。我没有标志来禁用 audio.By 更改 framwork/library 它可以轻松完成。我的目的是我必须在不更改 framework/library 的情况下禁用音频。谁能帮帮我?

用代码片段更新你的问题,你是如何创建 mediaStrem 或轨道的(audio/video)。

通常使用默认的原生 WebRTC 框架,

RTCMediaStream localStream = [_factory mediaStreamWithStreamId:kARDMediaStreamId];
if(audioRequired) {
  RTCAudioTrack *aTrack = [_lmStream createLocalAudioTrack];
  [localStream addAudioTrack:aTrack];
}
RTCVideoTrack *vTrack = [_lmStream createLocalVideoTrack];
[localStream addVideoTrack:vTrack];
[_peerConnection addStream:localStream];

如果您想在通话过程中将音频静音,请使用以下功能。

- (void)enableAudio:(NSString *)id isAudioEnabled:(BOOL) isAudioEnabled {
  NSLog(@"Auido enabled: %d streams count:%d ", id, isAudioEnabled, _peerConnection.localStreams.count);
  if(_peerConnection.localStreams.count > 0) {
    RTCMediaStream *lStream = _peerConnection.localStreams[0];
    if(lStream.audioTracks.count > 0) { // Usually we will have only one track. If you have more than one, need to traverse all.
      // isAudioEnabled == 1 -> Unmute
      // isAudioEnabled == 0 -> Mute
      [lStream.audioTracks[0] setIsEnabled:isAudioEnabled];
    }
  }
}

在我的例子中,我没有使用流,而是直接将音轨添加到对等连接。

private func createMediaSenders() {
    let streamId = "stream"
    
    // Audio
    let audioTrack = self.createAudioTrack()
    self.pc.add(audioTrack, streamIds: [streamId])
    
    // Video
  /*  let videoTrack = self.createVideoTrack()
    self.localVideoTrack = videoTrack
    self.peerConnection.add(videoTrack, streamIds: [streamId])
    self.remoteVideoTrack = self.peerConnection.transceivers.first { [=10=].mediaType == .video }?.receiver.track as? RTCVideoTrack
    
    // Data
    if let dataChannel = createDataChannel() {
        dataChannel.delegate = self
        self.localDataChannel = dataChannel
    }*/
}

private func createAudioTrack() -> RTCAudioTrack {
    let audioConstrains = RTCMediaConstraints(mandatoryConstraints: nil, optionalConstraints: nil)
    let audioSource = sessionFactory.audioSource(with: audioConstrains)
    let audioTrack = sessionFactory.audioTrack(with: audioSource, trackId: "audio0")
    return audioTrack
}

将麦克风静音和取消静音我使用此功能

public func muteMicrophone(_ mute:Bool){
    
        for sender in pc.senders{

            if (sender.track?.kind == "audio") {
                sender.track?.isEnabled = mute
            }
        }
    }