我已经遇到了一段时间的问题,在某些ios设备上,我的Webaudio系统似乎只能在耳机上工作,而在其他设备(操作系统,型号相同的设备)上,音频可以通过扬声器或耳机完美播放。我一直在寻找解决方案,但是在这个确切的问题上什么都没找到。我唯一能想到的就是可能是音频 channel 问题或其他问题。我很困惑你们有没有遇到过这个问题,或者有任何建议解决此问题?谢谢!
最佳答案
@Alastair是正确的,静音切换开关可以静音WebAudio,但是不能静音HTML5标签。多亏了他的工作,我设法找到了一种解决网络问题的方法,即使启用了静音拨动开关,也可以使WebAudio播放。我会将其作为对他的回复的评论,但我没有声誉。
为了播放WebAudio,您还必须在用户操作期间至少播放一个WebAudio声源节点和一个HTML5标签。如果这些声音是一小段沉默,那就很好。我发现此自包含代码无需任何其他文件即可工作:
编辑11/29/19:
删除了残留的 typescript typedef。谢谢@Joep。我还意识到下面的代码已经过时且过时了。只是考虑一个例子。编辑这篇文章促使我为此创建了一个开源解决方案。您可以在此处查看其演示:https://spencer-evans.com/share/github/unmute/,并在此处查看存储库:https://github.com/swevans/unmute
/**
* PLEASE DONT USE THIS AS IT IS, THIS IS JUST EXAMPLE CODE.
* If you want a drop in solution I have a script on git hub
* Demo:
* @see https://spencer-evans.com/share/github/unmute/
* Github Repo:
* @see https://github.com/swevans/unmute
*/
var isWebAudioUnlocked = false;
var isHTMLAudioUnlocked = false;
function unlock() {
if (isWebAudioUnlocked && isHTMLAudioUnlocked) return;
// Unlock WebAudio - create short silent buffer and play it
// This will allow us to play web audio at any time in the app
var buffer = myContext.createBuffer(1, 1, 22050); // 1/10th of a second of silence
var source = myContext.createBufferSource();
source.buffer = buffer;
source.connect(myContext.destination);
source.onended = function()
{
console.log("WebAudio unlocked!");
isWebAudioUnlocked = true;
if (isWebAudioUnlocked && isHTMLAudioUnlocked)
{
console.log("WebAudio unlocked and playable w/ mute toggled on!");
window.removeEventListener("mousedown", unlock);
}
};
source.start();
// Unlock HTML5 Audio - load a data url of short silence and play it
// This will allow us to play web audio when the mute toggle is on
var silenceDataURL = "data:audio/mp3;base64,//MkxAAHiAICWABElBeKPL/RANb2w+yiT1g/gTok//lP/W/l3h8QO/OCdCqCW2Cw//MkxAQHkAIWUAhEmAQXWUOFW2dxPu//9mr60ElY5sseQ+xxesmHKtZr7bsqqX2L//MkxAgFwAYiQAhEAC2hq22d3///9FTV6tA36JdgBJoOGgc+7qvqej5Zu7/7uI9l//MkxBQHAAYi8AhEAO193vt9KGOq+6qcT7hhfN5FTInmwk8RkqKImTM55pRQHQSq//MkxBsGkgoIAABHhTACIJLf99nVI///yuW1uBqWfEu7CgNPWGpUadBmZ////4sL//MkxCMHMAH9iABEmAsKioqKigsLCwtVTEFNRTMuOTkuNVVVVVVVVVVVVVVVVVVV//MkxCkECAUYCAAAAFVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV";
var tag = document.createElement("audio");
tag.controls = false;
tag.preload = "auto";
tag.loop = false;
tag.src = silenceDataURL;
tag.onended = function()
{
console.log("HTMLAudio unlocked!");
isHTMLAudioUnlocked = true;
if (isWebAudioUnlocked && isHTMLAudioUnlocked)
{
console.log("WebAudio unlocked and playable w/ mute toggled on!");
window.removeEventListener("mousedown", unlock);
}
};
var p = tag.play();
if (p) p.then(function(){console.log("play success")}, function(reason){console.log("play failed", reason)});
}
window.addEventListener("mousedown", unlock);
关于iOS WebAudio仅适用于耳机,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21122418/