mercredi 31 décembre 2014

Playing audio clips and lag in javascript


I'm having an issue with lag in javascript. I have many clips of audio a user can click on. I have a global "audioPlayer" that I start/pause and redefine everytime I play/stop something. But suppose I click on and off of the posts many times. Even though the posts stop and start as as they should, there's a HUGE lag before the last one plays. Such a large lag, too, that I believe instead of playing just the current audio player audio it's loading all previous audio from the posts I started to load and play and then stopped, even though it will only play the post most recently clicked. How should I fix this?



function didFinishPlayingAudio() {
IS_PLAYING_SHOUT=false;
alert("finished audio playing!")
}

var animationDiv;
var CURRENTLY_PLAYING_SHOUT;
var IS_PLAYING_SHOUT;
var audioPlayer;
function playShout(sender) {
console.log(sender.id)
var uniqueIdentifier;
if (sender.nodeName.toLowerCase() === "span") {


uniqueIdentifier=sender.parentNode.id
} else if (sender.nodeName.toLowerCase() === "img") {

uniqueIdentifier=sender.id

}
var shout;
for (i=0;i<shoutsArray.length;i++) {
if (shoutsArray[i].id==uniqueIdentifier) {
shout=shoutsArray[i];
}
}
CURRENTLY_PLAYING_SHOUT=shout;
var shoutAudioUrl=shout.get("audioData").url()

if (!IS_PLAYING_SHOUT) {
playAudioAtUrl(shoutAudioUrl)

} else {
if (shoutAudioUrl==audioPlayer.src) {
<!-- clicked on shout thats already playing-->
audioPlayer.pause();
audioPlayer=null;
IS_PLAYING_SHOUT=false;
console.log("stopped audio player")
} else {
playAudioAtUrl(shoutAudioUrl)
}
}
}

function playAudioAtUrl(url) {
console.log(url)
if (IS_PLAYING_SHOUT) {
audioPlayer.pause();
audioPlayer=null;
}
console.log("playin dat audio")
audioPlayer = new Audio(url);
audioPlayer.addEventListener("ended", didFinishPlayingAudio);
audioPlayer.id="shoutAudioPlayer"
audioPlayer.play();
IS_PLAYING_SHOUT=true;
}




Aucun commentaire:

Enregistrer un commentaire