【问题标题】:Javascript audio loopJavascript 音频循环
【发布时间】:2016-01-21 17:34:11
【问题描述】:

我想在后台播放音频 3 次,3 次后音频将自动停止。 我已经尝试过这段代码,但它没有用。 HTML 代码:

<audio id='beep' controls>
<source src="beep-02.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>

JAVASCRIPT:

var count = 1
document.getElementById('beep').addEventListener('ended', function(){
   this.currentTime = 0;
   if(count <= 3){
      this.play();
   }
   count++;
}, false);

谢谢。

【问题讨论】:

  • 当你说它不起作用时,它做了什么?
  • 它肯定是重复的,但那里接受的答案是可疑的。除非你真的不能依赖 ended 事件,否则我认为你不希望不得不依赖 setInterval or setTimeout 来匹配音频的持续时间。

标签: javascript html audio mp3 html5-audio


【解决方案1】:

您可以尝试使用 onended: 参考:http://www.w3schools.com/tags/av_event_ended.asp 或者您可能需要在再次调用 play() 之前增加计数

var count = 1;
var audio = document.getElementById('beep');
audio.onended = function() {
    if(count <= 3){
      count++;
      this.play();
   }
};

【讨论】:

  • ended 事件添加侦听器或为onended 设置显式处理程序之间应该没有功能区别...
  • 我不是 100% 确定,但如果您使用事件监听器,我认为您必须调用 bind() 才能在函数内部使用“this”?如:audio.addEventListener('keydown', function(e) { this.handle_keydown(e); }.bind(this), false);参考:stackoverflow.com/questions/13996263/…
  • 不,函数的上下文,this, will be set to the event's currentTarget` 在这种情况下,this 将是音频元素。 developer.mozilla.org/en-US/docs/Web/API/EventTarget/… "当使用 addEventListener() 将处理程序函数附加到元素时,处理程序内 this 的值是对该元素的引用。它与传递给的事件参数的 currentTarget 属性的值相同处理程序。”
  • 阅读参考,似乎该函数需要成为元素的一部分,而不是设置为新的(匿名)函数“如果在元素上指定了事件属性(例如,onclick)在 HTML 源代码中,属性值中的 JavaScript 代码有效地包装在一个处理函数中,该处理函数以与使用 addEventListener() 一致的方式绑定 this 的值“但也许我读错了......我会有到时候给它一个测试
  • 我在jsfiddle.net/jorw3yfn下面的答案中附加了一个示例jsfiddle,所以我没有被误解;是的,我认为这两种方法都可以正常工作并且通常是等效的。但我不认为“问题”是作者使用 addEventListner 而不是设置 onended。即这里的答案实际上并没有解释这是如何解决任何问题的。否则,这两种方法各有利弊。
【解决方案2】:

试试这个:

var count = 0;
document.getElementById('beep').addEventListener('ended', function(){
   if(count <= 3){
      this.play();
      count++;
   }
}, false);

【讨论】:

    【解决方案3】:

    1) 如果您确实想在后台播放,您还需要添加 autoplay 属性。

    <audio id='beep' controls autoplay>
      <source src="beep-02.mp3" type="audio/mpeg">
      Your browser does not support the audio element.
    </audio>
    

    2) 这应该与您编写的一样工作,但在某些浏览器中,如果您愿意,可能需要重置播放头:this.currentTime = 0

    JAVASCRIPT:

    var count = 0
    document.getElementById('beep').addEventListener('ended', function(){
      count++;
      if (count < 3) {
        this.currentTime = 0; // may be needed in some browsers to reset
        this.play();
      }
    }, false);
    

    为了记录,我之前建议使用loop 属性并在ended 触发3 次后停止,但是当我测试它时,loop 阻止ended 永远触发。

    示例 http://jsfiddle.net/jorw3yfn/

    【讨论】:

      【解决方案4】:

      jQuery(document).ready(function($) {
        // the audio loop trick is "alertCount" value, try playing around with it
        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
      
      })
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
      <audio controls id="audio">
          <source src="http://soundbible.com/grab.php?id=1599&type=mp3" type="audio/mpeg">
      </audio>

      注意:忽略错误

      【讨论】:

        猜你喜欢
        • 2013-01-05
        • 2019-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-17
        • 1970-01-01
        相关资源
        最近更新 更多