2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2014-02-05 05:54:25 +00:00
|
|
|
* @copyright 2014 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2013-09-16 14:37:30 +00:00
|
|
|
/**
|
2013-10-01 12:54:29 +00:00
|
|
|
* The Sound class constructor.
|
2013-09-16 14:37:30 +00:00
|
|
|
*
|
2013-10-01 12:54:29 +00:00
|
|
|
* @class Phaser.Sound
|
2013-09-16 14:37:30 +00:00
|
|
|
* @constructor
|
2013-10-01 12:54:29 +00:00
|
|
|
* @param {Phaser.Game} game - Reference to the current game instance.
|
|
|
|
* @param {string} key - Asset key for the sound.
|
2013-10-02 19:18:24 +00:00
|
|
|
* @param {number} [volume=1] - Default value for the volume, between 0 and 1.
|
|
|
|
* @param {boolean} [loop=false] - Whether or not the sound will loop.
|
2013-09-16 14:37:30 +00:00
|
|
|
*/
|
2013-11-26 05:13:56 +00:00
|
|
|
Phaser.Sound = function (game, key, volume, loop, connect) {
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-10-11 03:52:06 +00:00
|
|
|
if (typeof volume === 'undefined') { volume = 1; }
|
|
|
|
if (typeof loop === 'undefined') { loop = false; }
|
2013-11-26 05:13:56 +00:00
|
|
|
if (typeof connect === 'undefined') { connect = game.sound.connectToMaster; }
|
2013-09-03 00:24:16 +00:00
|
|
|
|
2013-09-19 14:34:48 +00:00
|
|
|
/**
|
|
|
|
* A reference to the currently running Game.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @property {Phaser.Game} game
|
2013-09-19 14:34:48 +00:00
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
this.game = game;
|
2013-09-19 14:34:48 +00:00
|
|
|
|
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {string} name - Name of the sound.
|
2013-09-19 14:34:48 +00:00
|
|
|
*/
|
2013-09-20 22:21:12 +00:00
|
|
|
this.name = key;
|
2013-09-19 14:34:48 +00:00
|
|
|
|
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {string} key - Asset key for the sound.
|
2013-09-19 14:34:48 +00:00
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
this.key = key;
|
2013-09-19 14:34:48 +00:00
|
|
|
|
|
|
|
/**
|
2014-04-28 01:08:19 +00:00
|
|
|
* @property {boolean} loop - Whether or not the sound or current sound marker will loop.
|
2013-09-19 14:34:48 +00:00
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
this.loop = loop;
|
2013-09-19 14:34:48 +00:00
|
|
|
|
|
|
|
/**
|
2014-04-28 01:08:19 +00:00
|
|
|
* @property {number} volume - The sound or sound marker volume. A value between 0 (silence) and 1 (full volume).
|
2013-09-19 14:34:48 +00:00
|
|
|
*/
|
2014-04-28 01:08:19 +00:00
|
|
|
this.volume = volume;
|
2013-09-19 14:34:48 +00:00
|
|
|
|
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {object} markers - The sound markers.
|
2013-09-19 14:34:48 +00:00
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
this.markers = {};
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-09-10 15:46:39 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {AudioContext} context - Reference to the AudioContext instance.
|
2013-09-10 15:46:39 +00:00
|
|
|
*/
|
|
|
|
this.context = null;
|
|
|
|
|
2013-09-19 14:34:48 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {boolean} autoplay - Boolean indicating whether the sound should start automatically.
|
2013-09-19 14:34:48 +00:00
|
|
|
*/
|
2013-09-10 15:46:39 +00:00
|
|
|
this.autoplay = false;
|
2013-09-19 14:34:48 +00:00
|
|
|
|
|
|
|
/**
|
2014-04-28 01:08:19 +00:00
|
|
|
* @property {number} totalDuration - The total duration of the sound in seconds.
|
2013-09-19 14:34:48 +00:00
|
|
|
*/
|
2013-09-10 15:46:39 +00:00
|
|
|
this.totalDuration = 0;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {number} startTime - The time the Sound starts at (typically 0 unless starting from a marker)
|
2013-10-01 12:54:29 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2013-09-10 15:46:39 +00:00
|
|
|
this.startTime = 0;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {number} currentTime - The current time the sound is at.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-10 15:46:39 +00:00
|
|
|
this.currentTime = 0;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-04-28 01:08:19 +00:00
|
|
|
* @property {number} duration - The duration of the current sound marker in seconds.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-10 15:46:39 +00:00
|
|
|
this.duration = 0;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-04-28 01:08:19 +00:00
|
|
|
/**
|
|
|
|
* @property {number} durationMS - The duration of the current sound marker in ms.
|
|
|
|
*/
|
|
|
|
this.durationMS = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} position - The position of the current sound marker.
|
|
|
|
*/
|
|
|
|
this.position = 0;
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {number} stopTime - The time the sound stopped.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-10 15:46:39 +00:00
|
|
|
this.stopTime = 0;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {boolean} paused - true if the sound is paused, otherwise false.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2013-09-10 15:46:39 +00:00
|
|
|
this.paused = false;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-24 03:27:28 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {number} pausedPosition - The position the sound had reached when it was paused.
|
2013-10-24 03:27:28 +00:00
|
|
|
*/
|
|
|
|
this.pausedPosition = 0;
|
|
|
|
|
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {number} pausedTime - The game time at which the sound was paused.
|
2013-10-24 03:27:28 +00:00
|
|
|
*/
|
|
|
|
this.pausedTime = 0;
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {boolean} isPlaying - true if the sound is currently playing, otherwise false.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2013-09-10 15:46:39 +00:00
|
|
|
this.isPlaying = false;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {string} currentMarker - The string ID of the currently playing marker, if any.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2013-09-10 15:46:39 +00:00
|
|
|
this.currentMarker = '';
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2015-01-28 17:17:44 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Tween} fadeTween - The tween that fades the audio, set via Sound.fadeIn and Sound.fadeOut.
|
|
|
|
*/
|
|
|
|
this.fadeTween = null;
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {boolean} pendingPlayback - true if the sound file is pending playback
|
|
|
|
* @readonly
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-10 15:46:39 +00:00
|
|
|
this.pendingPlayback = false;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {boolean} override - if true when you play this sound it will always start from the beginning.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @default
|
|
|
|
*/
|
2013-09-10 15:46:39 +00:00
|
|
|
this.override = false;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-10-09 13:44:25 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} allowMultiple - This will allow you to have multiple instances of this Sound playing at once. This is only useful when running under Web Audio, and we recommend you implement a local pooling system to not flood the sound channels.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.allowMultiple = false;
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {boolean} usingWebAudio - true if this sound is being played with Web Audio.
|
|
|
|
* @readonly
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-03 00:24:16 +00:00
|
|
|
this.usingWebAudio = this.game.sound.usingWebAudio;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {boolean} usingAudioTag - true if the sound is being played via the Audio tag.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-03 00:24:16 +00:00
|
|
|
this.usingAudioTag = this.game.sound.usingAudioTag;
|
|
|
|
|
2013-11-26 05:13:56 +00:00
|
|
|
/**
|
2014-05-20 21:09:46 +00:00
|
|
|
* @property {object} externalNode - If defined this Sound won't connect to the SoundManager master gain node, but will instead connect to externalNode.
|
2013-11-26 05:13:56 +00:00
|
|
|
*/
|
|
|
|
this.externalNode = null;
|
|
|
|
|
2014-04-28 01:08:19 +00:00
|
|
|
/**
|
|
|
|
* @property {object} masterGainNode - The master gain node in a Web Audio system.
|
|
|
|
*/
|
|
|
|
this.masterGainNode = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {object} gainNode - The gain node in a Web Audio system.
|
|
|
|
*/
|
|
|
|
this.gainNode = null;
|
|
|
|
|
2013-09-03 00:24:16 +00:00
|
|
|
if (this.usingWebAudio)
|
|
|
|
{
|
|
|
|
this.context = this.game.sound.context;
|
|
|
|
this.masterGainNode = this.game.sound.masterGain;
|
|
|
|
|
|
|
|
if (typeof this.context.createGain === 'undefined')
|
|
|
|
{
|
|
|
|
this.gainNode = this.context.createGainNode();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.gainNode = this.context.createGain();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.gainNode.gain.value = volume * this.game.sound.volume;
|
2013-11-26 05:13:56 +00:00
|
|
|
|
|
|
|
if (connect)
|
|
|
|
{
|
|
|
|
this.gainNode.connect(this.masterGainNode);
|
|
|
|
}
|
2013-09-03 00:24:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-11 10:33:27 +00:00
|
|
|
if (this.game.cache.getSound(key) && this.game.cache.isSoundReady(key))
|
2013-09-03 00:24:16 +00:00
|
|
|
{
|
|
|
|
this._sound = this.game.cache.getSoundData(key);
|
2013-09-11 10:33:27 +00:00
|
|
|
this.totalDuration = 0;
|
|
|
|
|
|
|
|
if (this._sound.duration)
|
|
|
|
{
|
|
|
|
this.totalDuration = this._sound.duration;
|
|
|
|
}
|
2013-09-03 00:24:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.game.cache.onSoundUnlock.add(this.soundHasUnlocked, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {Phaser.Signal} onDecoded - The onDecoded event is dispatched when the sound has finished decoding (typically for mp3 files)
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-11-25 04:40:04 +00:00
|
|
|
this.onDecoded = new Phaser.Signal();
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {Phaser.Signal} onPlay - The onPlay event is dispatched each time this sound is played.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-11-25 04:40:04 +00:00
|
|
|
this.onPlay = new Phaser.Signal();
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {Phaser.Signal} onPause - The onPause event is dispatched when this sound is paused.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-11-25 04:40:04 +00:00
|
|
|
this.onPause = new Phaser.Signal();
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {Phaser.Signal} onResume - The onResume event is dispatched when this sound is resumed from a paused state.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-11-25 04:40:04 +00:00
|
|
|
this.onResume = new Phaser.Signal();
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {Phaser.Signal} onLoop - The onLoop event is dispatched when this sound loops during playback.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-11-25 04:40:04 +00:00
|
|
|
this.onLoop = new Phaser.Signal();
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {Phaser.Signal} onStop - The onStop event is dispatched when this sound stops playback.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-11-25 04:40:04 +00:00
|
|
|
this.onStop = new Phaser.Signal();
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {Phaser.Signal} onMute - The onMouse event is dispatched when this sound is muted.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-11-25 04:40:04 +00:00
|
|
|
this.onMute = new Phaser.Signal();
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-11-25 04:40:04 +00:00
|
|
|
* @property {Phaser.Signal} onMarkerComplete - The onMarkerComplete event is dispatched when a marker within this sound completes playback.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-11-25 04:40:04 +00:00
|
|
|
this.onMarkerComplete = new Phaser.Signal();
|
2013-09-03 00:24:16 +00:00
|
|
|
|
2014-09-24 05:51:39 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onFadeComplete - The onFadeComplete event is dispatched when this sound finishes fading either in or out.
|
|
|
|
*/
|
|
|
|
this.onFadeComplete = new Phaser.Signal();
|
|
|
|
|
2014-04-28 01:08:19 +00:00
|
|
|
/**
|
|
|
|
* @property {number} _volume - The global audio volume. A value between 0 (silence) and 1 (full volume).
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._volume = volume;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {any} _buffer - Decoded data buffer / Audio tag.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._buffer = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} _muted - Boolean indicating whether the sound is muted or not.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._muted = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _tempMarker - Internal marker var.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._tempMarker = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _tempPosition - Internal marker var.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._tempPosition = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _tempVolume - Internal marker var.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._tempVolume = 0;
|
|
|
|
|
|
|
|
/**
|
2014-09-01 02:13:05 +00:00
|
|
|
* @property {number} _muteVolume - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._muteVolume = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} _tempLoop - Internal cache var.
|
2014-04-28 01:08:19 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._tempLoop = 0;
|
|
|
|
|
2014-04-28 19:30:34 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} _paused - Was this sound paused via code or a game event?
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._paused = false;
|
2014-10-11 03:52:06 +00:00
|
|
|
|
2014-06-23 22:59:44 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} _onDecodedEventDispatched - Was the onDecoded event dispatched?
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._onDecodedEventDispatched = false;
|
2013-09-03 00:24:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Sound.prototype = {
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2013-10-02 19:18:24 +00:00
|
|
|
* Called automatically when this sound is unlocked.
|
2013-11-25 04:40:04 +00:00
|
|
|
* @method Phaser.Sound#soundHasUnlocked
|
|
|
|
* @param {string} key - The Phaser.Cache key of the sound file to check for decoding.
|
2013-10-02 19:18:24 +00:00
|
|
|
* @protected
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2013-09-03 00:24:16 +00:00
|
|
|
soundHasUnlocked: function (key) {
|
|
|
|
|
2014-08-29 00:06:29 +00:00
|
|
|
if (key === this.key)
|
2013-09-03 00:24:16 +00:00
|
|
|
{
|
|
|
|
this._sound = this.game.cache.getSoundData(this.key);
|
|
|
|
this.totalDuration = this._sound.duration;
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-02 19:18:24 +00:00
|
|
|
/**
|
|
|
|
* Adds a marker into the current Sound. A marker is represented by a unique key and a start time and duration.
|
|
|
|
* This allows you to bundle multiple sounds together into a single audio file and use markers to jump between them for playback.
|
|
|
|
*
|
|
|
|
* @method Phaser.Sound#addMarker
|
|
|
|
* @param {string} name - A unique name for this marker, i.e. 'explosion', 'gunshot', etc.
|
|
|
|
* @param {number} start - The start point of this marker in the audio file, given in seconds. 2.5 = 2500ms, 0.5 = 500ms, etc.
|
|
|
|
* @param {number} duration - The duration of the marker in seconds. 2.5 = 2500ms, 0.5 = 500ms, etc.
|
|
|
|
* @param {number} [volume=1] - The volume the sound will play back at, between 0 (silent) and 1 (full volume).
|
|
|
|
* @param {boolean} [loop=false] - Sets if the sound will loop or not.
|
|
|
|
*/
|
2013-09-30 10:15:50 +00:00
|
|
|
addMarker: function (name, start, duration, volume, loop) {
|
|
|
|
|
2014-10-11 03:52:06 +00:00
|
|
|
if (typeof volume === 'undefined') { volume = 1; }
|
|
|
|
if (typeof loop === 'undefined') { loop = false; }
|
2013-09-30 10:15:50 +00:00
|
|
|
|
|
|
|
this.markers[name] = {
|
|
|
|
name: name,
|
|
|
|
start: start,
|
|
|
|
stop: start + duration,
|
|
|
|
volume: volume,
|
|
|
|
duration: duration,
|
2013-09-30 11:17:07 +00:00
|
|
|
durationMS: duration * 1000,
|
2013-09-30 10:15:50 +00:00
|
|
|
loop: loop
|
|
|
|
};
|
|
|
|
|
2013-09-03 00:24:16 +00:00
|
|
|
},
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Removes a marker from the sound.
|
|
|
|
* @method Phaser.Sound#removeMarker
|
|
|
|
* @param {string} name - The key of the marker to remove.
|
|
|
|
*/
|
2013-09-03 00:24:16 +00:00
|
|
|
removeMarker: function (name) {
|
|
|
|
|
|
|
|
delete this.markers[name];
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Called automatically by Phaser.SoundManager.
|
|
|
|
* @method Phaser.Sound#update
|
2013-10-02 19:18:24 +00:00
|
|
|
* @protected
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2013-09-03 00:24:16 +00:00
|
|
|
update: function () {
|
2014-10-11 03:52:06 +00:00
|
|
|
|
2014-08-29 00:06:29 +00:00
|
|
|
if (this.isDecoded && !this._onDecodedEventDispatched)
|
|
|
|
{
|
2014-06-15 18:23:13 +00:00
|
|
|
this.onDecoded.dispatch(this);
|
2014-08-29 00:06:29 +00:00
|
|
|
this._onDecodedEventDispatched = true;
|
2014-06-15 18:27:32 +00:00
|
|
|
}
|
2013-09-03 00:24:16 +00:00
|
|
|
|
|
|
|
if (this.pendingPlayback && this.game.cache.isSoundReady(this.key))
|
|
|
|
{
|
|
|
|
this.pendingPlayback = false;
|
|
|
|
this.play(this._tempMarker, this._tempPosition, this._tempVolume, this._tempLoop);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isPlaying)
|
|
|
|
{
|
2014-11-08 20:01:10 +00:00
|
|
|
this.currentTime = this.game.time.time - this.startTime;
|
2013-09-03 00:24:16 +00:00
|
|
|
|
2013-09-30 16:12:22 +00:00
|
|
|
if (this.currentTime >= this.durationMS)
|
2013-09-03 00:24:16 +00:00
|
|
|
{
|
|
|
|
if (this.usingWebAudio)
|
|
|
|
{
|
|
|
|
if (this.loop)
|
|
|
|
{
|
|
|
|
// won't work with markers, needs to reset the position
|
|
|
|
this.onLoop.dispatch(this);
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
if (this.currentMarker === '')
|
2013-09-03 00:24:16 +00:00
|
|
|
{
|
|
|
|
this.currentTime = 0;
|
2014-11-08 20:01:10 +00:00
|
|
|
this.startTime = this.game.time.time;
|
2013-09-03 00:24:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-02 11:11:20 +00:00
|
|
|
this.onMarkerComplete.dispatch(this.currentMarker, this);
|
2013-09-03 00:24:16 +00:00
|
|
|
this.play(this.currentMarker, 0, this.volume, true, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-28 17:17:44 +00:00
|
|
|
// console.log('Sound update stop: ' + this.currentTime + ' m: ' + this.currentMarker);
|
|
|
|
|
2013-09-03 00:24:16 +00:00
|
|
|
if (this.loop)
|
|
|
|
{
|
|
|
|
this.onLoop.dispatch(this);
|
|
|
|
this.play(this.currentMarker, 0, this.volume, true, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2013-09-03 00:24:16 +00:00
|
|
|
* Play this sound, or a marked section of it.
|
2013-10-02 19:18:24 +00:00
|
|
|
* @method Phaser.Sound#play
|
|
|
|
* @param {string} [marker=''] - If you want to play a marker then give the key here, otherwise leave blank to play the full sound.
|
|
|
|
* @param {number} [position=0] - The starting position to play the sound from - this is ignored if you provide a marker.
|
2013-11-24 11:04:58 +00:00
|
|
|
* @param {number} [volume=1] - Volume of the sound you want to play. If none is given it will use the volume given to the Sound when it was created (which defaults to 1 if none was specified).
|
2013-10-02 19:18:24 +00:00
|
|
|
* @param {boolean} [loop=false] - Loop when it finished playing?
|
|
|
|
* @param {boolean} [forceRestart=true] - If the sound is already playing you can set forceRestart to restart it from the beginning.
|
2013-11-24 11:04:58 +00:00
|
|
|
* @return {Phaser.Sound} This sound instance.
|
2013-09-03 00:24:16 +00:00
|
|
|
*/
|
|
|
|
play: function (marker, position, volume, loop, forceRestart) {
|
|
|
|
|
2015-01-01 19:51:02 +00:00
|
|
|
if (typeof marker === 'undefined' || marker === false || marker === null) { marker = ''; }
|
2013-11-25 04:40:04 +00:00
|
|
|
if (typeof forceRestart === 'undefined') { forceRestart = true; }
|
2013-09-03 00:24:16 +00:00
|
|
|
|
2015-01-28 17:17:44 +00:00
|
|
|
// console.log('Sound play: ' + marker);
|
|
|
|
|
2014-10-09 13:44:25 +00:00
|
|
|
if (this.isPlaying && !this.allowMultiple && !forceRestart && !this.override)
|
2013-09-03 00:24:16 +00:00
|
|
|
{
|
|
|
|
// Use Restart instead
|
2014-05-06 23:12:34 +00:00
|
|
|
return this;
|
2013-09-03 00:24:16 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 13:00:10 +00:00
|
|
|
if (this._sound && this.isPlaying && !this.allowMultiple && (this.override || forceRestart))
|
2013-09-03 00:24:16 +00:00
|
|
|
{
|
2014-12-18 11:14:45 +00:00
|
|
|
if (this.usingWebAudio)
|
2013-09-03 00:24:16 +00:00
|
|
|
{
|
|
|
|
if (typeof this._sound.stop === 'undefined')
|
|
|
|
{
|
|
|
|
this._sound.noteOff(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-17 13:00:10 +00:00
|
|
|
try {
|
|
|
|
this._sound.stop(0);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
2013-09-03 00:24:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (this.usingAudioTag)
|
|
|
|
{
|
|
|
|
this._sound.pause();
|
|
|
|
this._sound.currentTime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 19:51:02 +00:00
|
|
|
if (marker === '' && Object.keys(this.markers).length > 0)
|
|
|
|
{
|
|
|
|
// If they didn't specify a marker but this is an audio sprite,
|
|
|
|
// we should never play the entire thing
|
|
|
|
return this;
|
|
|
|
}
|
2013-09-03 00:24:16 +00:00
|
|
|
|
2013-09-30 16:12:22 +00:00
|
|
|
if (marker !== '')
|
2013-09-03 00:24:16 +00:00
|
|
|
{
|
2015-01-01 19:51:02 +00:00
|
|
|
this.currentMarker = marker;
|
|
|
|
|
2013-09-30 16:12:22 +00:00
|
|
|
if (this.markers[marker])
|
|
|
|
{
|
2014-03-21 15:43:45 +00:00
|
|
|
// Playing a marker? Then we default to the marker values
|
2013-09-30 16:12:22 +00:00
|
|
|
this.position = this.markers[marker].start;
|
|
|
|
this.volume = this.markers[marker].volume;
|
|
|
|
this.loop = this.markers[marker].loop;
|
|
|
|
this.duration = this.markers[marker].duration;
|
|
|
|
this.durationMS = this.markers[marker].durationMS;
|
|
|
|
|
2014-03-21 15:43:45 +00:00
|
|
|
if (typeof volume !== 'undefined')
|
|
|
|
{
|
|
|
|
this.volume = volume;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof loop !== 'undefined')
|
|
|
|
{
|
|
|
|
this.loop = loop;
|
|
|
|
}
|
2013-09-30 16:12:22 +00:00
|
|
|
|
|
|
|
this._tempMarker = marker;
|
|
|
|
this._tempPosition = this.position;
|
|
|
|
this._tempVolume = this.volume;
|
|
|
|
this._tempLoop = this.loop;
|
2015-01-28 17:17:44 +00:00
|
|
|
|
|
|
|
// console.log('Marker pos: ' + this.position + ' duration: ' + this.duration + ' ms: ' + this.durationMS);
|
2013-09-30 16:12:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-28 17:17:44 +00:00
|
|
|
// console.warn("Phaser.Sound.play: audio marker " + marker + " doesn't exist");
|
2014-05-06 23:12:34 +00:00
|
|
|
return this;
|
2013-09-30 16:12:22 +00:00
|
|
|
}
|
2013-09-03 00:24:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-21 15:43:45 +00:00
|
|
|
position = position || 0;
|
|
|
|
|
|
|
|
if (typeof volume === 'undefined') { volume = this._volume; }
|
|
|
|
if (typeof loop === 'undefined') { loop = this.loop; }
|
2013-09-30 10:15:50 +00:00
|
|
|
|
2013-09-03 00:24:16 +00:00
|
|
|
this.position = position;
|
|
|
|
this.volume = volume;
|
|
|
|
this.loop = loop;
|
|
|
|
this.duration = 0;
|
2013-09-30 16:12:22 +00:00
|
|
|
this.durationMS = 0;
|
2013-09-03 00:24:16 +00:00
|
|
|
|
|
|
|
this._tempMarker = marker;
|
|
|
|
this._tempPosition = position;
|
|
|
|
this._tempVolume = volume;
|
|
|
|
this._tempLoop = loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.usingWebAudio)
|
|
|
|
{
|
|
|
|
// Does the sound need decoding?
|
|
|
|
if (this.game.cache.isSoundDecoded(this.key))
|
|
|
|
{
|
|
|
|
// Do we need to do this every time we play? How about just if the buffer is empty?
|
2014-08-29 00:06:29 +00:00
|
|
|
if (this._buffer === null)
|
2013-09-03 00:24:16 +00:00
|
|
|
{
|
|
|
|
this._buffer = this.game.cache.getSoundData(this.key);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._sound = this.context.createBufferSource();
|
|
|
|
this._sound.buffer = this._buffer;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-11-26 05:13:56 +00:00
|
|
|
if (this.externalNode)
|
|
|
|
{
|
2014-05-20 21:09:46 +00:00
|
|
|
this._sound.connect(this.externalNode);
|
2013-11-26 05:13:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._sound.connect(this.gainNode);
|
|
|
|
}
|
|
|
|
|
2013-09-03 00:24:16 +00:00
|
|
|
this.totalDuration = this._sound.buffer.duration;
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
if (this.duration === 0)
|
2013-09-03 00:24:16 +00:00
|
|
|
{
|
2013-09-30 16:12:22 +00:00
|
|
|
// console.log('duration reset');
|
|
|
|
this.duration = this.totalDuration;
|
|
|
|
this.durationMS = this.totalDuration * 1000;
|
2013-09-03 00:24:16 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
if (this.loop && marker === '')
|
2013-09-03 00:24:16 +00:00
|
|
|
{
|
|
|
|
this._sound.loop = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Useful to cache this somewhere perhaps?
|
|
|
|
if (typeof this._sound.start === 'undefined')
|
|
|
|
{
|
2013-09-30 11:17:07 +00:00
|
|
|
this._sound.noteGrainOn(0, this.position, this.duration);
|
|
|
|
// this._sound.noteGrainOn(0, this.position, this.duration / 1000);
|
2013-09-03 00:24:16 +00:00
|
|
|
//this._sound.noteOn(0); // the zero is vitally important, crashes iOS6 without it
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-30 11:17:07 +00:00
|
|
|
// this._sound.start(0, this.position, this.duration / 1000);
|
|
|
|
this._sound.start(0, this.position, this.duration);
|
2013-09-03 00:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.isPlaying = true;
|
2014-11-08 20:01:10 +00:00
|
|
|
this.startTime = this.game.time.time;
|
2013-09-03 00:24:16 +00:00
|
|
|
this.currentTime = 0;
|
2013-09-30 16:12:22 +00:00
|
|
|
this.stopTime = this.startTime + this.durationMS;
|
2013-09-03 00:24:16 +00:00
|
|
|
this.onPlay.dispatch(this);
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-03 00:24:16 +00:00
|
|
|
this.pendingPlayback = true;
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
if (this.game.cache.getSound(this.key) && this.game.cache.getSound(this.key).isDecoding === false)
|
2013-09-03 00:24:16 +00:00
|
|
|
{
|
|
|
|
this.game.sound.decode(this.key, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this.game.cache.getSound(this.key) && this.game.cache.getSound(this.key).locked)
|
|
|
|
{
|
|
|
|
this.game.cache.reloadSound(this.key);
|
|
|
|
this.pendingPlayback = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-31 03:37:11 +00:00
|
|
|
if (this._sound && (this.game.device.cocoonJS || this._sound.readyState === 4))
|
2013-09-03 00:24:16 +00:00
|
|
|
{
|
2013-09-11 10:33:27 +00:00
|
|
|
this._sound.play();
|
|
|
|
// This doesn't become available until you call play(), wonderful ...
|
|
|
|
this.totalDuration = this._sound.duration;
|
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
if (this.duration === 0)
|
2013-09-03 00:24:16 +00:00
|
|
|
{
|
2013-09-30 16:12:22 +00:00
|
|
|
this.duration = this.totalDuration;
|
|
|
|
this.durationMS = this.totalDuration * 1000;
|
2013-09-03 00:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._sound.currentTime = this.position;
|
|
|
|
this._sound.muted = this._muted;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-09-03 00:24:16 +00:00
|
|
|
if (this._muted)
|
|
|
|
{
|
|
|
|
this._sound.volume = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._sound.volume = this._volume;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isPlaying = true;
|
2014-11-08 20:01:10 +00:00
|
|
|
this.startTime = this.game.time.time;
|
2013-09-03 00:24:16 +00:00
|
|
|
this.currentTime = 0;
|
2013-09-30 16:12:22 +00:00
|
|
|
this.stopTime = this.startTime + this.durationMS;
|
2013-09-03 00:24:16 +00:00
|
|
|
this.onPlay.dispatch(this);
|
2015-01-28 17:17:44 +00:00
|
|
|
|
|
|
|
// console.log('stopTime: ' + this.stopTime + ' rs: ' + this._sound.readyState);
|
2013-09-03 00:24:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.pendingPlayback = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-06 23:12:34 +00:00
|
|
|
|
|
|
|
return this;
|
2014-10-11 03:52:06 +00:00
|
|
|
|
2013-09-03 00:24:16 +00:00
|
|
|
},
|
|
|
|
|
2013-09-19 14:34:48 +00:00
|
|
|
/**
|
|
|
|
* Restart the sound, or a marked section of it.
|
2014-04-28 19:30:34 +00:00
|
|
|
*
|
2013-10-02 19:18:24 +00:00
|
|
|
* @method Phaser.Sound#restart
|
2013-10-03 00:21:08 +00:00
|
|
|
* @param {string} [marker=''] - If you want to play a marker then give the key here, otherwise leave blank to play the full sound.
|
|
|
|
* @param {number} [position=0] - The starting position to play the sound from - this is ignored if you provide a marker.
|
|
|
|
* @param {number} [volume=1] - Volume of the sound you want to play.
|
|
|
|
* @param {boolean} [loop=false] - Loop when it finished playing?
|
2013-09-19 14:34:48 +00:00
|
|
|
*/
|
2013-09-03 00:24:16 +00:00
|
|
|
restart: function (marker, position, volume, loop) {
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
marker = marker || '';
|
|
|
|
position = position || 0;
|
|
|
|
volume = volume || 1;
|
2014-10-11 03:52:06 +00:00
|
|
|
if (typeof loop === 'undefined') { loop = false; }
|
2013-09-03 00:24:16 +00:00
|
|
|
|
|
|
|
this.play(marker, position, volume, loop, true);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-09-19 14:34:48 +00:00
|
|
|
/**
|
2014-04-28 19:30:34 +00:00
|
|
|
* Pauses the sound.
|
|
|
|
*
|
2013-10-02 19:18:24 +00:00
|
|
|
* @method Phaser.Sound#pause
|
2013-09-19 14:34:48 +00:00
|
|
|
*/
|
2013-09-03 00:24:16 +00:00
|
|
|
pause: function () {
|
|
|
|
|
|
|
|
if (this.isPlaying && this._sound)
|
|
|
|
{
|
|
|
|
this.paused = true;
|
2013-10-24 03:27:28 +00:00
|
|
|
this.pausedPosition = this.currentTime;
|
2014-11-08 20:01:10 +00:00
|
|
|
this.pausedTime = this.game.time.time;
|
2013-09-03 00:24:16 +00:00
|
|
|
this.onPause.dispatch(this);
|
2014-06-05 01:55:20 +00:00
|
|
|
this.stop();
|
2013-09-03 00:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
2013-10-24 03:27:28 +00:00
|
|
|
|
2013-09-19 14:34:48 +00:00
|
|
|
/**
|
2014-04-28 19:30:34 +00:00
|
|
|
* Resumes the sound.
|
|
|
|
*
|
2013-10-02 19:18:24 +00:00
|
|
|
* @method Phaser.Sound#resume
|
2013-09-19 14:34:48 +00:00
|
|
|
*/
|
2013-09-03 00:24:16 +00:00
|
|
|
resume: function () {
|
|
|
|
|
|
|
|
if (this.paused && this._sound)
|
|
|
|
{
|
|
|
|
if (this.usingWebAudio)
|
|
|
|
{
|
2013-10-24 03:27:28 +00:00
|
|
|
var p = this.position + (this.pausedPosition / 1000);
|
|
|
|
|
|
|
|
this._sound = this.context.createBufferSource();
|
|
|
|
this._sound.buffer = this._buffer;
|
2013-12-31 17:03:09 +00:00
|
|
|
|
|
|
|
if (this.externalNode)
|
|
|
|
{
|
2014-05-20 21:09:46 +00:00
|
|
|
this._sound.connect(this.externalNode);
|
2013-12-31 17:03:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._sound.connect(this.gainNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.loop)
|
|
|
|
{
|
|
|
|
this._sound.loop = true;
|
|
|
|
}
|
2013-10-24 03:27:28 +00:00
|
|
|
|
2013-09-03 00:24:16 +00:00
|
|
|
if (typeof this._sound.start === 'undefined')
|
|
|
|
{
|
2013-10-24 03:27:28 +00:00
|
|
|
this._sound.noteGrainOn(0, p, this.duration);
|
2013-09-03 00:24:16 +00:00
|
|
|
//this._sound.noteOn(0); // the zero is vitally important, crashes iOS6 without it
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-24 03:27:28 +00:00
|
|
|
this._sound.start(0, p, this.duration);
|
2013-09-03 00:24:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._sound.play();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isPlaying = true;
|
|
|
|
this.paused = false;
|
2014-11-08 20:01:10 +00:00
|
|
|
this.startTime += (this.game.time.time - this.pausedTime);
|
2013-09-03 00:24:16 +00:00
|
|
|
this.onResume.dispatch(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2013-09-03 00:24:16 +00:00
|
|
|
* Stop playing this sound.
|
2014-04-01 02:38:13 +00:00
|
|
|
*
|
2013-10-02 19:18:24 +00:00
|
|
|
* @method Phaser.Sound#stop
|
2013-09-03 00:24:16 +00:00
|
|
|
*/
|
|
|
|
stop: function () {
|
|
|
|
|
|
|
|
if (this.isPlaying && this._sound)
|
|
|
|
{
|
|
|
|
if (this.usingWebAudio)
|
|
|
|
{
|
|
|
|
if (typeof this._sound.stop === 'undefined')
|
|
|
|
{
|
|
|
|
this._sound.noteOff(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-09 15:39:35 +00:00
|
|
|
try {
|
|
|
|
this._sound.stop(0);
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
// Thanks Android 4.4
|
|
|
|
}
|
2013-09-03 00:24:16 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-11 12:21:07 +00:00
|
|
|
else if (this.usingAudioTag)
|
2013-09-03 00:24:16 +00:00
|
|
|
{
|
|
|
|
this._sound.pause();
|
|
|
|
this._sound.currentTime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 17:17:44 +00:00
|
|
|
this.pendingPlayback = false;
|
2013-09-03 00:24:16 +00:00
|
|
|
this.isPlaying = false;
|
|
|
|
var prevMarker = this.currentMarker;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-03-02 11:11:20 +00:00
|
|
|
if (this.currentMarker !== '')
|
|
|
|
{
|
|
|
|
this.onMarkerComplete.dispatch(this.currentMarker, this);
|
|
|
|
}
|
|
|
|
|
2013-09-03 00:24:16 +00:00
|
|
|
this.currentMarker = '';
|
2014-06-05 01:55:20 +00:00
|
|
|
|
|
|
|
if (!this.paused)
|
|
|
|
{
|
|
|
|
this.onStop.dispatch(this, prevMarker);
|
|
|
|
}
|
2013-09-03 00:24:16 +00:00
|
|
|
|
2014-04-01 02:38:13 +00:00
|
|
|
},
|
|
|
|
|
2014-09-24 05:51:39 +00:00
|
|
|
/**
|
|
|
|
* Starts this sound playing (or restarts it if already doing so) and sets the volume to zero.
|
|
|
|
* Then increases the volume from 0 to 1 over the duration specified.
|
2014-10-11 03:52:06 +00:00
|
|
|
* At the end of the fade Sound.onFadeComplete is dispatched with this Sound object as the first parameter,
|
2014-09-24 05:51:39 +00:00
|
|
|
* and the final volume (1) as the second parameter.
|
|
|
|
*
|
|
|
|
* @method Phaser.Sound#fadeIn
|
2014-10-15 20:48:00 +00:00
|
|
|
* @param {number} [duration=1000] - The time in milliseconds over which the Sound should fade in.
|
2014-09-24 05:51:39 +00:00
|
|
|
* @param {boolean} [loop=false] - Should the Sound be set to loop? Note that this doesn't cause the fade to repeat.
|
|
|
|
*/
|
|
|
|
fadeIn: function (duration, loop) {
|
|
|
|
|
|
|
|
if (typeof loop === 'undefined') { loop = false; }
|
|
|
|
|
|
|
|
if (this.paused)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.play('', 0, 0, loop);
|
|
|
|
|
2014-10-15 20:48:00 +00:00
|
|
|
this.fadeTo(duration, 1);
|
2014-09-24 05:51:39 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decreases the volume of this Sound from its current value to 0 over the duration specified.
|
2014-10-11 03:52:06 +00:00
|
|
|
* At the end of the fade Sound.onFadeComplete is dispatched with this Sound object as the first parameter,
|
2014-09-24 05:51:39 +00:00
|
|
|
* and the final volume (0) as the second parameter.
|
|
|
|
*
|
|
|
|
* @method Phaser.Sound#fadeOut
|
2014-10-15 20:48:00 +00:00
|
|
|
* @param {number} [duration=1000] - The time in milliseconds over which the Sound should fade out.
|
2014-09-24 05:51:39 +00:00
|
|
|
*/
|
|
|
|
fadeOut: function (duration) {
|
|
|
|
|
2014-10-15 20:48:00 +00:00
|
|
|
this.fadeTo(duration, 0);
|
2014-09-24 05:51:39 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-10-09 22:03:13 +00:00
|
|
|
* Fades the volume of this Sound from its current value to the given volume over the duration specified.
|
|
|
|
* At the end of the fade Sound.onFadeComplete is dispatched with this Sound object as the first parameter,
|
|
|
|
* and the final volume (volume) as the second parameter.
|
|
|
|
*
|
|
|
|
* @method Phaser.Sound#fadeTo
|
|
|
|
* @param {number} [duration=1000] - The time in milliseconds during which the Sound should fade out.
|
2014-10-15 20:48:00 +00:00
|
|
|
* @param {number} [volume] - The volume which the Sound should fade to. This is a value between 0 and 1.
|
2014-10-09 22:03:13 +00:00
|
|
|
*/
|
|
|
|
fadeTo: function (duration, volume) {
|
|
|
|
|
2014-10-15 20:48:00 +00:00
|
|
|
if (!this.isPlaying || this.paused || volume === this.volume)
|
2014-10-09 22:03:13 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-15 20:48:00 +00:00
|
|
|
if (typeof duration === 'undefined') { duration = 1000; }
|
|
|
|
|
|
|
|
if (typeof volume === 'undefined')
|
2014-10-09 22:03:13 +00:00
|
|
|
{
|
2014-10-15 20:48:00 +00:00
|
|
|
console.warn("Phaser.Sound.fadeTo: No Volume Specified.");
|
2014-10-09 22:03:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-28 17:17:44 +00:00
|
|
|
this.fadeTween = this.game.add.tween(this).to( { volume: volume }, duration, Phaser.Easing.Linear.None, true);
|
2014-10-09 22:03:13 +00:00
|
|
|
|
2015-01-28 17:17:44 +00:00
|
|
|
this.fadeTween.onComplete.add(this.fadeComplete, this);
|
2014-10-09 22:03:13 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal handler for Sound.fadeIn, Sound.fadeOut and Sound.fadeTo.
|
2014-09-24 05:51:39 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Sound#fadeComplete
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
fadeComplete: function () {
|
|
|
|
|
|
|
|
this.onFadeComplete.dispatch(this, this.volume);
|
|
|
|
|
|
|
|
if (this.volume === 0)
|
|
|
|
{
|
|
|
|
this.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-04-01 02:38:13 +00:00
|
|
|
/**
|
|
|
|
* Destroys this sound and all associated events and removes it from the SoundManager.
|
|
|
|
*
|
|
|
|
* @method Phaser.Sound#destroy
|
|
|
|
* @param {boolean} [remove=true] - If true this Sound is automatically removed from the SoundManager.
|
|
|
|
*/
|
|
|
|
destroy: function (remove) {
|
|
|
|
|
|
|
|
if (typeof remove === 'undefined') { remove = true; }
|
|
|
|
|
|
|
|
this.stop();
|
|
|
|
|
2015-01-28 17:17:44 +00:00
|
|
|
if (this.fadeTween !== null)
|
|
|
|
{
|
|
|
|
this.fadeTween.stop();
|
|
|
|
}
|
|
|
|
|
2014-04-01 02:38:13 +00:00
|
|
|
if (remove)
|
|
|
|
{
|
|
|
|
this.game.sound.remove(this);
|
|
|
|
}
|
2014-06-05 01:50:53 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
this.markers = {};
|
|
|
|
this.context = null;
|
|
|
|
this._buffer = null;
|
|
|
|
this.externalNode = null;
|
|
|
|
|
|
|
|
this.onDecoded.dispose();
|
|
|
|
this.onPlay.dispose();
|
|
|
|
this.onPause.dispose();
|
|
|
|
this.onResume.dispose();
|
|
|
|
this.onLoop.dispose();
|
|
|
|
this.onStop.dispose();
|
|
|
|
this.onMute.dispose();
|
|
|
|
this.onMarkerComplete.dispose();
|
|
|
|
}
|
2014-04-01 02:38:13 +00:00
|
|
|
|
2013-09-03 00:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-12-30 16:54:00 +00:00
|
|
|
Phaser.Sound.prototype.constructor = Phaser.Sound;
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-03 00:21:08 +00:00
|
|
|
* @name Phaser.Sound#isDecoding
|
|
|
|
* @property {boolean} isDecoding - Returns true if the sound file is still decoding.
|
|
|
|
* @readonly
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-03 00:24:16 +00:00
|
|
|
Object.defineProperty(Phaser.Sound.prototype, "isDecoding", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
return this.game.cache.getSound(this.key).isDecoding;
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
2013-09-03 00:24:16 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-03 00:21:08 +00:00
|
|
|
* @name Phaser.Sound#isDecoded
|
|
|
|
* @property {boolean} isDecoded - Returns true if the sound file has decoded.
|
|
|
|
* @readonly
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-03 00:24:16 +00:00
|
|
|
Object.defineProperty(Phaser.Sound.prototype, "isDecoded", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
return this.game.cache.isSoundDecoded(this.key);
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
2013-09-03 00:24:16 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-03 00:21:08 +00:00
|
|
|
* @name Phaser.Sound#mute
|
|
|
|
* @property {boolean} mute - Gets or sets the muted state of this sound.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-03 00:24:16 +00:00
|
|
|
Object.defineProperty(Phaser.Sound.prototype, "mute", {
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-09-03 00:24:16 +00:00
|
|
|
get: function () {
|
2014-04-28 19:30:34 +00:00
|
|
|
|
|
|
|
return (this._muted || this.game.sound.mute);
|
|
|
|
|
2013-09-03 00:24:16 +00:00
|
|
|
},
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-09-03 00:24:16 +00:00
|
|
|
set: function (value) {
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
value = value || null;
|
2013-09-03 00:24:16 +00:00
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
this._muted = true;
|
|
|
|
|
|
|
|
if (this.usingWebAudio)
|
|
|
|
{
|
|
|
|
this._muteVolume = this.gainNode.gain.value;
|
|
|
|
this.gainNode.gain.value = 0;
|
|
|
|
}
|
|
|
|
else if (this.usingAudioTag && this._sound)
|
|
|
|
{
|
|
|
|
this._muteVolume = this._sound.volume;
|
|
|
|
this._sound.volume = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this._muted = false;
|
|
|
|
|
|
|
|
if (this.usingWebAudio)
|
|
|
|
{
|
|
|
|
this.gainNode.gain.value = this._muteVolume;
|
|
|
|
}
|
|
|
|
else if (this.usingAudioTag && this._sound)
|
|
|
|
{
|
|
|
|
this._sound.volume = this._muteVolume;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.onMute.dispatch(this);
|
|
|
|
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
2013-09-03 00:24:16 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-03 00:21:08 +00:00
|
|
|
* @name Phaser.Sound#volume
|
|
|
|
* @property {number} volume - Gets or sets the volume of this sound, a value between 0 and 1.
|
|
|
|
* @readonly
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-09-03 00:24:16 +00:00
|
|
|
Object.defineProperty(Phaser.Sound.prototype, "volume", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
return this._volume;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
if (this.usingWebAudio)
|
|
|
|
{
|
2013-09-11 10:33:27 +00:00
|
|
|
this._volume = value;
|
2013-09-03 00:24:16 +00:00
|
|
|
this.gainNode.gain.value = value;
|
|
|
|
}
|
2013-09-11 10:33:27 +00:00
|
|
|
else if (this.usingAudioTag && this._sound)
|
2013-09-03 00:24:16 +00:00
|
|
|
{
|
2013-09-11 10:33:27 +00:00
|
|
|
// Causes an Index size error in Firefox if you don't clamp the value
|
|
|
|
if (value >= 0 && value <= 1)
|
|
|
|
{
|
|
|
|
this._volume = value;
|
|
|
|
this._sound.volume = value;
|
|
|
|
}
|
2013-09-03 00:24:16 +00:00
|
|
|
}
|
2013-09-11 12:21:07 +00:00
|
|
|
}
|
2013-09-03 00:24:16 +00:00
|
|
|
|
|
|
|
});
|