mediaelement.js and custom playlist

大憨熊 提交于 2019-12-20 08:56:16

问题


I have such code (for example):

<script>
$('audio').mediaelementplayer();
</script>

<audio id="mejs" controls="controls">
    <source src="/media/file-2.mp3" type="audio/mp3" />
</audio>

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

I need to play audio files from the list by queue (one after another).

How can I change the source of the audio for the next file from the list after finishing playing current file and start playing the next one?

Update: related topic about mediaelement.js and playlist but the answer is about using many mediaelementplayer() instances.


回答1:


Code for making basic playlist for mediaelement.js:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HTML5 MediaElement</title>

<script src="../build/jquery.js"></script>
<script src="../build/mediaelement-and-player.min.js"></script>

<link rel="stylesheet" href="../build/mediaelementplayer.min.css" />

<style>
    .mejs-list li:hover {
        background: #ffa;
        cursor: pointer;
    }
    .mejs-list li.current {
        background: #cddfff;
    }
</style>

<script>
    $(function(){
        $('video,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>

</head>

<body>

<audio id="mejs" src="../media/audio-01.mp3" type="audio/mp3" controls="controls"></audio>
<ul class="mejs-list">
    <li class="current">../media/audio-01.mp3</li>
    <li>../media/audio-02.mp3</li>
    <li>../media/audio-03.mp3</li>
</ul>

</body>
</html>



回答2:


Now there is a plugin that solves this task - https://github.com/duozersk/mep-feature-playlist




回答3:


Have a look at the YouTube Playlist for MediaElement.js. It provides a playlist for YouTube videos but can be easily updated to support other video formats



来源:https://stackoverflow.com/questions/11867779/mediaelement-js-and-custom-playlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!