165 lines
3.7 KiB
JavaScript
165 lines
3.7 KiB
JavaScript
/**
|
|
* Creates a Audio object
|
|
* @param graphics R3.GraphicsRuntime
|
|
* @param apiAudio R3.D3.API.Audio
|
|
* @constructor
|
|
*/
|
|
R3.D3.Audio = function(
|
|
graphics,
|
|
apiAudio
|
|
) {
|
|
|
|
this.graphics = graphics;
|
|
this.graphics.isNotThreeThrow();
|
|
|
|
if (R3.Utils.UndefinedOrNull(apiAudio)) {
|
|
apiAudio = {};
|
|
}
|
|
|
|
R3.D3.API.Audio.call(
|
|
this,
|
|
apiAudio.id,
|
|
apiAudio.name,
|
|
apiAudio.path,
|
|
apiAudio.loop,
|
|
apiAudio.volume,
|
|
apiAudio.camera,
|
|
apiAudio.overplay,
|
|
apiAudio.pause,
|
|
apiAudio.parentEntity
|
|
);
|
|
|
|
R3.Component.call(
|
|
this,
|
|
{
|
|
'camera' : R3.D3.Camera
|
|
}
|
|
);
|
|
|
|
};
|
|
|
|
R3.D3.Audio.prototype = Object.create(R3.Component.prototype);
|
|
R3.D3.Audio.prototype.constructor = R3.D3.Audio;
|
|
|
|
/**
|
|
* Audio create instance
|
|
*/
|
|
R3.D3.Audio.prototype.createInstance = function() {
|
|
|
|
if (R3.Utils.UndefinedOrNull(this.camera) ||
|
|
R3.Utils.UndefinedOrNull(this.camera.instance)) {
|
|
console.log('audio not ready to create instance');
|
|
return;
|
|
}
|
|
|
|
|
|
R3.Event.Emit(
|
|
R3.Event.GET_API_URL,
|
|
null,
|
|
function(data) {
|
|
this.apiUrl = data.apiUrl;
|
|
}.bind(this)
|
|
);
|
|
|
|
//Create an AudioListener and add it to the camera
|
|
var listener = new THREE.AudioListener();
|
|
|
|
this.camera.instance.add( listener );
|
|
|
|
// create a global audio source
|
|
this.instance = new THREE.Audio( listener );
|
|
|
|
var audioLoader = new THREE.AudioLoader();
|
|
|
|
console.log('loading audio : ' + this.name);
|
|
|
|
if (document.documentMode || /Edge/.test(navigator.userAgent)) {
|
|
R3.Component.prototype.createInstance.call(this);
|
|
} else {
|
|
try {
|
|
audioLoader.load(
|
|
this.apiUrl + this.path + '?ts=' + Date.now(),
|
|
function (buffer) {
|
|
|
|
console.log('loaded audio: ' + this.name);
|
|
|
|
this.instance.setBuffer(buffer);
|
|
this.instance.setLoop(this.loop);
|
|
this.instance.setVolume(this.volume);
|
|
R3.Component.prototype.createInstance.call(this);
|
|
}.bind(this)
|
|
);
|
|
} catch (error) {
|
|
console.warn('failed to load audio - does it exist? : ' + error.message || 'unknown reason');
|
|
R3.Component.prototype.createInstance.call(this);
|
|
}
|
|
}
|
|
|
|
};
|
|
|
|
/**
|
|
* Updates the instance with the current state
|
|
*/
|
|
R3.D3.Audio.prototype.updateInstance = function(property) {
|
|
|
|
if (property === 'loop') {
|
|
this.instance.setLoop(this.loop);
|
|
}
|
|
|
|
if (property === 'volume') {
|
|
this.instance.setVolume(this.volume);
|
|
}
|
|
|
|
if (property === 'paused') {
|
|
if (this.paused) {
|
|
this.instance.pause();
|
|
} else {
|
|
this.instance.play();
|
|
}
|
|
}
|
|
|
|
R3.Component.prototype.updateInstance.call(this, property);
|
|
};
|
|
|
|
/**
|
|
* Converts a R3.D3.Audio to a new R3.D3.API.Audio
|
|
* @returns {R3.D3.API.Audio}
|
|
*/
|
|
R3.D3.Audio.prototype.toApiObject = function() {
|
|
|
|
return new R3.D3.API.Audio(
|
|
this.id,
|
|
this.name,
|
|
this.path,
|
|
this.loop,
|
|
this.volume,
|
|
R3.Utils.IdOrNull(this.camera),
|
|
this.overplay,
|
|
this.paused,
|
|
R3.Utils.IdOrNull(this.parentEntity)
|
|
);
|
|
|
|
};
|
|
|
|
/**
|
|
* Converts from an Object Audio to a R3.D3.Audio
|
|
* @param graphics R3.GraphicsRuntime
|
|
* @param objectAudio Object
|
|
* @returns {R3.D3.Audio}
|
|
* @constructor
|
|
*/
|
|
R3.D3.Audio.FromObject = function(graphics, objectAudio) {
|
|
|
|
var apiAudio = R3.D3.API.Audio.FromObject(objectAudio);
|
|
|
|
return new R3.D3.Audio(
|
|
graphics,
|
|
apiAudio
|
|
);
|
|
|
|
};
|
|
|
|
|
|
R3.D3.Audio.prototype.play = function() {
|
|
this.instance.play();
|
|
}; |