weixin_33717298 2013-06-05 05:33 采纳率: 0%
浏览 395

jQuery得到后播放mp3

I'm writing a little program that loops in the background and plays an mp3 player when a certain condition is met (checked by a php script).

Here's the script.js:

$(document).ready(function(){

    setInterval(function(){
        $.get("airhorn.php", function(data) {
        if(data == 1){
            //play sound
        }
        });
    },5000); //5 seconds

});

For now airhorn.php always returns a 1.

How do I set it up to make the browser play an mp3 (e.g. "airhorn.mp3") where is says "play sound"?

I've been trying to get jPlayer to work but I'm struggling.

For example, I tried:

 $(document).ready(function(){
      $("#jquery_jplayer_1").jPlayer("setMedia", mp3: "http://www.site.com/airhorn.mpp3");

    setInterval(function(){
        $.get("airhorn.php", function(data) {
        if(data == 1){
            $("#jquery_jplayer_1").jPlayer("play");//play sound
        }
        });
    },5000); //5 seconds

});

Edit:

Solution was to put the loop inside the jplayer ready event:

$(document).ready(function(){
    $("#jquery_jplayer_1").jPlayer( {
        ready: function () {
            $(this).jPlayer("setMedia", {
                mp3: "http://www.jplayer.org/audio/mp3/Miaow-snip-Stirring-of-a-fool.mp3" // Defines the mp3 url
            })
            setInterval(function(){
        $.get("airhorn.php", function(data) {
            if(data == 1){
                alert("should play")
                $("#jquery_jplayer_1").jPlayer("play"); //play sound
            }
        });
    },5000); //5 seconds
        },
        supplied: "mp3",
        swfPath: "jPlayer/js"
    });



    });
  • 写回答

2条回答 默认 最新

  • Lotus@ 2013-06-06 15:05
    关注

    The right answer this time... jPlayer needs to be configured after it is ready. It uses a callback in its ready configuration property. This is how you set it up...

    $(document).ready(function() {
        $("#jquery_jplayer_1").jPlayer({
            ready: function(event) {
                $(this).jPlayer("setMedia", {
                    mp3: "http://jplayer.org/audio/mp3/TSP-01-Cro_magnon_man.mp3",
                    oga: "http://jplayer.org/audio/ogg/TSP-01-Cro_magnon_man.ogg"
                });
            },
            swfPath: "http://jplayer.org/latest/js",
            supplied: "mp3, oga"
        });
    });
    

    Here is the official working example: http://jsfiddle.net/jPlayer/XLNCY/

    The reason looping works is because it continually tries and eventually it is ready. The problem with continually retrying is you never stop trying to hit your server. It's like a self inflicted DOS attack.

    Hope this cleans it up. Cheers!

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog