Vonage浏览器到浏览器的音频调用返回一些错误



我正在使用Laravel,并尝试添加浏览器到浏览器的音频呼叫。我正在使用Vonage (Tookbox) API对此,但我得到一些错误。

这是我的代码:

async function audioCall() {
var publisher;
var targetElement = 'publisher';
var pubOptions = {publishAudio:true, publishVideo:false};
publisher = OT.initPublisher(targetElement, pubOptions, function(error) {
if (error) {
alert("The client cannot publish.");
} else {
console.log('Publisher initialized.');
}
});
// Setting an audio source to a new MediaStreamTrack
const stream = await OT.getUserMedia({
videoSource: null
});
const [audioSource] = stream.getAudioTracks();
publisher.setAudioSource(audioSource).then(() => console.log('Audio source updated'));
// Cycling through microphone inputs
let audioInputs;
let currentIndex = 0;
OT.getDevices((err, devices) => {
audioInputs = devices.filter((device) => device.kind === 'audioInput');
// Find the right starting index for cycleMicrophone
audioInputs.forEach((device, idx) => {
if (device.label === publisher.getAudioSource().label) {
currentIndex = idx;
}
});
});
const cycleMicrophone = () => {
currentIndex += 1;
let deviceId = audioInputs[currentIndex % audioInputs.length].deviceId;
publisher.setAudioSource(deviceId);
};
}

这段代码在控制台返回一个错误:

Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules

我认为问题是你有

device.kind === 'audioInput'

,我很确定设备。Kind显示为'audioinput'(全小写)。

例子:

https://developer.mozilla.org/en-US/docs/Web/API/MediaDeviceInfo/kindhttps://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/enumerateDevices示例

这将使audioInputs为空(尝试console.log进行验证),并给出错误,因为没有设备。

试题:

device.kind.toLowerCase() === 'audioinput'

希望一切顺利。

最新更新