在没有<audio>元素(RTCMultiConnection)</audio>的情况下听到WebRTC音频

时间:2014-09-16 08:02:02

标签: html5 audio webrtc

即使没有音频元素插入DOM中,也会听到音频。

情景:

  1. 创建不带流的PeerConnection
  2. 添加流但禁用将MediaElements(音频,视频)添加到DOM
  3. 的代码

    问题:

    1. 流遇到后,可以从耳机(或扬声器)听到声音。
    2. 会发生什么:

      1. 由于我没有在dom上附上任何东西,我希望听不到任何声音。
      2. Code for replicating the scenario

        // <body>
        //   <script src="https://cdn.webrtc-experiment.com/RTCMultiConnection.js"></script>
        //   <button id="start">Start!</button>
        // </body>
        
        $('#start').click(function() {
          var NO_MEDIA_SESSION = {video: false, audio: false, oneway: true};
        
          var caller = new RTCMultiConnection('lets-try');
          caller.session = NO_MEDIA_SESSION;
          caller.dontAttachStream = true;
          caller.onstream = function() { console.log("Got stream but not attaching") };  
        
          var receiver = new RTCMultiConnection('lets-try');
          receiver.session = NO_MEDIA_SESSION;
          receiver.dontAttachStream = true;
          receiver.onstream = function() { console.log("Got stream but not attaching") };  
        
          caller.open();
          receiver.connect();
        
          receiver.onconnected = function() {
            console.log("Connected!");
            caller.addStream({audio: true});
          }
        });
        

        我很感兴趣如果没有 audio DOM元素,怎么可能听到MediaStream? 如果任何RTCMultiConnection专家回答,那么可能指出我如何避免音频流被听到? (我想获得流并在以后自己附上它。)

1 个答案:

答案 0 :(得分:2)

RTCMultiConnection即时创建mediaElement,以确保仅在媒体流开始流动时才会触发onstream事件。

connection.onstream = function(event) {
    event.mediaElement.pause(); // or volume=0

    // or
    event.mediaElement = null;

    // or
    delete event.mediaElement;
};

更新

使用以下代码段:

var connection = new RTCMultiConnection();

connection.session = {
    data: true
};

btnOpenRoom.onclick = function() {
    connection.open('roomid');
};

btnJoinRoom.onclick = function() {
    connection.join('roomid');
};

btnAddAudioStream.onclick = function() {
    connection.addStream({
        audio: true
    });
};

btnAddAudioVideoStream.onclick = function() {
    connection.addStream({
        audio: true,
        video: true
    });
};