首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在页面上播放每个音频元素(HTML、JQuery、Javascript)

问如何在页面上播放每个音频元素(HTML、JQuery、Javascript)
EN

Stack Overflow用户
提问于 2015-01-17 02:42:30
回答 3查看 2.1K关注 0票数 1

好的,我有一个有多个音频元素的页面。我正在寻找一种方式来玩每一个,从第一个,等待它完成播放,然后转移到下一个音频元素,直到最后一个元素。有可能做到这一点吗?谢谢!

编辑:

我不能添加代码示例,因为音频元素都是由PHP动态添加的,所以在此之前它是一个空文件。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-01-17 03:56:24

基于@的回答,我实际上想出了一个解决方案:

代码语言:javascript
复制
var index = 0;
var audioElements = document.getElementsByTagName('audio');

function playNext(index) {
    audioElements[index].play();
    $(audioElements[index]).bind("ended", function(){
        index++;
        if(index < audioElements.length){
            playNext(index);          
        }
    });

}

$(document).ready(function() {
    $("#swig").click(function() {
        audioElements[index].play();

        $(audioElements[index]).bind("ended", function(){
             index = index + 1;
             if(index < audioElements.length){
                playNext(index);          
             }                        
        });


    });
});

我只是盲目地编码这个。如果有人能详细说明这件事的原因,我会很感激。

票数 1
EN

Stack Overflow用户

发布于 2015-01-17 02:49:09

您可以通过将每个音频元素存储在队列中来做到这一点。假设您将每个元素存储在数组audioElements中。

代码语言:javascript
复制
var index = 0;
function playNext(index){
  audioElements[index].play(); //this will play the element
  audioElements[index].on('ended', function(){ //this will bind a function to the "ended" event
    //increment the index to target the next element
    index++;
    if(index < audioElements.length){
      //plays the next song and binds the function to the ended event again until the queue is empty
      playNext(index);          
    }
  }
}
playNext(index);
票数 2
EN

Stack Overflow用户

发布于 2018-05-14 08:12:16

代码语言:javascript
复制
jQuery(document).ready(function($) {
 // the loop trick belong to "alertCount": 4 inside the json, changing that 
will change the loop counter
  var alarmData = {
   "alertCount": 3,
   "trigger": [{
    "id": 47,
    "status": "active",
    "create_uid": 1,
    "create_date": "2018-04-19 01:38:39.59",
    "region": null,
    "acknowledge_by": "Hashmat",
    "write_uid": 1,
    "write_date": "2018-04-23 03:35:04.745",
    "date": "2018-04-19",
    "serial": null,
    "sim": null,
    "acknowledge_date": "2018-04-19",
    "station_id": null,
    "station_alerts": 72,
    "sound_triggered": null
  },
  {
    "id": 48,
    "status": "active",
    "create_uid": 1,
    "create_date": "2018-04-19 01:38:39.59",
    "region": null,
    "acknowledge_by": "Hashmat",
    "write_uid": 1,
    "write_date": "2018-04-23 03:35:04.745",
    "date": "2018-04-19",
    "serial": null,
    "sim": null,
    "acknowledge_date": "2018-04-19",
    "station_id": null,
    "station_alerts": 72,
    "sound_triggered": null
  },
  {
    "id": 49,
    "status": "active",
    "create_uid": 1,
    "create_date": "2018-04-19 01:38:39.59",
    "region": null,
    "acknowledge_by": "Hashmat",
    "write_uid": 1,
    "write_date": "2018-04-23 03:35:04.745",
    "date": "2018-04-19",
    "serial": null,
    "sim": null,
    "acknowledge_date": "2018-04-19",
    "station_id": null,
    "station_alerts": 72,
    "sound_triggered": null
  },
  {
    "id": 50,
    "status": "active",
    "create_uid": 1,
    "create_date": "2018-04-19 01:38:39.59",
    "region": null,
    "acknowledge_by": "Hashmat",
    "write_uid": 1,
    "write_date": "2018-04-23 03:35:04.745",
    "date": "2018-04-19",
    "serial": null,
    "sim": null,
    "acknowledge_date": "2018-04-19",
    "station_id": null,
    "station_alerts": 72,
    "sound_triggered": null
  }
]

  };

var audioElements = document.getElementsByTagName('audio');


  $.each(alarmData.trigger, function(index, alart_value) {


// update the status
// once the sounds are played (once for each alarm), then update the status 
 for each alarm triggered
  var countAlert = alarmData.alertCount;



if (alart_value.sound_triggered == null || alart_value.sound_triggered === 
 false) {

  audioElements[index].play(); // play the alert for first time
  audioElements[index].onended = function() { // once finished, then play it 
 according the number of alerts from backend(for jsfiddle purpose we use 
 local data source)
    if (index < --countAlert) {
      this.play();
    }
  };
}



 }); // close foreach loop for alertData.trigger

  })

JSFIDDLE Waziri/9t0sexo2/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27995654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档