我正在尝试在 iOS11 上的 Safari 中实现从麦克风进行音频捕获,最近已经添加了支持。
然而,onaudioprocess
回调从未被调用。这是一个示例页面:
<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, i.e Convert this to WAV
debug(e.inputBuffer);
};
};
navigator.mediaDevices.getUserMedia({audio: true, video: false})
.then(handleSuccess);
}
</script>
</body>
</html>
在大多数平台上,当调用onaudioprocess
回调函数时,您将看到项目被添加到消息列表中。然而,在iOS上,这个回调函数从未被调用。
是否有其他事情我应该尝试来在Safari的iOS 11上调用它?
webkitAudioContext()
或调用.resume()
,而不是在获取流时创建。请查看下面的答案,以获取您代码的完全可行版本。 - Nathan Friedly