dongqi6486 2012-03-08 16:01
浏览 45

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

i am building a mpe player for my production website, using the jplayer. the problem i have is i give my visitors the full song to listen to and for that reason i have to attempt to secure my music so heres the problem. jplayer requires a string that is a file location to the track that is to be played. i would like to make a ajax call and return that location. i tried to return a varible after a ajax call to be placed in the string location,but the code runs threw before the call is finish....

heres my code:

html markup:

<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 markup:

    $("#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; 
}
  • 写回答

1条回答 默认 最新

  • dongyunshan4066 2012-03-08 16:37
    关注

    first "A" in AJAX is for ayshnchronous...you can't return mp3 from your function because the ajax hasn't completed when you try returning it.

    You need to do the setMedia within success callback of 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;
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口