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