title: HTML5/JavaScript audio playlist
body: <p>I have found a nice tutorial on how to build a playlist using HTML5 and JavaScript in blog post <em><a href="http://blog.lastrose.com/html5-audio-video-playlist/" rel="nofollow">HTML5 Audio and Video and how to make a playlist</a></em>. I followed the instructions, but I did not get the correct outcome.</p>
  
  <p>This code SHOULD play all three audio files in order and stop when the last song has ended, but it what it actually does is autoplay the first file then stops when the first file is completed. What did I do wrong?</p>
  
  <pre><code>&lt;html&gt;
      &lt;body&gt;
          &lt;ul id="playlist"&gt;
              &lt;li class="active"&gt;
                  &lt;a href="http://www.codenamejupiterx.com/song/soundtest.mp3"&gt;
                     soundtest
                  &lt;/a&gt;
              &lt;/li&gt;
              &lt;li&gt;
                  &lt;a href="http://www.codenamejupiterx.com/song/soundtest2.mp3"&gt;
                      soundtest2
                  &lt;/a&gt;
              &lt;/li&gt;
              &lt;li&gt;
                  &lt;a href="http://www.codenamejupiterx.com/song/soundtest3.mp3"&gt;
                      soundtest3
                  &lt;/a&gt;
              &lt;/li&gt;
          &lt;/ul&gt;
  
          &lt;script&gt;
              var audio;
              var playlist;
              var tracks;
              var current;
  
              init();
              function init(){
                  current = 0;
                  audio = $('#audio');
                  playlist = $('#playlist');
                  tracks = playlist.find('li a');
                  len = tracks.length - 1;
                  audio[0].volume = .10;
                  audio[0].play();
                  playlist.find('a').click(function(e){
                      e.preventDefault();
                      link = $(this);
                      current = link.parent().index();
                      run(link, audio[0]);
                  });
                  audio[0].addEventListener('ended',function(e){
                      current++;
                      if(current == len){
                          current = 0;
                          link = playlist.find('a')[0];
                      }
                      else{
                          link = playlist.find('a')[current];
                      }
                      run($(link),audio[0]);
                  });
              }
  
              function run(link, player){
                  player.src = link.attr('href');
                  par = link.parent();
                  par.addClass('active').siblings().removeClass('active');
                  audio[0].load();
                  audio[0].play();
              }
          &lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>