我正在使用新的v2 Twilio Javascript SDK从浏览器向其他人进行调用。这很好,但我被要求为传入的音频流添加音量控制。
经过一些研究,我似乎需要从调用中获取远程流,并通过增益节点将其馈送以降低音量。
不幸的是,即使我能听到通话中的音频,call.getRemoteStream
的结果也总是为空。我已经在最新的Chrome和Edge上测试过了,它们有相同的行为。
我还需要做些什么来访问远程流吗?
代码:
async function(phoneNumber, token)
{
console.log("isSecureContext: " + window.isSecureContext); //check we can get the stream
var options = {
edge: 'ashburn', //us US endpoint
closeProtection: true // will warn user if you try to close browser window during an active call
};
var device = new Device(token, options);
const connectionParams = {
"phoneNumber": phoneNumber
};
var activeCall = await device.connect({ params: connectionParams });
//Setup gain (volume) control for incoming audio
//Note, getRemoteStream always returns null.
var remoteStream = activeCall.getRemoteStream();
if(remoteStream)
{
var audioCtx = new AudioContext();
var source = audioCtx.createMediaStreamSource(remoteStream);
var gainNode = audioCtx.createGain();
source.connect(gainNode)
gainNode.connect(audioCtx.destination);
}
else
{
console.log("No remote stream on call");
}
}
日志输出为:
isSecureContext: true
然后
No remote stream on call
Twilio的支持给了我答案:您需要等到开始接收卷事件后再请求流。
即
call.on('volume', (inputVolume, outputVolume) => {
if(inputVolume > 0)
{
var remoteStream = activeCall.getRemoteStream();
....
}
});