HTML5 音频多重播放

标签 html audio copy

我正在编写一个 Javascript 游戏,我想多次使用一个声音。我可以通过将一个声音多次加载到数组来做到这一点。但我想一次加载一个声音并将其“复制”到数组中,这样我就可以多次播放它。这是我现在拥有的方法:

this.list = [
        "LaserShot", //http://www.freesound.org/samplesViewSingle.php?id=39459
        "Ding", //http://www.freesound.org/samplesViewSingle.php?id=5212
        "Rocket" //http://www.freesound.org/samplesViewSingle.php?id=47252 -> http://creativecommons.org/licenses/sampling+/1.0/
    ];

...

for (i in this.list) {
        this.sounds[this.list[i]] = new Array();

        for (var j = 0; j < this.channels; j++) {
            this.sounds[this.list[i]][j] = new Audio("./sounds/"+ this.list[i] + type);
        }
    }

我只想这样做:

for (i in this.list) {
        this.sounds[this.list[i]] = new Array();

var tempAudio = new Audio("./sounds/"+ this.list[i] + type);

        for (var j = 0; j < this.channels; j++) {
            this.sounds[this.list[i]][j] = realCopyOfTempAudio;
        }
    }

非常感谢。

最佳答案

我的经验:

最好创建更多具有相同来源的音频 html 标签。我是 js 的粉丝,但这次最好以 html 形式使用 html 音频标签。

我制作了一个重复的音频标签并装饰了我的需求。如果您想在一秒钟内多次播放相同的声音,请添加更多克隆。

你也可以修复自动播放错误 (而不是 EXE_JUST_ONE_TIME 你可以使用覆盖点击事件,现在不重要):

    <audio controls id="LaserShot" >
     <source src="LaserShot.mp3" type="audio/mpeg">
     <source src="LaserShot.ogg" type="audio/ogg">
    </audio>
     <audio controls id="LaserShot_CLONE" >
     <source src="LaserShot.mp3" type="audio/mpeg">
     <source src="LaserShot.ogg" type="audio/ogg">
    </audio>

<script>

  var EXE_JUST_ONE_TIME = false;

  document.addEventListener("click" , function(e) {

    if (EXE_JUST_ONE_TIME == false){

      EXE_JUST_ONE_TIME = true;

      document.getElementById("LaserShot").play();
      document.getElementById("LaserShot").pause();

      document.getElementById("LaserShot_CLONE").play();
      document.getElementById("LaserShot_CLONE").pause();

      // Buffering in progress 
      // now you can play programmability from code
      // One click or touch can prepare max 6 audios 

    }
 }


Last part (need to be handled) this handler works only for one clone:


    var play_shoot = function(){

     if (document.getElementById('LaserShot').duration > 0 &&
       !document.getElementById('LaserShot').paused) {

      document.getElementById('LaserShot_CLONE').play();

     } else {

      document.getElementById('LaserShot').play();

     }

    }

关于HTML5 音频多重播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5194898/

相关文章:

javascript - 将富文本格式字符串复制为格式化文本

html - css 闪存卡(无 Javascript)

javascript - 在启动时使用 Meteor FS Collections 将 wav 转换为 mp3

powershell - 如何使用 PowerShell 从 SharePoint 下载文件?

audio - 检测音频中的特定声音

ios - 如何确定两个设备是否同时收听音频

C# 使用其他域/用户名/密码将文件复制到另一个目录

javascript - 聊天应用程序好友存在状态 View 刷新响应设计?

javascript - HTML5 使用 for 循环在 Canvas 上绘制图片?

css - 页脚没有响应