无法在 iOS 上加载 htmlAudioElement 音频
Cannot load htmlAudioElement audio on iOS
我发现以下代码适用于桌面浏览器和 Android 但不适用于我的 iPad。
document.onclick = () => {
const testAudio = document.createElement("AUDIO");
testAudio.oncanplaythrough = () => {
console.log("CAN PLAY!");
testAudio.play();
}
console.log("LOADING AUDIO...");
testAudio.src = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3";
}
当我在 iPad 的 Chrome 检查器上看到控制台日志时,它只打印“LOADING AUDIO...”而从不打印“CAN PLAY”。它也不适用于我的 iPad 的 Safari。
这是什么原因,我该如何解决?
移动浏览器通常在加载某些内容时比较犹豫,但调用 load()
应该会强制他们这样做。
document.onclick = () => {
const testAudio = document.createElement("AUDIO");
testAudio.oncanplaythrough = () => {
console.log("CAN PLAY!");
testAudio.play();
}
console.log("LOADING AUDIO...");
testAudio.src = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3";
testAudio.load();
}
我发现以下代码适用于桌面浏览器和 Android 但不适用于我的 iPad。
document.onclick = () => {
const testAudio = document.createElement("AUDIO");
testAudio.oncanplaythrough = () => {
console.log("CAN PLAY!");
testAudio.play();
}
console.log("LOADING AUDIO...");
testAudio.src = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3";
}
当我在 iPad 的 Chrome 检查器上看到控制台日志时,它只打印“LOADING AUDIO...”而从不打印“CAN PLAY”。它也不适用于我的 iPad 的 Safari。
这是什么原因,我该如何解决?
移动浏览器通常在加载某些内容时比较犹豫,但调用 load()
应该会强制他们这样做。
document.onclick = () => {
const testAudio = document.createElement("AUDIO");
testAudio.oncanplaythrough = () => {
console.log("CAN PLAY!");
testAudio.play();
}
console.log("LOADING AUDIO...");
testAudio.src = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3";
testAudio.load();
}