尝试捕获音频,但即使授予了麦克风权限,在Safari 12上navigator.mediaDevices.enumerateDevices()也为NULL

时间:2019-05-08 18:32:09

标签: safari audiocontext mediadevices enumerate-devices

查看相关问题: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 响应-因此,我们无法捕获音频流

1 个答案:

答案 0 :(得分:0)

发生这种情况是因为Mobile Safari不会公开“音频输入”类媒体设备。这是一个已知的限制。