audiojs icon indicating copy to clipboard operation
audiojs copied to clipboard

Destroy/remove instances

Open Ahrengot opened this issue 13 years ago • 6 comments

Hey Anthony!

Great piece of software. Really simplifies working with audio without doing too much and bloat your projects with thousands upon thousands of lines of code.

One question though: I've browsed through the annotated source and the audio.js file in search of a way to destroy and remove audio instances. I'm building a JavaScript app that switch between pages loaded in dynamically via AJAX. Each page has its own audio instances and i need to destroy and remove those when i transition out the page.

Is there any built-in way to do this currently? I'm looking for something like an myInstance.destroy(); method.

Ahrengot avatar Apr 05 '12 14:04 Ahrengot

I'm on the same boat right now and I cannot figure it out. I also have Bootstrap modals and each has its own

ipruthi avatar Nov 03 '12 10:11 ipruthi

Any input on this? I'm also in need of a way to destroy instances (assuming this is necessary when dynamically removing HTML with an audiojs instance bound to it)

jlefley avatar Jul 31 '13 02:07 jlefley

I had the same issue as OP: I was using AJAX to switch pages, each of which had a new player on it. I noticed that although the player was being destroyed when the page was destroyed, references to that instance was still there.

This is the solution I ended up using:

clearAllInstances: function(options, elements){ var audioElements = elements || document.getElementsByTagName('audio'), instances = [] options = options || {};

   this.instanceCount = 0;
   this.instances = {};
},

This isn't really the "correct" way to do it as it doesn't actually destroy the instances, only the references to them but as my instances were already deleted with the page itself I didn't have to worry about that.

sowhatdoido avatar Aug 02 '13 20:08 sowhatdoido

Some solid advice on how to correctly destroy instances would be invaluable, particularly for older browsers (IE8 at least).

EDIT: while I'm adding this in to manually, I came across a bad reference on line 542 in Douglas Crockford's adapted purge method:

for (i = 0; i < a.length; i += 1) purge(d.childNodes[i]);

should be:

for (i = 0; i < a.length; i += 1) this.purge(d.childNodes[i]);

davewallace avatar Jul 07 '14 05:07 davewallace

In case this helps anyone: I found myself unable to completely destroy player instances even when using Backbone/Marionette. Once they started playing, destroying the Marionette view containing the player worked fine; however, if I rapidly launched instances of the player but destroyed their DOM elements before buffering was finished, they would all start playing (simultaneously) as soon as their buffers was ready.

I discovered this technique somewhere else after hours of attempts and research:

var element = document.getElementById("audio_player_id"));
element.setAttribute('src', '/null');

It does the trick, though it is hacky. A proper self-destruction method would be fantastic. Great library otherwise.

dpearre avatar Sep 19 '14 21:09 dpearre

We have had the same problem. You can simply stop the browser from loading:

if(window.stop !== undefined) {
    window.stop();
} else if(document.execCommand !== undefined) {
    document.execCommand("Stop", false);
}

maxemilian avatar Aug 25 '15 19:08 maxemilian