在ajax调用jplayer之后检索文件位置返回字符串


retrieve file location return string after ajax call for jplayer

我正在使用jplayer为我的制作网站构建一个mpe播放器。我的问题是,我给我的访客完整的歌曲听,出于这个原因,我必须尝试保护我的音乐,所以这就是问题所在。jplayer需要一个字符串,该字符串是要播放的曲目的文件位置。我想调用ajax并返回那个位置。我试图在ajax调用后返回一个变量,将其放在字符串位置,但在调用完成之前运行的代码抛出了。。。。

这是我的代码:

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; 
}

AJAX中的第一个"A"用于ayshchronous。。。您无法从函数中返回mp3,因为当您尝试返回时ajax尚未完成。

您需要在ajax 的成功回调中执行setMedia

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