Skip to content Skip to sidebar Skip to footer

Html5/javascript Audio Playlist

I have found a nice tutorial on how to build a playlist using HTML5 and JavaScript in blog post HTML5 Audio and Video and how to make a playlist. I followed the instructions, but I

Solution 1:

1) JavaScript code is using jQuery (those $(...) statements), so it must be imported:

<html><head><scripttype="text/javascript"src="http://code.jquery.com/jquery-1.8.2.min.js"></script></head><body>
...

2) The audio HTML element (the real "player") is missed:

<body><audioid="audio"preload="auto"tabindex="0"controls="" ><sourcesrc="http://www.codenamejupiterx.com/song/soundtest.mp3"></audio>
...

3) The code play only TWO songs. To play THREE:

...
len = tracks.length; //"-1" removed
...

4) The code play again and again the three songs. To stop it:

audio[0].addEventListener('ended',function(e){
    current++;
    if(current < len){
      link = playlist.find('a')[current];   
      run($(link),audio[0]);
    }
});

Solution 2:

Using jQuery I do have achieved this by using the following control.

Add the audio Control tag with following parameters:

<audio id="audio1" controls="controls" autoplay="autoplay">    </audio>

And in JavaScript:

jQuery(function($) {
    var supportsAudio = !!document.createElement('audio').canPlayType;
    if (supportsAudio) {

        url = URL.baseUrl + Books + authors + "/" + subject + "/data.json";
        $.getJSON(url, function(data){
            console.log("ddd"+JSON.stringify(data));

            var index = 0,
            trackCount = data.URL.length,
                npAction = $('#npAction'),
                npTitle = $('#npTitle'),
                audioid = $('#audio1').bind('play', function() {
                }).bind('ended', function() {

                if((index + 1) < trackCount) {
                    index++;
                    loadTrack(index);
                    audioid.play();
                }
                else {
                    audioid.pause();
                    index = 0;
                    loadTrack(index);
                }
            }).get(0),
                loadTrack = function(id) {
                    index = id;
                    audioid.src = data.URL[index].ayah;
                };
            loadTrack(index);
        });
    }
});

Post a Comment for "Html5/javascript Audio Playlist"