我不知道此时该怎么做,因为文档对此非常清楚,但它不起作用。以下是我使用 peer js 和他们的云服务器设置音频呼叫的方法,我为此获得了 API 密钥:
--请注意,我不乐意在这里放一大段代码,但它就在这里。注释掉的部分是试图遵循官方文档。此外,我没有收到不安全的来源错误。有一个现场演示 @ https://thealmightyone.github.io/AMAclient
去测试:
- 注册并登录 2 名玩家
- 右键单击一个
- 选择交谈
- 在右上角接听电话
您可以清楚地看到控制台中的日志。
函数 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'); } };
}
这里发生了什么?我以为我正在接收远程流并正确创建音频元素。