2012-03-08 26 views
0

我正在爲我的製作網站構建一個mpe播放器,使用jplayer。我遇到的問題是我給我的訪客全部歌曲聽,因此我必須嘗試保護我的音樂,以便繼承問題。 jplayer需要一個字符串,它是要播放的曲目的文件位置。我想打一個ajax電話並返回該位置。我試圖返回varible後要放置在字符串位置的Ajax調用,但代碼運行之前扔在通話結束....ajax調用jplayer後檢索文件位置返回字符串

繼承人我的代碼:

HTML標記:

<div id="player"></div> 

     <!-- Using the cssSelectorAncestor option with the default cssSelector class names to enable control association of standard functions using built in features --> 

     <div id="jp_container" class="demo-container"> 

      <p> 
       <div class="pro"></div> 
       <span class="play-state"></span> : <span class="track-name">nothing</span><br /> 

       of <span class="jp-duration"></span>, which is 
       <span class="jp-current-time"></span><br /> 
      </p> 
      <ul class="toolbar ui-widget-header ui-corner-all"> 
       <li><button class="jp-Prev" href="#">Prev</button></li> 
       <li><button class="jp-play" href="#">Play</button></li> 
       <li><button class="jp-pause" href="#">Pause</button></li> 
       <li><button class="jp-stop" href="#">Stop</button></li> 
       <li><button class="jp-Next" href="#">Next</button></li> 
       <li><button class="jp-mute" href="#">Mute</button></li> 
       <li><button class="jp-unmute" href="#">Unmute</button></li> 
       <li><div class="jp-volume-bar"></div></li> 
      </ul> 
      <ul class="playlist"> 
       <li><span>Select a track :</span></li> 
       <? Beats(); ?> 
      </ul> 
     </div> 

jQuery的標記:

$("#jp_container .track").on("click",function(event) { 
     var x = $(this).attr('id'); 
     var mp3File = // maybe a function can go here 
     my_jPlayer.jPlayer("setMedia", { 
      mp3: //this is where the string is expected 
     }); 
     my_jPlayer.jPlayer("play"); 
     my_trackName.text($(this).text()); 
     $(this).blur(); 
     return false;  
    }); 
// here is were i get the location in a function i workout already 
function url(x){ 
    var mp3; 
    $.ajax({ 
    type: "POST", 
    url: "hosts/beats/beat.php", 
    data: "<?=md5('url')?>="+x+"&ok=<?=md5(rand(1,20))?>", 
    dataType: "html", 
    success:function(data){ var mp3 = data; } 
     }); 
    return mp3; 
} 

回答

2

第一個「A」在AJAX是ayshnchronous ...你不能從你的函數返回mp3因爲AJAX還沒有喲完成時你嘗試返回它。

你需要做的setMedia AJAX的成功回調中

$("#jp_container .track").on("click", function(event) { 
    var x = $(this).attr('id'); 

    $.ajax({ 
     type: "POST", 
     url: "hosts/beats/beat.php", 
     data: "<?=md5('url')?>=" + x + "&ok=<?=md5(rand(1,20))?>", 
     dataType: "html", 
     success: function(data) { 

      my_jPlayer.jPlayer("setMedia", { 
       mp3: data 
      }); 
      my_jPlayer.jPlayer("play"); 
      my_trackName.text($(this).text()); 
      $(this).blur(); 

     } 
    }); 

    return false; 
}); 
相關問題