在手机和平​​板电脑浏览器上播放音频片段

时间:2013-11-28 16:59:53

标签: javascript

我正在使用以下代码在单击图像时播放音频剪辑。 它适用于桌面设备,但不适用于移动设备或平板电脑浏览器,我如何才能使用它们?

var sounds = [


"AUDIO URL",
"AUDIO URL",
"AUDIO URL",
"AUDIO URL",


          ],
oldSounds = [];

var playSounds = function () {
var index = Math.floor(Math.random() * (sounds.length)),
    thisSound = sounds[index];

    oldSounds.push(thisSound);
    sounds.splice(index, 1);

    if (sounds.length < 1) {
        sounds = oldSounds.splice(0, oldSounds.length);
    }

    $("#element").html("<embed src=\"" + thisSound + "\" hidden=\"true\" autostart=\"true\" />");
}

1 个答案:

答案 0 :(得分:0)

也许you can use HTML audio tag

<!DOCTYPE html>
<html>
<body>
<audio controls>
  <source src="horse.ogg" type="audio/ogg">
  <source src="horse.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>
</body>
</html>

您可以查看实时示例here

修改

所以改为你的最后一行:

$("#element").html("<audio autoplay><source src=\"" + thisSound + "\" type=\"audio\/mp4\"><\/audio>");