onaudioprocess没有呼吁ios11

我试图从iOS11上的Safari上的麦克风获取audio捕获后,最近添加的支持

然而, onaudioprocesscallback从来没有被调用。 这是一个示例页面:

 <html> <body> <button onclick="doIt()">DoIt</button> <ul id="logMessages"> </ul> <script> function debug(msg) { if (typeof msg !== 'undefined') { var logList = document.getElementById('logMessages'); var newLogItem = document.createElement('li'); if (typeof msg === 'function') { msg = Function.prototype.toString(msg); } else if (typeof msg !== 'string') { msg = JSON.stringify(msg); } var newLogText = document.createTextNode(msg); newLogItem.appendChild(newLogText); logList.appendChild(newLogItem); } } function doIt() { var handleSuccess = function (stream) { var context = new AudioContext(); var input = context.createMediaStreamSource(stream) var processor = context.createScriptProcessor(1024, 1, 1); input.connect(processor); processor.connect(context.destination); processor.onaudioprocess = function (e) { // Do something with the data, ie Convert this to WAV debug(e.inputBuffer); }; }; navigator.mediaDevices.getUserMedia({audio: true, video: false}) .then(handleSuccess); } </script> </body> </html> 

在大多数平台上,随着onaudioprocesscallback被调用,您将看到添加到消息列表中的onaudioprocess 。 不过,在iOS上,这个callback从来不会被调用。

还有什么我应该做的尝试,并得到它与Safari的iOS 11呼吁?

有两个问题。 主要的一点是iOS 11上的Safari似乎会自动挂起新的AudioContext ,而这些新的AudioContext并不是响应点击而创build的。 你可以resume()他们,但只是响应一个水龙头。

因此,您必须在获得MediaStream之前创build它,或者稍后让用户再次点击。

您的代码的另一个问题是, AudioContext在Safari中作为webkitAudioContext前缀。

这是一个工作版本:

 <html> <body> <button onclick="doIt()">DoIt</button> <ul id="logMessages"> </ul> <script> function debug(msg) { if (typeof msg !== 'undefined') { var logList = document.getElementById('logMessages'); var newLogItem = document.createElement('li'); if (typeof msg === 'function') { msg = Function.prototype.toString(msg); } else if (typeof msg !== 'string') { msg = JSON.stringify(msg); } var newLogText = document.createTextNode(msg); newLogItem.appendChild(newLogText); logList.appendChild(newLogItem); } } function doIt() { var AudioContext = window.AudioContext || window.webkitAudioContext; var context = new AudioContext(); var processor = context.createScriptProcessor(1024, 1, 1); processor.connect(context.destination); var handleSuccess = function (stream) { var input = context.createMediaStreamSource(stream); input.connect(processor); processor.onaudioprocess = function (e) { // Do something with the data, ie Convert this to WAV debug(e.inputBuffer); }; }; navigator.mediaDevices.getUserMedia({audio: true, video: false}) .then(handleSuccess); } </script> </body> </html> 

(您可以尽早设置onaudioprocesscallback,但是您将获得空的缓冲区,直到用户批准麦克风访问。)

哦,另外还有一个需要注意的iOS错误:iPod touch上的Safari(iOS 11)认为它没有麦克风(它确实是)。 所以,getUserMedia将拒绝一个Error: Invalid constraint如果你问那里的audio。

仅供参考:我在npm上维护麦克风stream包,为您提供这个服务,并以Node.js风格的ReadableStream提供audio。 我只是用这个修补程序更新它,如果你或者其他人更喜欢在原始代码上使用它。

尝试在iOS 11.0.1,不幸的是,这个问题仍然是不固定的。

作为一个解决方法,我想知道是否有意义的是用一个函数replaceScriptProcessor,该函数从自助餐中获取蒸汽数据,然后每x毫秒处理一次。 但是这对function来说是一个很大的改变。

只是想知道…你有Safari设置中启用设置? 它在iOS11默认启用,但也许你只是禁用它没有注意到。

在这里输入图像说明