我目前正在努力改编这个 web audio API demo对于我正在做的一个项目,但是当我在 iPhone 上测试时没有声音。它在 iPad 上运行良好。
我搜索了解决方案并找到了这个 thread在 StackOverflow 上使用以下答案之一的片段:
Safari on iOS 6 effectively starts with the Web Audio API muted. It will not unmute until you attempt to play a sound in a user input event (create a buffer source, connect it to destination, and call noteOn()). After this, it unmutes and audio plays unrestricted and as it ought to. This is an undocumented aspect of how the Web Audio API works on iOS 6 (Apple's doc is here, hopefully they update it with a mention of this soon!)
用户输入事件应该是播放按钮上的 onclick 事件,但更改为使用 noteOn()
而不是 start()
仍然无法解决问题。
更新:我也尝试过将播放按钮与 touchend
事件绑定(bind),但没有成功。
这是使用 noteOn() 的函数:
function playNote(buffer, pan, x, y, z, sendGain, mainGain, playbackRate, noteTime) {
// Create the note
var voice = context.createBufferSource();
voice.buffer = buffer;
voice.playbackRate.value = playbackRate;
// Optionally, connect to a panner
var finalNode;
if (pan) {
var panner = context.createPanner();
panner.panningModel = "HRTF";
panner.setPosition(x, y, z);
voice.connect(panner);
finalNode = panner;
} else {
finalNode = voice;
}
// Connect to dry mix
var dryGainNode = context.createGain();
dryGainNode.gain.value = mainGain * effectDryMix;
finalNode.connect(dryGainNode);
dryGainNode.connect(masterGainNode);
// Connect to wet mix
var wetGainNode = context.createGain();
wetGainNode.gain.value = sendGain;
finalNode.connect(wetGainNode);
wetGainNode.connect(convolver);
if (iOS) {
voice.noteOn(noteTime);
}
else {
voice.start(noteTime);
}
}
如有任何建议,我们将不胜感激。谢谢。
最佳答案
我觉得自己很蠢。显然,如果您的 iPhone 处于振动模式,则不会播放声音。
关于javascript - Web Audio API 演示在 iOS 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46010483/