在 jPlayer 中使用来自 PHP 的 JSON 动态填充播放列表

2023-11-24

我有一个 PHP,可以在目录中创建 mp3 文件的 JSON 数组。 PHP 的 JSON 数组输出为:

[{"title":"Kalimba","mp3":"/path/to/mydirectory/Kalimba.mp3"},{"title":"Maid with  the Flaxen Hair","mp3":"/path/to/mydirectory/Maid with the Flaxen Hair.mp3"},{"title":"Sleep Away","mp3":"/path/to/mydirectory/Sleep Away.mp3"}]

好吧,这似乎是 JQuery.jPlayer 所期望的。

现在在一个基本的 jplayer.js 文件中我有:

$(document).ready(function(){

new jPlayerPlaylist({
    jPlayer: "#jquery_jplayer_1",
    cssSelectorAncestor: "#jp_container_1"
}, [
    //I guess my JSON array should come here
    // but no idea on how I put it in...
], {
    swfPath: "../js",
    supplied: "mp3",
    wmode: "window"
});
});

我的问题是我无法将 JSON 数组放在它应该在的位置(请参阅 js 代码中的注释)

任何帮助将非常感激! 原谅我的英语,这不是我的母语 提前致谢

编辑并解决

大家好, 对于那些有兴趣的人,我找到了一个解决方案: 我的JS文件是:

$(document).ready(function(){
    var cssSelector = {
        jPlayer: "#jquery_jplayer_1", 
        cssSelectorAncestor: "#jp_container_1"
    };
    var playlist = []; // Empty playlist
    var options = {
        swfPath: "./js", 
        supplied: "mp3"
    };
    var myPlaylist = new jPlayerPlaylist(cssSelector, playlist, options);
    $.getJSON("../PHP/radio.php",function(data){  // get the JSON array produced by my PHP
        $.each(data,function(index,value){
            myPlaylist.add(value); // add each element in data in myPlaylist
        })
    }); 
});

你为什么不直接输入 javascript:

var playlist = [{"title":"Kalimba","mp3":"/path/to/mydirectory/Kalimba.mp3"},{"title":"Maid with  the Flaxen Hair","mp3":"/path/to/mydirectory/Maid with the Flaxen Hair.mp3"},{"title":"Sleep Away","mp3":"/path/to/mydirectory/Sleep Away.mp3"}];

$(document).ready(function(){

  new jPlayerPlaylist({
    jPlayer: "#jquery_jplayer_1",
    cssSelectorAncestor: "#jp_container_1"
  },
  playlist,
  {
    swfPath: "../js",
    supplied: "mp3",
    wmode: "window"
  });
});

您甚至可以使用 PHP 来生成您的playlist var.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 jPlayer 中使用来自 PHP 的 JSON 动态填充播放列表 的相关文章