如何使用 mediaelement.js 获取 HTML5 音频播放列表?

发布于 2024-11-15 22:53:38 字数 98 浏览 4 评论 0原文

我尝试在 mediaelement.js 中搜索音频播放列表的示例。但我没有找到,mediaelement.js 支持音频播放列表吗?如果是这样,请支持我的示例代码或链接。非常感谢。

I tried to search the example of audio playlist in mediaelement.js. But i found none, is mediaelement.js supports audio playlist? If so, please kindly support me the sample code or links. Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

司马昭之心 2024-11-22 22:53:38

我设法获得了一个超级基本(阅读:hacky)的播放列表演示,该演示在我正在使用的 WordPress 主题中运行。您所要做的就是使用一组 ID、一个类或本例中的 标记来设置多个媒体元素。

然后,您可以使用 JS 查看播放器何时结束(停止播放音乐)并运行一些基本条件来查看哪个播放器正在播放、下一个播放器应该播放什么以及如果没有下一个播放器,则从头开始。这又很hacky,但它似乎对我有用。

HTML:

<figure class="entry-audio">
    <audio id="wp_mep_1" controls="controls">
        <source src="linkto/music.mp3" type="audio/mp3" />
        <object width="100%" height="23" type="application/x-shockwave-flash" data="flashmediaelement.swf">
            <param name="movie" value="flashmediaelement.swf" />
            <param name="flashvars" value="controls=true&file=linkto/music.mp3" />
        </object>
    </audio>
</figure>
<!-- .entry-audio -->

<figure class="entry-audio">
    <audio id="wp_mep_2" controls="controls">
        <source src="linkto/music.mp3" type="audio/mp3" />
        <object width="100%" height="23" type="application/x-shockwave-flash" data="flashmediaelement.swf">
            <param name="movie" value="flashmediaelement.swf" />
            <param name="flashvars" value="controls=true&file=linkto/music.mp3" />
        </object>
    </audio>
</figure>
<!-- .entry-audio -->

<figure class="entry-audio">
    <audio id="wp_mep_3" controls="controls">
        <source src="linkto/music.mp3" type="audio/mp3" />
        <object width="100%" height="23" type="application/x-shockwave-flash" data="flashmediaelement.swf">
            <param name="movie" value="flashmediaelement.swf" />
            <param name="flashvars" value="controls=true&file=linkto/music.mp3" />
        </object>
    </audio>
</figure>
<!-- .entry-audio -->

JavaScript:

jQuery(function (jQuery) {
    // make an array for the mediaelement players
    var mediaElementPlayers = new Array();

    // run mediaelement.js (Ignore options)
    jQuery('audio').mediaelementplayer({
        iPadUseNativeControls: false,
        iPhoneUseNativeControls: false,
        AndroidUseNativeControls: false,
        pauseOtherPlayers: true,
        success: function (mediaElement, domObject) {
            // add this mediaelement to the mediaElementPlayers array
            mediaElementPlayers.push(mediaElement);
            console.log(mediaElement);
            mediaElement.addEventListener('ended', function (e) {
                playNext(e.target);
            }, false);
        },
        keyActions: []
    })

    // find the current player and start playing the following player
    function playNext(currentPlayer) {
        for (i = 0; i < mediaElementPlayers.length; i++) {
            if (mediaElementPlayers[i] == currentPlayer) {
                if (mediaElementPlayers[i + 1] == undefined) { // If this is the last player Start from the beginning
                    mediaElementPlayers[0].play();
                } else { // Start the next player
                    mediaElementPlayers[i + 1].play();
                }
            }
        }
    }
});

I managed to get a super basic (read: hacky) demo of a playlist working in a WordPress theme I’ve got going. All you have to do is setup multiple media elements using a set of IDs, a class or in this case the <audio> tag.

Then you can use JS to see when a player has ended (stopped playing music) and run some basic conditionals to see which player is playing, what player should play next and if there is no next player, start over from the beginning. Again this is very hacky, but it seems to be working for me.

HTML:

<figure class="entry-audio">
    <audio id="wp_mep_1" controls="controls">
        <source src="linkto/music.mp3" type="audio/mp3" />
        <object width="100%" height="23" type="application/x-shockwave-flash" data="flashmediaelement.swf">
            <param name="movie" value="flashmediaelement.swf" />
            <param name="flashvars" value="controls=true&file=linkto/music.mp3" />
        </object>
    </audio>
</figure>
<!-- .entry-audio -->

<figure class="entry-audio">
    <audio id="wp_mep_2" controls="controls">
        <source src="linkto/music.mp3" type="audio/mp3" />
        <object width="100%" height="23" type="application/x-shockwave-flash" data="flashmediaelement.swf">
            <param name="movie" value="flashmediaelement.swf" />
            <param name="flashvars" value="controls=true&file=linkto/music.mp3" />
        </object>
    </audio>
</figure>
<!-- .entry-audio -->

<figure class="entry-audio">
    <audio id="wp_mep_3" controls="controls">
        <source src="linkto/music.mp3" type="audio/mp3" />
        <object width="100%" height="23" type="application/x-shockwave-flash" data="flashmediaelement.swf">
            <param name="movie" value="flashmediaelement.swf" />
            <param name="flashvars" value="controls=true&file=linkto/music.mp3" />
        </object>
    </audio>
</figure>
<!-- .entry-audio -->

JavaScript:

jQuery(function (jQuery) {
    // make an array for the mediaelement players
    var mediaElementPlayers = new Array();

    // run mediaelement.js (Ignore options)
    jQuery('audio').mediaelementplayer({
        iPadUseNativeControls: false,
        iPhoneUseNativeControls: false,
        AndroidUseNativeControls: false,
        pauseOtherPlayers: true,
        success: function (mediaElement, domObject) {
            // add this mediaelement to the mediaElementPlayers array
            mediaElementPlayers.push(mediaElement);
            console.log(mediaElement);
            mediaElement.addEventListener('ended', function (e) {
                playNext(e.target);
            }, false);
        },
        keyActions: []
    })

    // find the current player and start playing the following player
    function playNext(currentPlayer) {
        for (i = 0; i < mediaElementPlayers.length; i++) {
            if (mediaElementPlayers[i] == currentPlayer) {
                if (mediaElementPlayers[i + 1] == undefined) { // If this is the last player Start from the beginning
                    mediaElementPlayers[0].play();
                } else { // Start the next player
                    mediaElementPlayers[i + 1].play();
                }
            }
        }
    }
});
无远思近则忧 2024-11-22 22:53:38

不,没有播放列表。
如果您正在寻找拥有该功能的播放器,请查看此处:http://praegnanz.de/html5video/
此外,您只需使用 mediaelementjs JavaScript API 即可轻松制作播放列表。这是非常简单的任务,IMO 不需要额外的支持。

Nope, no playlist.
If you looking for player that has one, check here: http://praegnanz.de/html5video/
Also, you can easy make playlist simply by using mediaelementjs JavaScript API. It is pretty simple task, IMO there is no need for extra support for that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文