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

时间:2016-02-18 09:50:41

标签: jquery html audio playlist

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

function adify_paragraphs($content) {
    $tmp = $content;
    $tmp = explode('</p>',$content);
    $adcode = 'additional code 1';
    $adcode2 = 'additional code 2';

    if (count($tmp)>3):
        array_splice($tmp,1,0,$adcode);
    endif;
    if (count($tmp)>9):
        array_splice($tmp,7,0,$adcode2);
    endif;
    $cc = implode('<p/>',$tmp);
    return $cc;
}

add_filter( 'the_content', 'adify_paragraphs');

和jquery部分:

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

我怎样才能让这2个mp3一个接一个播放?谢谢你的帮助。

3 个答案:

答案 0 :(得分:1)

一种方法是使用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();

});

小提琴:https://jsfiddle.net/wpwddq30/

注意:这是未经测试的,只是为了了解事件

答案 1 :(得分:1)

  • 没有使用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>

答案 2 :(得分:0)

我确实改进了一些答案,所以最初的歌曲会随机开始,然后按照时间顺序跟着其他歌曲。

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

我正在考虑让所有歌曲以一种随机的方式重复播放,但我相信我需要做一系列的数字来计算它们或其他东西。仍然。它只是可选的!