请参阅相关问题:Navigator.mediaDevices.getUserMedia not working on iOS 12 Safari
我们正在尝试从用户输入用户 MediaDevices.getUserMedia 和音频上下文中捕获音频
当用户单击按钮时,我们会检查可用设备,然后捕获他们的音频流
let enumDevicePromise = navigator.mediaDevices.enumerateDevices()
.then(devices => devices.find(d => d.kind === "audioinput" && d.label !== "" && d.deviceId === "default"))
.catch((error) => {
// handle error
});
this.handleCheckEnumeratedDevices(enumDevicePromise); // capture device in backend
.....
navigator.mediaDevices
.getUserMedia({
audio: true,
video: false,
})
.then(stream => {
let AudioContext = window.AudioContext || window.webkitAudioContext;
if (AudioContext) {
let context = new AudioContext();
let source = context.createMediaStreamSource(stream);
let processor = context.createScriptProcessor(4096, 1, 1);
source.connect(processor);
processor.connect(context.destination);
processor.onaudioprocess = (event) => {
let audioIn = event.inputBuffer.getChannelData(0);
this.sendMessage(this.toInt16(audioIn));
}
} else {
// handle error, ie, Audio Context not supported
}
}).catch((error) => {
// handle error
});
});
这在 Chrome 和 Firefox 上运行良好,但在 Safari 12 上,我们从枚举设备承诺中得到了Null响应——尽管允许麦克风权限——因此我们无法捕获音频流