crossbrowdy
Version:
A Multimedia JavaScript framework to create real cross-platform and hybrid game engines, games, emulators, multimedia libraries and apps.
607 lines (512 loc) • 88.3 kB
JavaScript
/**
* @file Audio files management, including abstraction for different audio APIs. Possible audio APIs are "WAAPI" ([HTML5 Web Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API}), "SM2" ([SoundManager 2]{@link http://schillmania.com/projects/soundmanager2/}), "ACMP" ([Apache Cordova Media Plugin]{@link https://github.com/apache/cordova-plugin-media}) or "AAPI" ([HTML5 Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio}). Contains the {@link CB_AudioFile} class.
* @author Joan Alba Maldonado <workindalian@gmail.com>
* @license Creative Commons Attribution 4.0 International. See more at {@link https://crossbrowdy.com/about#what_is_the_crossbrowdy_copyright_and_license}.
*/
/**
* The constructor is recommended to be called through a user-driven event (as onClick, onTouch, etc.) if the "autoPlay" option is set to true, as some clients may need this at least the first time in order to be able to play the audio.
* @class
* @classdesc Class to manage an audio file. Internally, it uses one audio API object which belongs to the audio API being used (when the audio API is changed, it keeps the old audio API objects just in case they are needed in the future when the audio API is changed again). Possible internal audio API objects are {@link CB_AudioFile_API.WAAPI} object for "WAAPI" ([HTML5 Web Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API}), {@link CB_AudioFile_API.SM2} object for "SM2" ([SoundManager 2]{@link http://schillmania.com/projects/soundmanager2/}), {@link CB_AudioFile_API.ACMP} object for "ACMP" ([Apache Cordova Media Plugin]{@link https://github.com/apache/cordova-plugin-media}) or {@link CB_AudioFile_API.AAPI} object for "AAPI" ([HTML5 Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio}).
* @param {string} filePath - The path of the audio file or a data URI. NOTE: Only some clients with some audio APIs will support data URIs.
* @param {string} [audioId='CB_AudioFile_' + CB_AudioFile._idUnique++] - Desired identifier for the audio object (can be a different element depending on the audio API used). If not provided, an automatic unique ID will be calculated. Note that it is not case sensitive and it should be unique for each object.
* @param {CB_AudioFile.OPTIONS} [options={@link CB_AudioFile#DEFAULT_OPTIONS}] - Object with the desired options.
* @param {string} [audioAPI=CB_AudioDetector.getPreferredAPI(undefined, false, null) || CB_AudioDetector.getPreferredAPI(undefined, true, null)] - The desired audio API to be used. If not provided, it will try to calculate the best one for the current client by calling the {@link CB_AudioDetector.getPreferredAPI} function internally. Audio API support will depend on the current client being used. All possible ones are defined in {@link CB_Configuration.CrossBase.CB_AudioFileCache_PREFERRED_AUDIO_APIS}. For example: "WAAPI" ([HTML5 Web Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API}), "SM2" ([SoundManager 2]{@link http://schillmania.com/projects/soundmanager2/}), "ACMP" ([Apache Cordova Media Plugin]{@link https://github.com/apache/cordova-plugin-media}) or "AAPI" ([HTML5 Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio}).
* @param {function} [callbackOk] - Function with no parameters to be called when the audio has been loaded successfully, being "this" the {@link CB_AudioFile} object itself.
* @param {function} [callbackError] - Function to be called if the audio has not been loaded successfully. The first and unique parameter will be a string describing the error found (if it could be determined), being "this" the {@link CB_AudioFile} object itself.
* @returns {CB_AudioFile} Returns a new {@link CB_AudioFile} object.
* @todo Do not allow to create one object with an "id" which has already been used (unless the value is undefined, null...). Note that the "id" is not case sensitive and it should be unique for each object.
* @todo Send the {@link CB_AudioFile} object itself as a parameter when calling both "callbackOk" and "callbackError".
* @todo Think about allowing to define 'useXHR' and 'useCache' options (used by {@link CB_AudioFile_API.WAAPI} objects).
* @todo Method getCopy and static method filterProperties (similar to the ones from {@link CB_GraphicSprites} and {@link CB_GraphicSpritesScene}).
*/
var CB_AudioFile = function(filePath, audioId, options, audioAPI, callbackOk, callbackError)
{
//Creates an instance of this object and returns it in the case that it is being called from an unexpected context:
if (this === window || !(this instanceof CB_AudioFile)) { return new CB_AudioFile(filePath, audioId, options, audioAPI, callbackOk, callbackError); }
//Constants:
/**
* Keeps the default volume. If the {@link CB_Configuration.CrossBase.CB_AudioFile_AudioFileCache_USE_SPEAKER_VOLUME_AS_DEFAULT} property is true, this will keep the result of calling the {@link CB_Speaker.getVolume} function. Otherwise, it will use the value of the {@link CB_Configuration.CrossBase.CB_Speaker_DEFAULT_VOLUME} variable.
* @constant
* @type {number}
* @default CB_Configuration.CrossBase.CB_AudioFile_AudioFileCache_USE_SPEAKER_VOLUME_AS_DEFAULT ? CB_Speaker.getVolume() : CB_Configuration.CrossBase.CB_Speaker_DEFAULT_VOLUME
*/
CB_AudioFile.prototype.DEFAULT_VOLUME = CB_Configuration[CB_BASE_NAME].CB_AudioFile_AudioFileCache_USE_SPEAKER_VOLUME_AS_DEFAULT ? CB_Speaker.getVolume() : CB_Configuration[CB_BASE_NAME].CB_Speaker_DEFAULT_VOLUME;
/**
* Keeps the default options when an object is created. Format: { autoLoad: boolean, autoPlay: boolean, loop: boolean, volume: number }.
* @constant
* @type {CB_AudioFile.OPTIONS}
* @default { autoLoad: true, autoPlay: false, loop: false, volume: [CB_AudioFile.prototype.DEFAULT_VOLUME]{@link CB_AudioFile#DEFAULT_VOLUME} }
*/
CB_AudioFile.prototype.DEFAULT_OPTIONS = { autoLoad: true, autoPlay: false, loop: false, volume: CB_AudioFile.prototype.DEFAULT_VOLUME };
//Properties and variables:
/**
* Defines whether the file loops by default when the audio is played or not. Its value will be modified automatically whenever the {@link CB_AudioFile#play} method is called, getting the value from the "loop" parameter (but only if contains a boolean).
* @var
* @readonly
* @type {boolean}
* @default [CB_AudioFile.prototype.DEFAULT_OPTIONS]{@link CB_AudioFile#DEFAULT_OPTIONS}.loop
*/
this.loop = CB_AudioFile.prototype.DEFAULT_OPTIONS.loop;
/**
* Stores the volume of this audio. Accepted values go from 0 to MAX_VOLUME, where MAX_VOLUME is 100 if the {@link CB_Configuration.CrossBase.CB_AudioFile_AudioFileCache_USE_SPEAKER_VOLUME_AS_MAXIMUM} property is false or otherwise MAX_VOLUME is the returning value of the {@link CB_Speaker.getVolume} function.
* @var
* @readonly
* @type {number}
* @default [CB_AudioFile.prototype.DEFAULT_OPTIONS]{@link CB_AudioFile#DEFAULT_OPTIONS}.volume
*/
this.volume = CB_AudioFile.prototype.DEFAULT_OPTIONS.volume;
/**
* Stores the identifier for the audio file.
* @var
* @readonly
* @type {string}
* @default 'CB_AudioFile_' + CB_AudioFile._idUnique++
*/
this.id = "";
/**
* Stores the path of the audio file or the data URI. NOTE: Only some clients with some audio APIs will support data URIs.
* @var
* @readonly
* @type {string}
* @default
*/
this.filePath = "";
/**
* Defines the Audio API used for this audio file. Audio API support will depend on the current client being used. All possible ones are defined in {@link CB_Configuration.CrossBase.CB_AudioFileCache_PREFERRED_AUDIO_APIS}. For example: "WAAPI" ([HTML5 Web Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API}), "SM2" ([SoundManager 2]{@link http://schillmania.com/projects/soundmanager2/}), "ACMP" ([Apache Cordova Media Plugin]{@link https://github.com/apache/cordova-plugin-media}) or "AAPI" ([HTML5 Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio}).
* @var
* @readonly
* @type {string}
* @default
*/
this.audioAPI = null;
this._onStopFunction = null; //Function to call when the audio stops.
//Fake AudioFile[x] object:
var that = this;
CB_AudioFile._audioFileObject_prototype = CB_AudioFile._audioFileObject_prototype ||
{
usingPrototype : true,
status : CB_AudioFile.UNLOADED,
paused : false,
stopped : true,
destructor : function() {},
getDuration : function() { return 0; },
checkPlaying :
function(callbackOk, callbackError)
{
if (typeof(callbackError) === "function") { callbackError.call(that, "audioFileObject is not loaded (using CB_AudioFile._audioFileObject_prototype)"); }
return false;
},
play : function() {},
resume : function() {},
pause : function() {},
stop : function() {},
volume : this.DEFAULT_OPTIONS.volume,
volumeBeforeMute : this.DEFAULT_OPTIONS.volume,
setVolume :
function(volume)
{
var MAX_VOLUME = CB_Configuration[CB_BASE_NAME].CB_AudioFile_AudioFileCache_USE_SPEAKER_VOLUME_AS_MAXIMUM ? CB_Speaker.getVolume() : 100;
if (volume > MAX_VOLUME) { volume = MAX_VOLUME; }
else if (volume < 0) { volume = 0; }
this.volume = volume;
return volume;
},
mute :
function()
{
this.volumeBeforeMute = this.volume;
this.volume = 0;
return 0;
},
unmute :
function()
{
this.volume = this.volumeBeforeMute;
return this.volume;
},
getCurrentTime : function() { return 0; },
onStop : function() { return false; },
getProgress : function() { return 0; }
};
CB_AudioFile._audioFileObject_prototype.volume = CB_AudioFile.prototype.DEFAULT_OPTIONS.volume; //Updates the property because maybe the volume has changed.
CB_AudioFile._audioFileObject_prototype.volumeBeforeMute = CB_AudioFile.prototype.DEFAULT_OPTIONS.volume; //Updates the property because maybe the volume has changed.
/**
* It will store the created audio file objects for the different audio APIs (for optimization purposes, to avoid creating more than one per API). Being each index the name of the audio API ("WAAPI", "AAPI", "SM2" or "ACMP"), their value will be an object which can be {@link CB_AudioFile_API.WAAPI} object for "WAAPI" ([HTML5 Web Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API}), {@link CB_AudioFile_API.SM2} object for "SM2" ([SoundManager 2]{@link http://schillmania.com/projects/soundmanager2/}), {@link CB_AudioFile_API.ACMP} object for "ACMP" ([Apache Cordova Media Plugin]{@link https://github.com/apache/cordova-plugin-media}) or {@link CB_AudioFile_API.AAPI} object for "AAPI" ([HTML5 Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio}). Recommended for internal usage only.
* @var
* @type {Object}
* @default
*/
this.audioFileObjects = {}; //It will store the created objects for the different audio APIs (for optimization purposes, to avoid creating more than one per API).
/**
* It will store the current audio file object for the current audio API. The {@link CB_AudioFile#load} method will set the value of this property only after the audio file object (stored in its value) is loaded properly. Possible internal audio API objects are {@link CB_AudioFile_API.WAAPI} object for "WAAPI" ([HTML5 Web Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API}), {@link CB_AudioFile_API.SM2} object for "SM2" ([SoundManager 2]{@link http://schillmania.com/projects/soundmanager2/}), {@link CB_AudioFile_API.ACMP} object for "ACMP" ([Apache Cordova Media Plugin]{@link https://github.com/apache/cordova-plugin-media}) or {@link CB_AudioFile_API.AAPI} object for "AAPI" ([HTML5 Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio}). When no audio API object is being set, it will contain a fake object with same methods and properties (defined in {@link CB_AudioFile._audioFileObject_prototype}). Recommended for internal usage only.
* @var
* @type {CB_AudioFile_API.WAAPI | CB_AudioFile_API.SM2 | CB_AudioFile_API.ACMP | CB_AudioFile_API.AAPI | Object}
* @default
*/
this.audioFileObject = CB_AudioFile._audioFileObject_prototype;
/**
* Stores the last audio file object created or reused, for the current API being used. The {@link CB_AudioFile#load} method will set the value of this property before knowing whether the audio file object (stored in its value) will be loaded properly or not. Used by the {@link CB_AudioFile#load} and {@link CB_AudioFile#getProgress} methods. Possible internal audio API objects are {@link CB_AudioFile_API.WAAPI} object for "WAAPI" ([HTML5 Web Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API}), {@link CB_AudioFile_API.SM2} object for "SM2" ([SoundManager 2]{@link http://schillmania.com/projects/soundmanager2/}), {@link CB_AudioFile_API.ACMP} object for "ACMP" ([Apache Cordova Media Plugin]{@link https://github.com/apache/cordova-plugin-media}) or {@link CB_AudioFile_API.AAPI} object for "AAPI" ([HTML5 Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio}). When no audio API object is being set, it will contain a fake object with same methods and properties (defined in {@link CB_AudioFile._audioFileObject_prototype}). Recommended for internal usage only.
* @var
* @type {CB_AudioFile_API.WAAPI | CB_AudioFile_API.SM2 | CB_AudioFile_API.ACMP | CB_AudioFile_API.AAPI | Object}
* @default
*/
this.audioFileObjectLast = CB_AudioFile._audioFileObject_prototype;
this._loadingAudioFileObject = false; //Tells when the audio object is being loading.
//Variables to keep same parameters when API is changed:
this._avoidDelayedPlayLast = null;
this._allowedRecursiveDelayLast = null;
//this._onPlayStartLast = undefined;
this._onLoadErrorLast = null;
//Calls the constructor of the object when creates an instance:
return this._init(filePath, audioId, options, audioAPI, callbackOk, callbackError);
}
/**
* Object with the options for an audio file. The format is the following one: { autoLoad: boolean, autoPlay: boolean, loop: boolean, volume: number }.
* @memberof CB_AudioFile
* @typedef {Object} CB_AudioFile.OPTIONS
* @property {boolean} [autoLoad={@link CB_AudioFile#DEFAULT_OPTIONS}.autoLoad] - Value which will be used as the "autoLoad" parameter when calling the {@link CB_AudioFile#setAudioAPI} method internally (when the constructor is called).
* @property {boolean} [autoPlay={@link CB_AudioFile#DEFAULT_OPTIONS}.autoPlay] - Value which will be used as the "autoPlay" parameter when calling the {@link CB_AudioFile#setAudioAPI} method internally (when the constructor is called).
* @property {boolean} [loop={@link CB_AudioFile#DEFAULT_OPTIONS}.loop] - Value that will be used for the {@link CB_AudioFile#loop} property.
* @property {number} [volume={@link CB_AudioFile#DEFAULT_OPTIONS}.volume] - The desired volume (from 0 to the maximum value, where the maximum value will be the returning value of calling the {@link CB_Speaker.getVolume} function if the {@link CB_Configuration.CrossBase.CB_AudioFile_AudioFileCache_USE_SPEAKER_VOLUME_AS_MAXIMUM} property is set to true or it will be 100 otherwise) that will be used for the {@link CB_AudioFile#volume} property.
*/
//Static properties and constants:
CB_AudioFile._idUnique = 0; //Counter to make the id unique.
/**
* Status value for an audio file which is unloaded. Can be used to compare the value returned by the {@link CB_AudioFile#getStatus} method. Recommended for internal usage only.
* @constant
* @type {integer}
* @default 0
*/
CB_AudioFile.UNLOADED = 0;
/**
* Status value for an audio file which is loading. Can be used to compare the value returned by the {@link CB_AudioFile#getStatus} method. Recommended for internal usage only.
* @constant
* @type {integer}
* @default
*/
CB_AudioFile.LOADING = 1;
/**
* Status value for an audio file which has been not checked yet. Can be used to compare the value returned by the {@link CB_AudioFile#getStatus} method. Recommended for internal usage only.
* @constant
* @type {integer}
* @default
*/
CB_AudioFile.UNCHECKED = 2;
/**
* Status value for an audio file which is being checked currently. Can be used to compare the value returned by the {@link CB_AudioFile#getStatus} method. Recommended for internal usage only.
* @constant
* @type {integer}
* @default
*/
CB_AudioFile.CHECKING = 3;
/**
* Status value for an audio file which has been loaded. Can be used to compare the value returned by the {@link CB_AudioFile#getStatus} method. Recommended for internal usage only.
* @constant
* @type {integer}
* @default
*/
CB_AudioFile.LOADED = 4;
/**
* Status value for an audio file which failed to be loaded or failed for any other reason. Can be used to compare the value returned by the {@link CB_AudioFile#getStatus} method. Recommended for internal usage only.
* @constant
* @type {integer}
* @default
*/
CB_AudioFile.FAILED = 5;
/**
* Status value for an audio file which has been aborted. This will happen when the audio file has been destroyed with the {@link CB_AudioFile#destructor} method. Can be used to compare the value returned by the {@link CB_AudioFile#getStatus} method. Recommended for internal usage only.
* @constant
* @type {integer}
* @default
*/
CB_AudioFile.ABORTED = 6;
//Constructor:
CB_AudioFile.prototype._init = function(filePath, audioId, options, audioAPI, callbackOk, callbackError)
{
//If not given, defines the default parameters:
if (typeof(audioId) === "undefined" || audioId === null) { audioId = "CB_AudioFile_" + CB_AudioFile._idUnique++; } //Uses the file path as default id.
if (typeof(options) === "undefined" || options === null) { options = this.DEFAULT_OPTIONS; }
else
{
if (typeof(options.loop) === "undefined" || options.loop === null) { options.loop = this.DEFAULT_OPTIONS.loop; }
if (typeof(options.autoLoad) === "undefined" || options.autoLoad === null) { options.autoLoad = this.DEFAULT_OPTIONS.autoLoad; }
if (typeof(options.autoPlay) === "undefined" || options.autoPlay === null) { options.autoPlay = this.DEFAULT_OPTIONS.autoPlay; }
if (typeof(options.volume) === "undefined" || options.volume === null) { options.volume = this.DEFAULT_OPTIONS.volume; }
}
//Sets the audio ID:
this.id = CB_trim(audioId).toUpperCase();
//Sets the file path:
this.filePath = filePath;
//Sets whether it will loop or not:
this.loop = options.loop;
//Sets the volume:
//this.volume = options.volume;
this.setVolume(options.volume);
//Sets the audio API and proceeds according to the options received:
this.setAudioAPI(audioAPI, options.autoLoad, options.autoPlay, callbackOk, callbackError); //Will load the audio too.
//Returns the object:
return this;
}
/**
* Destroys the audio file object and frees memory. Sets its current status to ABORTED ({@link CB_AudioFile.ABORTED} value).
* @function
* @param {boolean} [stopSound=false] - If set to true, it will also call the "stop" method of the internal audio file object for the current API (stored in the {@link CB_AudioFile#audioFileObject} property). This method has the same parameters as the {@link CB_AudioFile#stop} method.
* @param {boolean} [keepStoppedUnaltered=false] - Used internally as the "keepStoppedUnaltered" parameter to call the "stop" method of the internal audio file object for the current API (stored in the {@link CB_AudioFile#audioFileObject} property). This method has the same parameters as the {@link CB_AudioFile#stop} method. If the "stopSound" parameter is not set to true, this parameter will be ignored as the "stop" method will not be called.
* @param {boolean} [avoidOnStop=false] - Used internally as the "avoidOnStop" parameter to call the "stop" method of the internal audio file object for the current API (stored in the {@link CB_AudioFile#audioFileObject} property). This method has the same parameters as the {@link CB_AudioFile#stop} method. If the "stopSound" parameter is not set to true, this parameter will be ignored as the "stop" method will not be called.
* @param {boolean} [forceOnStop=false] - Used internally as the "forceOnStop" parameter to call the "stop" method of the internal audio file object for the current API (stored in the {@link CB_AudioFile#audioFileObject} property). This method has the same parameters as the {@link CB_AudioFile#stop} method. If the "stopSound" parameter is not set to true, this parameter will be ignored as the "stop" method will not be called.
*/
CB_AudioFile.prototype.destructor = function(stopSound, keepStoppedUnaltered, avoidOnStop, forceOnStop)
{
this.audioFileObject.destructor(stopSound, keepStoppedUnaltered, avoidOnStop, forceOnStop);
this.audioFileObject = CB_AudioFile._audioFileObject_prototype;
this.audioFileObject.status = CB_AudioFile.ABORTED;
}
/**
* Sets the desired audio API. This method will also be called automatically by the constructor. If the "autoLoad" parameter is set to true, it will call the {@link CB_AudioFile#load} method internally, changing the audio API on-the-fly, and the audio will try to continue playing if it was playing at the moment of calling this method. Check the {@link CB_AudioFile#load} method documentation for more information. If the "autoLoad" parameter is set to true, it is recommended to be called through a user-driven event (as onClick, onTouch, etc.), as some clients may need this at least the first time in order to be able to play the audio. The audio API used will be stored in the {@link CB_AudioFile#audioAPI} property.
* @function
* @param {string} [audioAPI=CB_AudioDetector.getPreferredAPI(undefined, false, null) || CB_AudioDetector.getPreferredAPI(undefined, true, null)] - The desired audio API to be used. If not provided, it will try to calculate the best one for the current client by calling the {@link CB_AudioDetector.getPreferredAPI} function internally. Audio API support will depend on the current client being used. All possible ones are defined in {@link CB_Configuration.CrossBase.CB_AudioFileCache_PREFERRED_AUDIO_APIS}. For example: "WAAPI" ([HTML5 Web Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API}), "SM2" ([SoundManager 2]{@link http://schillmania.com/projects/soundmanager2/}), "ACMP" ([Apache Cordova Media Plugin]{@link https://github.com/apache/cordova-plugin-media}) or "AAPI" ([HTML5 Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio}). Used internally as the "audioAPI" parameter when calling the {@link CB_AudioFile#load} method internally (only when the "autoLoad" parameter is set to true).
* @param {string} [autoLoad=true] - If set to false, it will not call the {@link CB_AudioFile#load} method internally and will only set the {@link CB_AudioFile#audioAPI} property (not recommended).
* @param {string} [autoPlay=false] - Used internally as the "autoPlay" parameter when calling the {@link CB_AudioFile#load} method internally (only when the "autoLoad" parameter is set to true).
* @param {function} [callbackOk] - Function with no parameters to be called when the audio has been loaded successfully, being "this" the {@link CB_AudioFile} object itself. Used internally as the "callbackOk" parameter when calling the {@link CB_AudioFile#load} method internally (only when the "autoLoad" parameter is set to true).
* @param {function} [callbackError] - Function to be called if the audio has not been loaded successfully. The first and unique parameter will be a string describing the error found (if it could be determined), being "this" the {@link CB_AudioFile} object itself. Used internally as the "callbackError" parameter when calling the {@link CB_AudioFile#load} method internally (only when the "autoLoad" parameter is set to true).
* @param {string} [ignoreOldValues=false] - Used internally as the "ignoreOldValues" parameter when calling the {@link CB_AudioFile#load} method internally (only when the "autoLoad" parameter is set to true).
* @param {string} [filePath={@link CB_AudioFile#filePath}] - Used internally as the "filePath" parameter when calling the {@link CB_AudioFile#load} method internally (only when the "autoLoad" parameter is set to true).
* @param {string} [forceReload=false] - Used internally as the "forceReload" parameter when calling the {@link CB_AudioFile#load} method internally (only when the "autoLoad" parameter is set to true).
* @returns {string} Returns the desired audio API that has been tried to set, in upper case (successfully or not).
* @todo Think about using the "forceReload" just after the "callbackError" to match the parameter order of the "load" method of all the audio API objects.
*/
CB_AudioFile.prototype.setAudioAPI = function(audioAPI, autoLoad, autoPlay, callbackOk, callbackError, ignoreOldValues, filePath, forceReload)
{
if (typeof(audioAPI) === "undefined" || audioAPI === null) //Uses the preferred API as default.
{
audioAPI = CB_AudioDetector.getPreferredAPI(undefined, false, null) || CB_AudioDetector.getPreferredAPI(undefined, true, null);
}
audioAPI = CB_trim(audioAPI).toUpperCase();
if (!CB_AudioDetector.APIExists(audioAPI))
{
if (typeof(callbackError) === "function") { callbackError.call(this, "Audio API given does not exist (" + audioAPI + ")."); }
return audioAPI;
}
if (typeof(autoLoad) === "undefined" || autoLoad === null) { autoLoad = true; }
//Since AJAX doesn't allow to load local files, if the file is local we can't use WAAPI:
//if (CB_isFileLocal(this.filePath)) { if (audioAPI === "WAAPI") { audioAPI = "SM2"; } } //Uses SM2 instead.
//If it is the same audio API as the current one, we don't need to do more:
if (this.audioAPI === audioAPI) { if (typeof(callbackOk) === "function") { callbackOk.call(this); } return audioAPI; }
//First time is undefined, so we accept any first value:
if (typeof(this.audioAPI) === "undefined") { this.audioAPI = audioAPI; }
//We (re)load the audio file if we want to (NOTE: audioAPI will change only if the object is created and loaded successfully):
if (autoLoad)
{
this.load(filePath ? filePath : this.filePath, audioAPI, autoPlay, callbackOk, callbackError, ignoreOldValues, forceReload);
}
//...otherwise, we just change the audioAPI property:
else { this.audioAPI = audioAPI; }
return audioAPI;
}
/**
* Loads the desired audio file with the desired options. Recommended to be called through a user-driven event (as onClick, onTouch, etc.), as some clients may need this at least the first time in order to be able to play the audio. This method will be called automatically if the "autoLoad" option was set to true when calling the {@link CB_AudioFile#setAudioAPI} method. The audio API used will be stored in the {@link CB_AudioFile#audioAPI} property.
* When this method is called, if the "status" property of the audio API object already has the "LOADED" status (defined in the {@link CB_AudioFile.LOADED} constant) and the "forceReload" parameter is not set to true, it will exit calling the given "callbackOk" function (if any) immediately. Otherwise, regardless the status, the status will be set to "LOADING" (defined in the {@link CB_AudioFile.LOADING} constant). After it, it will reach the "UNCHECKED" (defined in the {@link CB_AudioFile.UNCHECKED} constant). If the "autoPlay" parameter is not set to true, this will be the final status (and it will be necessary to call the "checkPlaying" method of the audio API object after it). After it and only if the "autoPlay" is set to true, as the "checkPlaying" method of the audio API object will be called internally, it will have the "CHECKING" status (defined in the {@link CB_AudioFile.CHECKING} constant) and finally the "LOADED" status (defined in the {@link CB_AudioFile.LOADED} constant) if all goes well.
* Although it is not recommended to do so, if this method is called when the audio API object has the "UNCHECKED" status (defined in the {@link CB_AudioFile.UNCHECKED} constant), it will call the "checkPlaying" method of the audio API object internally.
* Internally, it can use the {@link CB_AudioFile#audioFileObjects} property as a cache.
* @function
* @param {string} [filePath={@link CB_AudioFile#filePath}] - The path of the audio file or a data URI. NOTE: Only some clients with some audio APIs will support data URIs.
* @param {string} [audioAPI={@link CB_AudioFile#audioAPI}] - The desired audio API to be used. If not provided, it will try to use the previously-set one (in the {@link CB_AudioFile#audioAPI} property). All possible ones are defined in {@link CB_Configuration.CrossBase.CB_AudioFileCache_PREFERRED_AUDIO_APIS}. For example: "WAAPI" ([HTML5 Web Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API}), "SM2" ([SoundManager 2]{@link http://schillmania.com/projects/soundmanager2/}), "ACMP" ([Apache Cordova Media Plugin]{@link https://github.com/apache/cordova-plugin-media}) or "AAPI" ([HTML5 Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio}).
* @param {string} [autoPlay=false] - If set to true, it will start playing the audio automatically (by calling the {@link CB_AudioFile#play} method internally) unless the "ignoreOldValues" parameter is set to false and the previous audio was playing or paused. If set to true and the "status" property of the audio API object reaches to the "UNCHECKED" status (defined in the {@link CB_AudioFile.UNCHECKED} constant), it will also call internally the "checkPlaying" method of the audio API object before anything. Possible internal audio API objects are {@link CB_AudioFile_API.WAAPI} object for "WAAPI" ([HTML5 Web Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API}), {@link CB_AudioFile_API.SM2} object for "SM2" ([SoundManager 2]{@link http://schillmania.com/projects/soundmanager2/}), {@link CB_AudioFile_API.ACMP} object for "ACMP" ([Apache Cordova Media Plugin]{@link https://github.com/apache/cordova-plugin-media}) or {@link CB_AudioFile_API.AAPI} object for "AAPI" ([HTML5 Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio}).
* @param {function} [callbackOk] - Function with no parameters to be called when the audio has been loaded successfully, being "this" the {@link CB_AudioFile} object itself.
* @param {function} [callbackError] - Function to be called if the audio has not been loaded successfully. The first and unique parameter will be a string describing the error found (if it could be determined), being "this" the {@link CB_AudioFile} object itself.
* @param {string} [ignoreOldValues=false] - If set to true, it will ignore the old values of the previous used audio API object. This means that it will neither continue playing if it was playing (changing the audio API on-the-fly) nor keep the paused status if it was paused nor copy its "loop" property to the new audio API object. Possible internal audio API objects are {@link CB_AudioFile_API.WAAPI} object for "WAAPI" ([HTML5 Web Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API}), {@link CB_AudioFile_API.SM2} object for "SM2" ([SoundManager 2]{@link http://schillmania.com/projects/soundmanager2/}), {@link CB_AudioFile_API.ACMP} object for "ACMP" ([Apache Cordova Media Plugin]{@link https://github.com/apache/cordova-plugin-media}) or {@link CB_AudioFile_API.AAPI} object for "AAPI" ([HTML5 Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio}).
* @param {string} [forceReload=false] - Used internally as the "forceReload" parameter when calling the "load" method of the used audio API object. Possible internal audio API objects are {@link CB_AudioFile_API.WAAPI} object for "WAAPI" ([HTML5 Web Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API}), {@link CB_AudioFile_API.SM2} object for "SM2" ([SoundManager 2]{@link http://schillmania.com/projects/soundmanager2/}), {@link CB_AudioFile_API.ACMP} object for "ACMP" ([Apache Cordova Media Plugin]{@link https://github.com/apache/cordova-plugin-media}) or {@link CB_AudioFile_API.AAPI} object for "AAPI" ([HTML5 Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio}).
* @returns {CB_AudioFile_API.WAAPI|CB_AudioFile_API.SM2|CB_AudioFile_API.ACMP|CB_AudioFile_API.AAPI|null} Returns the used audio API object or null otherwise. Possible internal audio API objects are {@link CB_AudioFile_API.WAAPI} object for "WAAPI" ([HTML5 Web Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API}), {@link CB_AudioFile_API.SM2} object for "SM2" ([SoundManager 2]{@link http://schillmania.com/projects/soundmanager2/}), {@link CB_AudioFile_API.ACMP} object for "ACMP" ([Apache Cordova Media Plugin]{@link https://github.com/apache/cordova-plugin-media}) or {@link CB_AudioFile_API.AAPI} object for "AAPI" ([HTML5 Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio}).
* @todo Think about using the "forceReload" just after the "callbackError" to match the parameter order of the "load" method of all the audio API objects.
*/
CB_AudioFile.prototype.load = function(filePath, audioAPI, autoPlay, callbackOk, callbackError, ignoreOldValues, forceReload)
{
//Defines the default parameters:
if (typeof(audioAPI) === "undefined" || audioAPI === null)
{
audioAPI = this.audioAPI; //Uses the previously set Audio API as default.
//if (typeof(audioAPI) === "undefined" || audioAPI === null) { audioAPI = CB_AudioDetector.getPreferredAPI(undefined, false, null) || CB_AudioDetector.getPreferredAPI(undefined, true, null); } //Uses the preferred API as default.
}
audioAPI = CB_trim(audioAPI).toUpperCase();
filePath = filePath || this.filePath;
var that = this;
var callbackErrorFunction =
function(error)
{
that._loadingAudioFileObject = false; //The audio object is not being loaded anymore.
that.audioFileObjectLast = that.audioFileObject;
//If we are using the prototype (no real audio object adopted), and the audio API requested has been set or the audio API was never set (first time), status is failed:
if (that.audioFileObject.usingPrototype && (audioAPI === that.audioAPI || that.audioAPI === null)) //This way, we avoid setting as FAILED if the audio API has not been changed.
{
that.audioFileObject.status = CB_AudioFile.FAILED;
}
//Calls the error function (if any):
if (typeof(callbackError) === "function") { callbackError.call(that, error); }
};
//Creates the audio object depending on the API chosen:
if (typeof(CB_AudioFile_API) !== "undefined" && typeof(CB_AudioFile_API[audioAPI]) !== "undefined")
{
this._loadingAudioFileObject = true; //The audio object is being loaded.
var callbackOkFunction =
function()
{
//If there is an object already:
var wasPlaying = false;
var wasPaused = false;
//If we wanto to play automatically and the status of the new object is unchecked, checks the file and the function will be called when finishes:
if (autoPlay && audioFileObject.status === CB_AudioFile.UNCHECKED && typeof(audioFileObject.checkPlaying) !== "undefined")
{
audioFileObject.checkPlaying(callbackOkFunction, callbackErrorFunction, false, false, false);
return;
}
//If we do not want to ignore old values and the previous audio file object used was not a prototype (it was a real one):
if (!ignoreOldValues && typeof(that.audioFileObject.usingPrototype) === "undefined")
{
//If the status of the new object is unchecked, checks the file and the function will be called when finishes:
if (audioFileObject.status === CB_AudioFile.UNCHECKED && typeof(audioFileObject.checkPlaying) !== "undefined")
{
audioFileObject.checkPlaying(callbackOkFunction, callbackErrorFunction, false, false, false);
return;
}
//var status = that.getStatus();
var status = that.audioFileObject.status;
//audioFileObject.status = LOADED;
//If the audio was LOADED:
if (status === CB_AudioFile.LOADED)
{
//Stores the startAt:
audioFileObject.lastStartAt = that.audioFileObject.lastStartAt;
//Stores the stopAt:
audioFileObject.lastStopAt = that.audioFileObject.lastStopAt;
//Stores loop:
that.loop = audioFileObject.loop = that.audioFileObject.loop;
//If the sound is playing, pauses it:
if (that.isPlaying())
{
//Stops it:
that.pause();
wasPlaying = true;
}
else if (that.audioFileObject.paused)
{
wasPaused = true;
}
//Stores the pause time (if any):
audioFileObject.pauseTime = that.audioFileObject.pauseTime;
//If it was using WAAPI and not now, we need to substract the startTime:
/*
if (that.audioAPI === "WAAPI" && audioAPI !== "WAAPI" && typeof(that.audioFileObject.startTime) !== "undefined" && that.audioFileObject.startTime !== null && !isNaN(that.audioFileObject.startTime))
{
audioFileObject.pauseTime -= that.audioFileObject.startTime * 1000;
}
*/
}
}
//If we changed the API (this means the object will have changed too):
if (that.audioAPI !== audioAPI)
{
//Stops and destroys previous object (if any) and declares it as ABORTED:
//////that.destructor(true, false, true); //Stops the object (avoiding to fire onStop) and destroys the object (sets status as ABORTED).
}
//Stores the volume:
that.volume = audioFileObject.volume = that.getVolume();
//Stores the new audio object:
that.audioFileObject = audioFileObject;
//If it was playing, continues playing from the same point:
if (!ignoreOldValues && (wasPlaying || wasPaused))
{
that.audioFileObject.stopped = false;
that.audioFileObject.paused = true;
//that.resume(that.loop);
//that.resume(that.loop, that._avoidDelayedPlayLast, that._allowedRecursiveDelayLast, that._onPlayStartLast, that._onLoadErrorLast);
that.resume(that.loop, that._avoidDelayedPlayLast, that._allowedRecursiveDelayLast, null, that._onLoadErrorLast);
if (wasPaused) { that.pause(); }
}
//...otherwise, if we wanted to play automatically, we start playing:
else if (autoPlay)
{
that.play();
}
//Now the new API can be accepted:
that.audioAPI = audioAPI;
//Sets the file path:
that.filePath = filePath;
//Set the desired onStop event (if any):
/////////that.onStop(that.onStopFunction, false);
that.onStop(that._onStopFunction, false);
that._loadingAudioFileObject = false; //The audio object has been loaded already.
//Calls the OK function (if any):
if (typeof(callbackOk) === "function") { callbackOk.call(that); }
};
//Creates a new CB_AudioFile[x] object or uses the existing one (if available):
var audioFileObject;
if (typeof(this.audioFileObjects[audioAPI]) !== "undefined" && this.audioFileObjects[audioAPI] !== null)
{
//Gets the existing object:
audioFileObject = this.audioFileObjects[audioAPI];
//Sets the desired options:
////if (typeof(this.id) === "undefined" || this.id === null) { audioFileObject.id = filePath; } //Uses the file path as default id.
if (typeof(this.id) === "undefined" || this.id === null) { audioFileObject.id = CB_trim("CB_AudioFile_" + CB_AudioFile._idUnique++).toUpperCase(); } //Uses the file path as default id.
else { audioFileObject.id = this.id; }
audioFileObject.loop = this.loop;
//audioFileObject.setVolume(this.volume);
//audioFileObject.setVolume(0); //Sets volume to zero to prevent hearing the sound in some web clients when checkPlaying is called.
if (CB_Configuration[CB_BASE_NAME].CB_AudioFile_AudioFileCache_MUTE_ON_LOAD_AND_CHECKING) { audioFileObject.mute(); } //Sets volume to zero to prevent hearing the sound in some web clients when checkPlaying is called.
//Calls the load method of the object (load method also calls the destructor method):
audioFileObject.load(filePath, false, callbackOkFunction, callbackErrorFunction, forceReload);
}
else
{
//Creates the new object and stores it (sets volume to zero to prevent hearing the sound in some web clients when checkPlaying is called):
//audioFileObject = new CB_AudioFile_API[audioAPI](filePath, this.id, { autoLoad: true, autoPlay: autoPlay, loop: this.loop, volume: this.volume }, callbackOkFunction, callbackErrorFunction);
this.DEFAULT_VOLUME = CB_Configuration[CB_BASE_NAME].CB_AudioFile_AudioFileCache_USE_SPEAKER_VOLUME_AS_DEFAULT ? CB_Speaker.getVolume() : CB_Configuration[CB_BASE_NAME].CB_Speaker_DEFAULT_VOLUME;
audioFileObject = new CB_AudioFile_API[audioAPI](filePath, this.id, { autoLoad: true, autoPlay: false, loop: this.loop, volume: CB_Configuration[CB_BASE_NAME].CB_AudioFile_AudioFileCache_MUTE_ON_LOAD_AND_CHECKING ? 0 : this.DEFAULT_VOLUME }, callbackOkFunction, callbackErrorFunction);
this.audioFileObjects[audioAPI] = audioFileObject;
}
this.audioFileObjectLast = this.audioFileObjects[audioAPI];
return audioFileObject;
}
else
{
callbackErrorFunction("CB_AudioFile_API['" + audioAPI + "'] not found");
return null;
}
}
/**
* Checks whether the audio can be played or not. Recommended to be called through a user-driven event (as onClick, onTouch, etc.), as some clients may need this at least the first time in order to be able to play the audio. Also recommended to use before calling the {@link CB_AudioFile#play} method the first time. Internally, uses the "checkPlaying" method of the used audio API object. The checking action will only be performed if the value of the "status" property of the used audio API object belongs to the {@link CB_AudioFile.UNCHECKED} or to the {@link CB_AudioFile.CHECKING} value. After checking, if the audio can be played, the "status" property of the used audio API object will get the value of {@link CB_AudioFile.LOADED}. Otherwise, if it cannot be played, the "status" property of the used audio API object will get the value of {CB_AudioFile.FAILED}. Possible internal audio API objects are {@link CB_AudioFile_API.WAAPI} object for "WAAPI" ([HTML5 Web Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API}), {@link CB_AudioFile_API.SM2} object for "SM2" ([SoundManager 2]{@link http://schillmania.com/projects/soundmanager2/}), {@link CB_AudioFile_API.ACMP} object for "ACMP" ([Apache Cordova Media Plugin]{@link https://github.com/apache/cordova-plugin-media}) or {@link CB_AudioFile_API.AAPI} object for "AAPI" ([HTML5 Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio}).
* @function
* @param {function} [callbackOk] - Function with no parameters to be called when the audio has been checked successfully, being "this" the {@link CB_AudioFile} object itself.
* @param {function} [callbackError] - Function to be called if the audio has not been checked successfully. The first and unique parameter will be a string describing the error found (if it could be determined), being "this" the {@link CB_AudioFile} object itself.
* @param {boolean} [ignoreStatus=false] - If set to false and the audio status is neither "UNCHECKED" nor "CHECKING", it will fail calling the "callbackError" function (if any). If set to true, it will try to perform the checking action regardless the status of the audio.
* @param {boolean} [ignoreQueue=false] - If set to false and there is already the maximum number of audio files being checked (defined internally, depending on the audio API), the function will exit and it will call itself automatically again and again until the checking process can be performed (when its queue turn has been reached). This is done for performance purposes. Some audio APIs will ignore this parameter as they do not use checking queues.
* @param {boolean} [useCache=false] - If set to true (not recommended) and the same audio file was checked previously, it will not perform the checking process again and it will do the same as the previous call. Some audio APIs will ignore this parameter as they do not use cache.
* @returns {boolean} Returns false if the checking could not be performed and failed. If it returns true, it can mean either the checking has been processed successfully or it will fail in the future, so it is recommended to ignore the returning value and use the callback functions instead.
*/
CB_AudioFile.prototype.checkPlaying = function(callbackOk, callbackError, ignoreStatus, ignoreQueue, useCache)
{
var that = this;
return this.audioFileObject.checkPlaying
(
function() { if (typeof(callbackOk) === "function") { callbackOk.call(that); } }, //callbackOk.
function(error) { if (typeof(callbackError) === "function") { callbackError.call(that, error); } }, //callbackError.
ignoreStatus,
ignoreQueue,
useCache
);
}
/**
* Tells the duration of the audio (in milliseconds). Note that some clients might not calculate the duration correctly and, in this case, a zero (0) value would be returned. Internally, uses the "getDuration" method of the used audio API object. Possible internal audio API objects are {@link CB_AudioFile_API.WAAPI} object for "WAAPI" ([HTML5 Web Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API}), {@link CB_AudioFile_API.SM2} object for "SM2" ([SoundManager 2]{@link http://schillmania.com/projects/soundmanager2/}), {@link CB_AudioFile_API.ACMP} object for "ACMP" ([Apache Cordova Media Plugin]{@link https://github.com/apache/cordova-plugin-media}) or {@link CB_AudioFile_API.AAPI} object for "AAPI" ([HTML5 Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio}).
* @function
* @returns {number} Returns the duration of the audio (in milliseconds). Note that some clients might not calculate the duration correctly and, in this case, a zero (0) value would be returned.
*/
CB_AudioFile.prototype.getDuration = function()
{
return this.audioFileObject.getDuration();
}
/**
* Plays the audio. Internally, uses the "play" method of the used audio API object. Possible internal audio API objects are {@link CB_AudioFile_API.WAAPI} object for "WAAPI" ([HTML5 Web Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API}), {@link CB_AudioFile_API.SM2} object for "SM2" ([SoundManager 2]{@link http://schillmania.com/projects/soundmanager2/}), {@link CB_AudioFile_API.ACMP} object for "ACMP" ([Apache Cordova Media Plugin]{@link https://github.com/apache/cordova-plugin-media}) or {@link CB_AudioFile_API.AAPI} object for "AAPI" ([HTML5 Audio API]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio}).
* @function
* @param {number} [startAt=0 | {@link CB_AudioFile_API.WAAPI#lastStartAt} | {@link CB_AudioFile_API.SM2#lastStartAt} | {@link CB_AudioFile_API.ACMP#lastStartAt} | {@link CB_AudioFile_API.AAPI#lastStartAt} | stopAt] - Time in milliseconds where we want the audio to start at. If not provided or it is not a valid number, it will use zero (0) as default which belongs to the beginning of the audio. If the value provided is greater than the "stopAt" provided, it will use the value set in the "lastStartAt" property of the used audio API object (which belongs to the "startAt" value the last time that the "play" method was called). If, even using the "lastStartAt" value is still greather than the "stopAt" provided, it will use the same value as the "stopAt" which means it will not play and will stop immediately. Used internally as the "startAt" parameter to call the "play" method of the audio API object.
* @param {number} [stopAt={@link CB_AudioFile_API.WAAPI#getDuration}() | {@link CB_AudioFile_API.SM2#getDuration}() | {@link CB_AudioFile_API.ACMP#getDuration}() | {@link CB_AudioFile_API.AAPI#getDuration}()] - Time in milliseconds where we want the audio to stop at. If not provided or it is not a valid number, it will use the returning value of the "getDuration" method of the used audio API object (which should belong to the total duration of the audio, if it was calculated correctly). Used internally as the "stopAt" parameter to call the "play" method of the audio API object.
* @param {boolean} [loop={@link CB_AudioFile#loop}] - Sets whether we want to play the audio looping (starting again and again) or just play it once. Note that at the end of each loop the "onStop" function defined (through the {@link CB_AudioFile#onStop} method) will not be called. Used internally as the "loop" parameter to call the "play" method of the audio API object.
* @param {boolean} [avoidDelayedPlay=false] - If set to false (recommended) and the audio failed previously or was aborted (destroyed), it will try to load it correctly again automatically and play it after that if possible (this can take some time so the audio could start playing after a delay). Otherwise, if set to true and the audio failed or was aborted (destroyed), the audio will not play at all and the "stop" method of the used audio API object will be called immediately. Used interna