8

I have a PHP that create a JSON array of mp3 files in a directory. The JSON array output from PHP is :

[{"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"}]

Fine, it seems to be what is expected by JQuery.jPlayer.

Now in a basic jplayer.js file I have :

$(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"
});
});

My problem is I can't put my JSON array in the place it should be (see comments in js code)

Any help would be very appreciate ! Forgive my english it's not my native tongue Thanks in advance

EDIT & SOLVED

Hi all, For those who are interested I find a solution : My JS file is :

$(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
        })
    }); 
});
Community
  • 1
  • 1
phron
  • 1,795
  • 17
  • 23
  • 1
    you can answer your own question and solve it so you dont have to answer in the question field – samura Dec 07 '11 at 23:29
  • Don't worry, if you are answering your own questions, it just means you are actually going out and making an effort to learn. So no flames from me. However, FYI, people will get on your case if you don't do things as they should so.. keep that in mind. None the less, I am still having problems getting this to work, however.. I can't see why it doesn't work, it looks right. I think I need to debug a bit more. Thanks though! Appreciate the follow up. – John Drefahl Dec 27 '11 at 11:35
  • I trying use your solution but I have two question, what your radio.php return ( Format ) and if you no get the error about setMedia() – AFetter May 22 '12 at 14:11
  • Can you please post the `radio.php` file content in here to make benefit of your answer? – Black Block Jul 20 '15 at 12:32

2 Answers2

2

Why dont you just put in 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"
  });
});

You can even use PHP to generate your playlist var.

samura
  • 4,375
  • 1
  • 19
  • 26
  • Hi, just because it doesn't work... I think it's because in this case (and in the case of a JSON array, it's an array of Objects, and it seems that it's not what is expected by JPlayer. I tried to transform it in an array of strings but it fails too... – phron Dec 06 '11 at 13:04
  • I think the problem we are trying to tackle here is how to easily have JPlayer grab a playlist via JSON. Not PHP created JSON that may have to be remapped, but just externalizing the jPlayerPlaylist() constructor and making it work with a simple JSON playlist. – John Drefahl Dec 27 '11 at 11:38
0

change your

 myPlaylist.add(value);

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

before that check that you have passed the values correctly using alert(), or console.log()

navinspm
  • 153
  • 1
  • 11