peerJS audiocall仅在本地运行

时间:2016-03-31 01:20:50

标签: javascript node.js audio webrtc peerjs

我不知道该做什么,因为文档对此很清楚,但它不起作用。 以下是我正在使用peer js及其云服务器设置音频呼叫的工作,我获得了一个API密钥:

- 请注意我在这里放一大块代码并不高兴,但现在就是这样。 注释掉的部分是试图遵循官方文档。 另外,我没有得到不安全的原始错误。有一个现场演示@ KeyboardFocusManager#getFocusOwner

TO TEST:

  1. 注册并登录2名玩家
  2. 右键单击一个
  3. 选择谈话
  4. 在右键角落接听电话
  5. 您可以在控制台中清楚地看到日志。

    function peerJsTools(){     var allAudioCalls = {};     var playerPeer = new Peer({key:'lwjd5qra8257b9'}); //将PEERJS API键移动到网络设置

    var playerPeerId;
    this.getPeerId = function() {
        return playerPeerId;
    };
    navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
    playerPeer.on('open', function(id) {
        console.log('My peer ID is: ' + id);
        playerPeerId = id;
    });
    
    playerPeer.on('call', function(call) {
        console.log('incoming call detected')
        navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
        navigator.getUserMedia({
            video: false,
            audio: true
        }, function(stream) {
            call.answer(stream); // Answer the call with an A/V stream.
            call.on('stream', function(remoteStream) {
                console.log('PEERJS: incoming audio stream', remoteStream);
                // WEBAUDIO API BELOW - DOESN'T WORK IN CHROME?
                // var audioContext = new AudioContext();
                // var audioStream = audioContext.createMediaStreamSource(remoteStream);
                // audioStream.connect(audioContext.destination);
                  var audio = $('<audio autoplay />').appendTo('body');
                  audio[0].src = (URL || webkitURL || mozURL).createObjectURL(remoteStream);
            });
        }, function(err) {
            console.log('Failed to get local stream', err);
        });
    });
    
    this.initiatePeerAudioCall = function(id, peerId) {
        navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
        navigator.getUserMedia({
                video: false,
                audio: true
            }, function(stream) {
                var call = playerPeer.call(peerId, stream);
    
                console.log('PEERJS: calling', peerId, 'with', stream);
    
                allAudioCalls[id] = call;
                call.on('stream', function(remoteStream) {
                    console.log('PEERJS: got an answer back');
                    // var audioContext = new AudioContext();
                    // var audioStream = audioContext.createMediaStreamSource(remoteStream);
                    // audioStream.connect(audioContext.destination);
                      var audio = $('<audio autoplay />').appendTo('body');
                      audio[0].src = (URL || webkitURL || mozURL).createObjectURL(remoteStream);
                });
            }, function(err) {
                console.log('Failed to get local stream', err);
            }
        );
    };
    
    this.endActiveCall = function(id) {
        if(allAudioCalls[id]){
            console.log('ending active call');
            allAudioCalls[id].close();
        }
        else{
            console.log('stopping call attempt');
        }
    };
    

    }

  6. 这里发生了什么?我以为我正在接收远程流并正确创建音频元素。

0 个答案:

没有答案