我使用该线程中的一些代码创建了一个网站,在该网站上随机选择的声音在图像点击时播放:Play random sound without repeat。对我来说重要的是,在整个系列循环播放之前,声音不会重复,这就是我采用此解决方案的原因:http: //jsfiddle.net/cbuckley/FdLge/
它在 Google Chrome 中就像一个魅力,但由于某种原因,它似乎无法在其他网络浏览器(如 Safari)中播放音频。我知道它不适用于移动设备,但我没想到会这样。有什么想法我可以做些什么来解决它?
这是我的网站,供参考:http ://comfortinajar.com/
html如下:
<div id="element"></div>
<img src="Images/Screen.jpg" id=test>
脚本是:
<script>
var sounds = [];
// Return the full list of URLs in random order
function getSounds() {
return [
'Audio/Comfort1.mp3',
'Audio/Comfort2.mp3',
'Audio/Comfort3.mp3',
'Audio/Comfort4.mp3',
'Audio/Comfort5.mp3',
'Audio/Comfort6.mp3',
'Audio/Comfort7.mp3',
'Audio/Comfort8.mp3',
'Audio/Comfort9.mp3',
'Audio/Comfort10.mp3',
'Audio/Comfort11.mp3',
'Audio/Comfort12.mp3',
'Audio/Comfort13.mp3',
'Audio/Comfort14.mp3',
'Audio/Comfort15.mp3',
"Audio/Comfort16.mp3",
"Audio/Comfort17.mp3",
"Audio/Comfort18.mp3"].sort(function () {
// http://stackoverflow.com/a/18650169/283078
return (.5 - Math.random());
});
}
// Play the next sound
function playSound() {
if (!sounds.length) {
// Get the list of sounds (random order)
sounds = getSounds();
}
// Pops a URL every time, ensuring all are played exactly once
$("#element").html(
'<embed src="' + sounds.pop() +
'" hidden="true" autostart="true" />'
);
// Once all the URLs have been popped, the array is repopulated
}
$('#test').click(playSound);
</script>
该代码当前还导致页面跳转,第一次加载时 div 标记所在的位置(在页面的最底部),之后仍然有一块白色,并且加载的第一个声音由于某种原因淡入(降低可听度),但我不太关心这些问题。我真正关心的是让声音在所有浏览器中都能正常工作。让我知道您是否可以提供帮助!