我已经阅读了很多关于此事的主题和问题,但我找不到解决方案。我可以在每个桌面浏览器上的每个Android浏览器上播放声音,但不能在iPhone 5c上的Safari上播放声音。 我正在尝试使用Web Audio API(我绝对不是专家),这是我的代码:
window.addEventListener ('load', loadSound);
canvas.addEventListener ('touchend', loadSound);
canvas.addEventListener ('mouseup', loadSound);
window.AudioContext = window.AudioContext || window.webkitAudioContext;
var audioContext = new window.AudioContext();
var playSoundBuffer;
var source;
function loadSound() {
setTimeout(function() {
var request = new XMLHttpRequest();
var audioURL = "media/sound/WW_start.wav";
request.open("GET", audioURL, true);
request.responseType = "arraybuffer";
request.onload = function() {
audioContext.decodeAudioData(request.response, function(buffer) {
playSoundBuffer = buffer;
playSound();
}, function(error) {
console.error("Error in sound", error);
});
request.send();
}
}, 50);
}
function playSound() {
source = audioContext.createBufferSource();
source.buffer = playSoundBuffer;
source.connect(audioContext.destination);
source.start(0);
}
我也尝试过source.noteOn(),source.noteOn(0),webkitAudioContext ...
答案 0 :(得分:0)
咆哮是解决方案:
<script src="lib/howler.js-master/howler.js-master/dist/howler.js"></script>
...
var sound = new Howl({
src: ['file.wav', 'file.mp3', 'file.ogg'],
volume: 1,
buffer: false
});
window.addEventListener('mousedown', function() {
sound.play();
}, false);