1
0
Fork 0
oldhaven/public/js/beathaven/audio.js

113 lines
2.2 KiB
JavaScript

var Audio = {
audio: null,
tid: null,
tinterval: 300,
ttime: 0,
play: function() {
try {
this.audio.play();
} catch(e) {}
},
pause: function() {
try {
this.audio.pause();
} catch(e) {}
},
rewind: function() {
try {
this.audio.currentTime = 0;
} catch(e) {}
},
addTrack: function(id, url) {
if ($('#a'+ id).length == 0) {
$('#audiobox').html('<audio id="a'+ id +'" src="'+ url +'"></audio>')
}
},
setTrack: function(id) {
this.audio = document.getElementById('a'+ id);
$(this.audio).bind('play',function() {
$('#player .controls .play').hide();
$('#player .controls .pause').show();
}).bind('pause ended', function() {
$('#player .controls .pause').hide();
$('#player .controls .play').show();
});
this.ttime = 0;
},
getLoadedPercent: function() {
try {
return Audio.audio.buffered.end(0) / Audio.audio.duration;
} catch(e) {}
return 0;
},
getPlayedPercent: function() {
try {
return Audio.audio.currentTime / Audio.audio.duration;
} catch(e) {}
return 0;
},
getListenedPercent: function() {
try {
if (Audio.audio.duration < 30 || Audio.ttime < 0) {
return -1;
}
return Audio.ttime / 1000 / Audio.audio.duration;
} catch(e) {}
return -1;
},
setPlayedPercent: function(val) {
if (typeof(this.audio.duration) != undefined) {
this.audio.currentTime = Math.round(val * this.audio.duration / 100);
return true;
}
return false;
},
skipToPercent: function(val) {
try {
this.audio.currentTime = this.audio.duration * val;
} catch(e) {}
},
startListener: function() {
Audio.killListener();
Audio.tid = window.setTimeout(Audio.startListener, Audio.tinterval);
if (Audio.ttime !== -1) {
Audio.ttime += Audio.tinterval;
}
Player.updateUI(
Audio.getLoadedPercent(),
Audio.getPlayedPercent(),
Audio.getListenedPercent()
);
var pp = Audio.getPlayedPercent();
if (pp > 0.5) {
Audio.ttime = -1;
}
},
killListener: function() {
try {
window.clearTimeout(Audio.tid);
} catch(e) {}
}
}
$('#player .controls .play').live('click', function(){
Audio.play();
});
$('#player .controls .pause').live('click', function(){
Audio.pause();
});