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

我有一个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。

改变你的

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

在那之前检查你是否使用alert()或console.log()正确传递了值