Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Custom Playsound Macro and the DOM

So I wrote my own playsound macro. You may be asking, "Cora, why are you friggin' reinventing the wheel? We already have a sound macro."

Yes, yes that is a very valid point. Ultimately this boils down to two things. I like learning. And I also like knowing exactly what my code is doing. I'm nowhere as good as L, but I'd feel better if I worked at it as opposed to stagnating.
macros['playsound'] = macros['stopsound'] = {
handler: function(place, macroName, params, parser) {

var audio;

if(macroName == 'playsound') {

audio = document.createElement("audio");
audio.setAttribute('id', params[0]);
$("#passages").append(audio);

if (audio != null && audio.canPlayType)
{
audio.src = params[1];
audio.volume = 0.1;
audio.play();
}
}

if(macroName == 'stopsound') {

audio = document.getElementById(params[0]);
audio.pause();
}

}
};
Tada! Written from scratch! And I know how everything works! Woo! I'm smert. So as you can see, I'm appending the audio element to the DOM. We can create audio elements dynamically with argument generated id names, and then stop those whenever we want. Problem is, I'm appending to #passages, which uh... gets wiped every time the history state changes. So the sound goes poof. Is there a 'correct' place to put elements that should endure across passage transitions? Or am I going about this the complete wrong way?
Sign In or Register to comment.