在MediaElement播放器中动态添加播放列表项

3

目前我可以通过使用mep-feature-playlist插件的静态HTML代码将媒体项目添加到播放列表。

<video id="player" controls="controls" poster="media/poster.jpg" width="540" height="400">
    <Source src="media/video1.mp4" title="video1" type="video/mp4"></Source>
    <Source src="media/flashMovie.flv" title="Flash File" type="video/flv"></Source>
</video>

然而,我希望能够使用jQuery或JavaScript动态添加播放列表项。我有包含媒体源和类型的JSON对象。

提前感谢您的帮助。

2个回答

3
你可以简化开始DOM的过程。
<video id="player" controls="controls" poster="media/poster.jpg" width="540" height="400"></video>

然后,您可以将JSON附加到此DOM对象。
var someJson = [
    {
        'media_src': 'http://www.test.com/movie.mp4',
        'type': 'video/mp4',
        'title': 'mp4'
    },
    {
        'media_src': 'http://www.test.com/movie.flv',
        'type': 'video/flv',
        'title': 'flv'
    },
    {
        'media_src': 'http://www.test.com/movie.ogg',
        'type': 'video/ogg',
        'title': 'flv'
    }
];

// create an object that you can clone and add data to
var sourceObject = $('<source />');

// iterate thru the json
$.each(someJson, function (index_someJson, value_someJson) {

    // clone the original object
    var thisSourceObject = sourceObject.clone();

    // give it attributes based on the json item
    thisSourceObject.attr({
        'src': value_someJson.media_src,
        'type': value_someJson.type,
        'title': value_someJson.title
    });

    // append this to the '#player' element
    $('#player').append(thisSourceObject);

})

console.log($('#player')[0])

1
使用jQuery .append方法 http://api.jquery.com/append/
jQuery("#player").append("<Source src=\"media/newfile.flv\" title=\"new title\" type=\"video/flv\"></Source>");

对于一个JSON对象,您可以使用forEach循环来帮助构建此HTML以进行附加语句。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接