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

时间:2017-05-23 09:52:28

标签: ios audio webrtc apprtc

我正在使用webrtc mobile(ios)。我无法在webrtc(ios)中禁用音频。我没有禁用音频的标志。通过更改框架/库可以轻松完成。我的目的是我必须在不改变框架/库的情况下禁用音频。任何人都可以帮助我吗?。

2 个答案:

答案 0 :(得分:3)

使用代码段更新您的问题,如何创建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];
    }
  }
}

答案 1 :(得分: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
            }
        }
    }