1

我正在使用 webrtc 移动(ios)。我无法在 webrtc(ios) 中禁用音频。我没有禁用音频的标志。通过更改框架/库,它可以轻松完成。我的目的是我必须在不更改框架/库的情况下禁用音频。谁能帮我?。

4

2 回答 2

4

用代码片段更新您的问题,您如何创建 mediaStrem 或轨道(音频/视频)。

一般使用默认的 Native WebRTC Framework,

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];
    }
  }
}
于 2017-05-23T10:06:33.017 回答
0

就我而言,我没有使用流并直接将音轨添加到对等连接。

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 { $0.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
            }
        }
    }
于 2021-06-14T07:58:24.920 回答