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

Play audio clips on mobile and tablet browsers

本文关键字:播放 音频 片段 浏览器 手机 平板电脑      更新时间:2023-12-09

我使用以下代码在单击图像时播放音频片段。它可以在桌面上工作,但不能在手机或平板电脑浏览器上工作,我如何才能让它在它们上工作?

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'" />");
}

也许您可以使用HTML音频标记:

<!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>

你可以在这里查看一个实例。

编辑

所以把你的最后一行改成:

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