Skip to content

Instantly share code, notes, and snippets.

@humbletim
Last active March 15, 2017 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save humbletim/d0a5019c371eb85b8b5b202fa4571b43 to your computer and use it in GitHub Desktop.
Save humbletim/d0a5019c371eb85b8b5b202fa4571b43 to your computer and use it in GitHub Desktop.
Entity unload resource leaks
(function() {
var TROMBONE_URL = "https://s3.amazonaws.com/hifi-public/tony/audio/sad-trombone.wav";
return {
_resource: SoundCache.prefetch(TROMBONE_URL),
_sound: undefined,
_onStateChanged: function() {
var resource = this._resource;
if (resource.state === Resource.State.FINISHED) {
resource.stateChanged.disconnect(this, '_onStateChanged');
this._sound = SoundCache.getSound(TROMBONE_URL);
this.play();
} else if (resource.state === Resource.State.FAILED) {
resource.stateChanged.disconnect(this, '_onStateChanged');
this._sound = null;
print("Failed to download sound: " + TROMBONE_URL);
}
},
preload: function(uuid) {
print("++ resource check preload", uuid);
this._resource.stateChanged.connect(this, '_onStateChanged');
this._onStateChanged();
},
play: function(uuid, args) {
if (this.tromboneInjector) {
this.tromboneInjector.restart();
} else {
this.tromboneInjector = Audio.playSound(this._sound, {
position: MyAvatar.position,
volume: .01,
loop: true,
localOnly: true,
});
}
},
stop: function(uuid, args) {
if (this.tromboneInjector) {
this.tromboneInjector.stop();
}
},
unload: function(uuid) {
print("-- resource check unload", uuid);
this.stop();
if (this._sound === undefined) {
this._resource.stateChanged.disconnect(this, '_onStateChanged');
}
},
};
})
var uuid = Entities.addEntity({
type: 'Sphere',
position: MyAvatar.position,
collisionless: true,
script: Script.resolvePath('').replace('.js','-entity.js'),
lifetime: 60
});
print('== created entity', uuid);
Script.scriptEnding.connect(function() { Entities.deleteEntity(uuid); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment