如何使用 Jquery 制作简单的 HTML 播放列表

How to make a simple HTML playlist using Jquery

我需要一些帮助来使用音频 html 标签制作一个非常简单的 Jquery 播放列表。到目前为止我得到了他的:

<audio id="myAudio" preload="auto">
  Your user agent does not support the HTML5 Audio element.
</audio>

和 jquery 部分:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>

$(document).ready(function() {
  bgAudio = document.getElementById("myAudio");
  bgAudio.volume = 0.3;

  bgAudio.controls = true;
  bgAudio.loop = true;
  bgAudio.autoplay = true;

  bgAudio.src = "bg1.mp3";
  bgAudio.src = "bg2.mp3";
  bgAudio.play();

});
</script>

如何让这 2 个 mp3 一个接一个地播放?感谢您的帮助。

一种方法是使用 audio APIonended 事件绑定到 bgAudio 并在那里切换源。

JS:

$(document).ready(function() {

  var bgAudio = document.getElementById("myAudio");
  var src1 = "bg1.mp3";
  var src2 = "bg2.mp3";

  bgAudio.volume = 0.3;

  bgAudio.controls = true;
  bgAudio.loop = false;
  bgAudio.autoplay = true;
  bgAudio.src = src1;

  bgAudio.onended = function(){
    bgAudio.stop();
    bgAudio.src = src2;
    bgAudio.play();
  }

  bgAudio.play();

});

Fiddle: https://jsfiddle.net/wpwddq30/

注意:这是未经测试的,只是为了让大家了解一下事件

  • 没有要使用 animate 的元素,所以我制作了一个 div#bg 并将其包裹在音频元素周围。请记住,如果你想让一个元素以不透明度淡入,请确保该元素以 opacity:0

  • 开头
  • 表达式应该是:$('div#bg').animate({opacity: 1}, 1000);

  • 我看了你的问题...它不再有 animate() 了?

  • 播放列表在一个数组中。

  • 函数 player()document ready 上调用(因此您不需要移动设备无论如何都会忽略的 autoplay

  • 播放器将连续播放每个音频剪辑,并在完成播放列表后重新开始播放(loop 仅适用于一个文件,不适用于播放列表。所以你永远不会如果 loop=true)

  • 则进入下一个文件

片段

MNG- https://vocaroo.com/media_command.php?media=s0Ai9tr7779v&command=download_mp3
Righteous- https://vocaroo.com/media_command.php?media=s1dKHkbev0dJ&command=download_mp3
<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <title>35478017</title>
</head>

<body>
  <div id='bg' style="opacity:0">
    <audio id="xAudio" preload="auto"></audio>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <script>
    
    // This is a simple array of strings
    var playlist = [
      "https://vocaroo.com/media_command.php?media=s1H9fX5GI9Fa&command=download_mp3",
      "https://vocaroo.com/media_command.php?media=s0Ai9tr7779v&command=download_mp3",
      "https://vocaroo.com/media_command.php?media=s1dKHkbev0dJ&command=download_mp3"
    ];
    
    // Remember that element must start out at opacity:0
    // The duration should be only a number outside of the curly brackets
    $('div#bg').animate({
      opacity: 1
    }, 1000);


    $(document).ready(function() {
      var xA = document.getElementById("xAudio");
      xA.volume = 0.3;
      xA.controls = true;


      function player(x) {
        var i = 0;
        xA.src = playlist[x]; // x is the index number of the playlist array 
        xA.load();            // use the load method when changing src
        xA.play();
        xA.onended = function() { // Once the initial file is played it plays the rest of the files
          /* This would be better as a 'for' loop */
          i++;                   
          if (i > 2) {            //         ... Repeat
            i = 0;                //         ^
          }                       //         ^
          xA.src = playlist[i];   // Rinse,  ^
          xA.load();              // Lather, ^
          xA.play();              // and.....^
        }
      }
      player(0); // Call the player() function at 0 index of playlist array
    });
  </script>
</body>

</html>

我确实改进了一点答案,所以最初的歌曲将随机开始,其余歌曲按时间顺序跟随。

<script>

    var playlist = [
        "music/bg1.mp3",
        "music/bg2.mp3",
        "music/bg3.mp3",
        "music/bg4.mp3"
    ];

    var n = playlist.length-1;
    var r = 1 + Math.floor(Math.random() * n);

    $(document).ready(function() {
        var xA = document.getElementById("myAudio");
        xA.volume = 0.3;
        xA.controls = true;

        function player(x) {
            var i = 0;

            xA.src = playlist[r];
            xA.load();
            xA.play();
            xA.onended = function() {
                i++;
                if (i > n) {
                    i = 0;
                }

                xA.src = playlist[i];
                xA.load();
                xA.play();
            }
        }
        player(0);
    });
    </script>

我正在考虑让所有歌曲以随机播放的方式重复播放,但我相信我需要做一组数字来倒数或做些什么。仍然。这只是可选的!