【发布时间】:2018-03-03 22:25:53
【问题描述】:
我正在尝试从在 iOS11 上的 Safari 上工作的麦克风获取音频捕获 after support was recently added
但是,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()以直接响应点击,而不是在获得流时。有关您的代码的完整工作版本,请参阅下面的答案。
标签: ios safari web-audio-api ios11