【问题标题】:WebRTC audio heard without <audio> element (RTCMultiConnection)没有 <audio> 元素的 WebRTC 音频 (RTCMultiConnection)
【发布时间】:2014-09-16 08:02:01
【问题描述】:

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

场景:

  1. 创建没有流的 PeerConnection
  2. 添加流但禁用将 MediaElements(音频、视频)添加到 DOM 的代码

问题:

  1. 流通过后,可以从耳机(或扬声器)听到音频。

应该发生什么:

  1. 由于我没有在 dom 上附加任何东西,我希望不会听到任何声音。

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 专家回答,那么也许会指出我如何避免音频流被听到? (我想自己获取流并稍后附加)。

【问题讨论】:

    标签: html audio webrtc


    【解决方案1】:

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

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

    更新:

    使用以下sn-p:

    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
        });
    };
    

    【讨论】:

    • 谢谢。这解决了这个问题。我自己也得出了结论 - 如果创建的 MediaElement(音频或视频)调用了#play,用户将听到音频。因此,正如您建议调用#pause 那样,将音量设置为0(或不调用#play)即可。 (github.com/muaz-khan/WebRTC-Experiment/blob/…) 我的假设是错误的,即 MediaElement 需要附加到 DOM。
    • 创建了另一个示例来证明不需要附加到 dom,尽管从 DOM 中删除会停止音频。 jsfiddle.net/2113jvr9
    猜你喜欢
    • 2018-02-28
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 2012-02-16
    相关资源
    最近更新 更多