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

时间:2011-12-05 23:15:23

标签: jquery jplayer

我有一个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代码中的注释)

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

EDIT&解决

大家好, 对于那些有兴趣的人我找到了解决方案: 我的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
        })
    }); 
});

2 个答案:

答案 0 :(得分:2)

为什么你不加入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。

答案 1 :(得分:0)

改变你的

 myPlaylist.add(value);

myPlaylist.add({title:$(this).attr("title"),mp3:$(this).attr("mp3")});

之前检查您是否使用alert()或console.log()

正确传递了值