Jquery 在jPlayer中使用PHP中的JSON动态填充播放列表
我有一个PHP,可以在目录中创建mp3文件的JSON数组。PHP的JSON数组输出为:Jquery 在jPlayer中使用PHP中的JSON动态填充播放列表,jquery,jplayer,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/Mai
[{"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生成您的播放列表变量。更改您的
myPlaylist.add(value);
myPlaylist.add({title:$(this).attr("title"),mp3:$(this).attr("mp3")});
在此之前,请检查您是否已使用alert()或console.log()正确传递了值,因为它不起作用。。。我想是因为在这种情况下(在JSON数组的情况下,它是一个对象数组,这似乎不是JPlayer所期望的。我尝试将其转换为字符串数组,但也失败了……我认为我们试图解决的问题是如何让JPlayer通过JSON轻松获取播放列表。不是PHP创建的可能需要重新映射的JSON,而是e将jPlayerPlaylist()外部化构造器,并使它与一个简单的JSON播放列表一起工作。你可以回答自己的问题并解决它,这样你就不必在问题域中回答。别担心,如果你回答自己的问题,这只意味着你实际上是在努力学习。所以我不会发火。不过,仅供参考,如果你没有按照他们应该做的去做。记住这一点。尽管如此,我仍然很难让它正常工作。我不明白为什么它不工作,它看起来是正确的。我想我需要调试多一点。谢谢!感谢你的跟进。我试着使用你的解决方案,但我有两个问题,你的radio.php返回值是多少(格式)如果您没有得到关于setMedia()的错误,请在这里发布radio.php
文件内容,以充分利用您的答案?