javascriptjqueryjquery-eventsmediaelement.jsplaylist

MediaElement.js + jQuery with improved playlist (hopefully)


My JavaScript framework: jQuery 1.8.3

Using mediaelement.js and playlist to play audio (from this thread) mediaelement.js and custom playlist

I've been trying to figure out how to change the script (at bottom) to use this kind of list:

 <ul class="mejs-list">
     <li class="current"><a href="/media/file-1.mp3">Nice Name for file1</a> 
       other random text or html/img, file description etc </li>
    <li><a href="/media/file-2.mp3">Nice Name for file2</a>other text or html/img</li>
    <li><a href="/media/file-3.mp3">Nice Name for file3</a>other text or html/img</li>
</ul>

instead of this one

<ul class="mejs-list">
  <li>/media/file-1.mp3</li>
  <li class="current">/media/file-2.mp3</li>
  <li>/media/file-3.mp3</li>
</ul>

This script turns this list into a playlist, and works very well, but creates links displaying full path, I prefer to grab the src from the link instead of just the text inside the li, so I can use better names.

<script>
$(function(){
    $('audio').mediaelementplayer({
        success: function (mediaElement, domObject) {
            mediaElement.addEventListener('ended', function (e) {
                mejsPlayNext(e.target);
            }, false);
        },
        keyActions: []
    });

    $('.mejs-list li').click(function() {
        $(this).addClass('current').siblings().removeClass('current');
        var audio_src = $(this).text();
        $('audio#mejs:first').each(function(){
            this.player.pause();
            this.player.setSrc(audio_src);
            this.player.play();
        });
    });

});

function mejsPlayNext(currentPlayer) {
    if ($('.mejs-list li.current').length > 0){ // get the .current song
        var current_item = $('.mejs-list li.current:first'); // :first is added if we have few .current classes
        var audio_src = $(current_item).next().text();
        $(current_item).next().addClass('current').siblings().removeClass('current');
        console.log('if '+audio_src);
    }else{ // if there is no .current class
        var current_item = $('.mejs-list li:first'); // get :first if we don't have .current class
        var audio_src = $(current_item).next().text();
        $(current_item).next().addClass('current').siblings().removeClass('current');
        console.log('elseif '+audio_src);
    }

    if( $(current_item).is(':last-child') ) { // if it is last - stop playing
        $(current_item).removeClass('current');
    }else{
        currentPlayer.setSrc(audio_src);
        currentPlayer.play();
    }
}
</script>

If there's a better way to do it using jQuery and mediaelement.js I'm open to suggestions.

p.s.
Would also be a plus if I could have previous and next track links, but it's not required.


Solution

  • Figured it out and posting answer for everyone's benefit.

    HTML

    <audio id="mejs" src="track1.mp3" type="audio/mp3" controls></audio>
    <ul class="mejs-list">
      <li id="track1.mp3">Track1</li>
      <li id="track2.mp3">Track2</li>
      <li id="track3.mp3">Track3</li>
    </ul>
    

    javascript

        <script>
            $(function(){
                $('audio').mediaelementplayer({
                    success: function (mediaElement, domObject) {
                        mediaElement.addEventListener('ended', function (e) {
                            mejsPlayNext(e.target);
                        }, false);
                    },
                    keyActions: []
                });
    
                $('.mejs-list li').click(function() {
                    $(this).addClass('current').siblings().removeClass('current');
                    var audio_src = this.id;
                    $('audio#mejs:first').each(function(){
                        this.player.pause();
                        this.player.setSrc(audio_src);
                        this.player.play();
                    });
                });
    
            });
    
            function mejsPlayNext(currentPlayer) {
                if ($('.mejs-list li.current').length > 0){ // get the .current song
                    var current_item = $('.mejs-list li.current:first'); // :first is added if we have few .current classes
                    var audio_src = $(current_item).next().text();
                    $(current_item).next().addClass('current').siblings().removeClass('current');
                    console.log('if '+audio_src);
                }else{ // if there is no .current class
                    var current_item = $('.mejs-list li:first'); // get :first if we don't have .current class
                    var audio_src = $(current_item).next().text();
                    $(current_item).next().addClass('current').siblings().removeClass('current');
                    console.log('elseif '+audio_src);
                }
    
                if( $(current_item).is(':last-child') ) { // if it is last - stop playing
                    $(current_item).removeClass('current');
                }else{
                    currentPlayer.setSrc(audio_src.match('http.*\.mp3'));
                    currentPlayer.play();
                }
            }
            </script>
    

    NOTE: this dose not validate HTML standerds(WC3), but it dose work, Tested Chrome, Safari, Firefox on mac, latest versions Still looking for HTML compliant version to validate.