mirror of
https://github.com/smallmain/cocos-enhance-kit.git
synced 2025-10-09 11:05:24 +00:00
初始化
This commit is contained in:
568
engine/cocos2d/audio/CCAudio.js
Normal file
568
engine/cocos2d/audio/CCAudio.js
Normal file
@@ -0,0 +1,568 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2008-2010 Ricardo Quesada
|
||||
Copyright (c) 2011-2012 cocos2d-x.org
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
const EventTarget = require('../core/event/event-target');
|
||||
const sys = require('../core/platform/CCSys');
|
||||
const LoadMode = require('../core/assets/CCAudioClip').LoadMode;
|
||||
|
||||
let touchBinded = false;
|
||||
let touchPlayList = [
|
||||
//{ instance: Audio, offset: 0, audio: audio }
|
||||
];
|
||||
|
||||
let Audio = function (src) {
|
||||
EventTarget.call(this);
|
||||
this._shouldRecycleOnEnded = false;
|
||||
this._src = src;
|
||||
this._element = null;
|
||||
this.id = 0;
|
||||
this._state = Audio.State.INITIALZING;
|
||||
|
||||
const self = this;
|
||||
this._onended = function () {
|
||||
self._state = Audio.State.STOPPED;
|
||||
self.emit('ended');
|
||||
};
|
||||
this._onendedSecond = function () {
|
||||
self._unbindEnded(self._onendedSecond);
|
||||
self._bindEnded();
|
||||
};
|
||||
};
|
||||
|
||||
cc.js.extend(Audio, EventTarget);
|
||||
|
||||
/**
|
||||
* !#en Audio state.
|
||||
* !#zh 声音播放状态
|
||||
* @enum audioEngine.AudioState
|
||||
* @memberof cc
|
||||
*/
|
||||
// TODO - At present, the state is mixed with two states of users and systems, and it is best to split into two types. A "loading" should also be added to the system state.
|
||||
Audio.State = {
|
||||
/**
|
||||
* @property {Number} ERROR
|
||||
*/
|
||||
ERROR: -1,
|
||||
/**
|
||||
* @property {Number} INITIALZING
|
||||
*/
|
||||
INITIALZING: 0,
|
||||
/**
|
||||
* @property {Number} PLAYING
|
||||
*/
|
||||
PLAYING: 1,
|
||||
/**
|
||||
* @property {Number} PAUSED
|
||||
*/
|
||||
PAUSED: 2,
|
||||
/**
|
||||
* @property {Number} STOPPED
|
||||
*/
|
||||
STOPPED: 3,
|
||||
};
|
||||
|
||||
(function (proto) {
|
||||
|
||||
proto._bindEnded = function (callback) {
|
||||
callback = callback || this._onended;
|
||||
if (callback._binded) {
|
||||
return;
|
||||
}
|
||||
callback._binded = true;
|
||||
|
||||
let elem = this._element;
|
||||
if (this._src && (elem instanceof HTMLAudioElement)) {
|
||||
elem.addEventListener('ended', callback);
|
||||
} else {
|
||||
elem.onended = callback;
|
||||
}
|
||||
};
|
||||
|
||||
proto._unbindEnded = function (callback) {
|
||||
callback = callback || this._onended;
|
||||
if (!callback._binded) {
|
||||
return;
|
||||
}
|
||||
callback._binded = false;
|
||||
|
||||
let elem = this._element;
|
||||
if (elem instanceof HTMLAudioElement) {
|
||||
elem.removeEventListener('ended', callback);
|
||||
} else if (elem) {
|
||||
elem.onended = null;
|
||||
}
|
||||
};
|
||||
|
||||
proto._onLoaded = function () {
|
||||
this._createElement();
|
||||
this._state = Audio.State.INITIALZING;
|
||||
this.setVolume(1);
|
||||
this.setLoop(false);
|
||||
};
|
||||
|
||||
proto._createElement = function () {
|
||||
let elem = this._src._nativeAsset;
|
||||
if (elem instanceof HTMLAudioElement) {
|
||||
// Reuse dom audio element
|
||||
if (!this._element) {
|
||||
this._element = document.createElement('audio');
|
||||
}
|
||||
this._element.src = elem.src;
|
||||
}
|
||||
else {
|
||||
this._element = new WebAudioElement(elem, this);
|
||||
}
|
||||
};
|
||||
|
||||
proto.play = function () {
|
||||
let self = this;
|
||||
this._src && this._src._ensureLoaded(function () {
|
||||
// marked as playing so it will playOnLoad
|
||||
self._state = Audio.State.PLAYING;
|
||||
// TODO: move to audio event listeners
|
||||
self._bindEnded();
|
||||
let playPromise = self._element.play();
|
||||
// dom audio throws an error if pause audio immediately after playing
|
||||
if (window.Promise && playPromise instanceof Promise) {
|
||||
playPromise.catch(function (err) {
|
||||
// do nothing
|
||||
});
|
||||
}
|
||||
self._touchToPlay();
|
||||
});
|
||||
};
|
||||
|
||||
proto._touchToPlay = function () {
|
||||
if (this._src && this._src.loadMode === LoadMode.DOM_AUDIO &&
|
||||
this._element.paused) {
|
||||
touchPlayList.push({ instance: this, offset: 0, audio: this._element });
|
||||
}
|
||||
|
||||
if (touchBinded) return;
|
||||
touchBinded = true;
|
||||
|
||||
let touchEventName = ('ontouchend' in window) ? 'touchend' : 'mousedown';
|
||||
// Listen to the touchstart body event and play the audio when necessary.
|
||||
cc.game.canvas.addEventListener(touchEventName, function () {
|
||||
let item;
|
||||
while (item = touchPlayList.pop()) {
|
||||
item.audio.play(item.offset);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
proto.destroy = function () {
|
||||
this._element = null;
|
||||
};
|
||||
|
||||
proto.pause = function () {
|
||||
if (this.getState() !== Audio.State.PLAYING) {
|
||||
return;
|
||||
}
|
||||
let self = this;
|
||||
this._src && this._src._ensureLoaded(function () {
|
||||
// pause operation may fire 'ended' event
|
||||
self._unbindEnded();
|
||||
self._element.pause();
|
||||
self._state = Audio.State.PAUSED;
|
||||
});
|
||||
};
|
||||
|
||||
proto.resume = function () {
|
||||
if (this.getState() !== Audio.State.PAUSED) {
|
||||
return;
|
||||
}
|
||||
let self = this;
|
||||
this._src && this._src._ensureLoaded(function () {
|
||||
self._bindEnded();
|
||||
self._element.play();
|
||||
self._state = Audio.State.PLAYING;
|
||||
});
|
||||
};
|
||||
|
||||
proto.stop = function () {
|
||||
let self = this;
|
||||
this._src && this._src._ensureLoaded(function () {
|
||||
self._element.pause();
|
||||
self._element.currentTime = 0;
|
||||
// remove touchPlayList
|
||||
for (let i = 0; i < touchPlayList.length; i++) {
|
||||
if (touchPlayList[i].instance === self) {
|
||||
touchPlayList.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
self._unbindEnded();
|
||||
self.emit('stop');
|
||||
self._state = Audio.State.STOPPED;
|
||||
});
|
||||
};
|
||||
|
||||
proto.setLoop = function (loop) {
|
||||
let self = this;
|
||||
this._src && this._src._ensureLoaded(function () {
|
||||
self._element.loop = loop;
|
||||
});
|
||||
};
|
||||
proto.getLoop = function () {
|
||||
return this._element ? this._element.loop : false;
|
||||
};
|
||||
|
||||
proto.setVolume = function (num) {
|
||||
let self = this;
|
||||
this._src && this._src._ensureLoaded(function () {
|
||||
self._element.volume = num;
|
||||
});
|
||||
};
|
||||
proto.getVolume = function () {
|
||||
return this._element ? this._element.volume : 1;
|
||||
};
|
||||
|
||||
proto.setCurrentTime = function (num) {
|
||||
let self = this;
|
||||
this._src && this._src._ensureLoaded(function () {
|
||||
// setCurrentTime would fire 'ended' event
|
||||
// so we need to change the callback to rebind ended callback after setCurrentTime
|
||||
self._unbindEnded();
|
||||
self._bindEnded(self._onendedSecond);
|
||||
self._element.currentTime = num;
|
||||
});
|
||||
};
|
||||
|
||||
proto.getCurrentTime = function () {
|
||||
return this._element ? this._element.currentTime : 0;
|
||||
};
|
||||
|
||||
proto.getDuration = function () {
|
||||
return this._src ? this._src.duration : 0;
|
||||
};
|
||||
|
||||
proto.getState = function (forceUpdating = true) {
|
||||
// HACK: in some browser, audio may not fire 'ended' event
|
||||
// so we need to force updating the Audio state
|
||||
if (forceUpdating) {
|
||||
this._forceUpdatingState();
|
||||
}
|
||||
return this._state;
|
||||
};
|
||||
|
||||
proto._forceUpdatingState = function () {
|
||||
let elem = this._element;
|
||||
if (elem) {
|
||||
if (Audio.State.PLAYING === this._state && elem.paused) {
|
||||
this._state = Audio.State.STOPPED;
|
||||
}
|
||||
else if (Audio.State.STOPPED === this._state && !elem.paused) {
|
||||
this._state = Audio.State.PLAYING;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Object.defineProperty(proto, 'src', {
|
||||
get: function () {
|
||||
return this._src;
|
||||
},
|
||||
set: function (clip) {
|
||||
this._unbindEnded();
|
||||
if (clip && clip.isValid) {
|
||||
if (clip !== this._src) {
|
||||
this._src = clip;
|
||||
if (!clip.loaded) {
|
||||
let self = this;
|
||||
// need to call clip._ensureLoaded mannually to start loading
|
||||
clip.once('load', function () {
|
||||
// In case set a new src when the old one hasn't finished loading
|
||||
if (clip === self._src) {
|
||||
self._onLoaded();
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
this._onLoaded();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
this._src = null;
|
||||
if (this._element instanceof WebAudioElement) {
|
||||
this._element = null;
|
||||
}
|
||||
else if (this._element) {
|
||||
this._element.src = '';
|
||||
}
|
||||
this._state = Audio.State.INITIALZING;
|
||||
}
|
||||
return clip;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
|
||||
Object.defineProperty(proto, 'paused', {
|
||||
get: function () {
|
||||
return this._element ? this._element.paused : true;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
|
||||
// setFinishCallback
|
||||
|
||||
})(Audio.prototype);
|
||||
|
||||
|
||||
// TIME_CONSTANT is used as an argument of setTargetAtTime interface
|
||||
// TIME_CONSTANT need to be a positive number on Edge and Baidu browser
|
||||
// TIME_CONSTANT need to be 0 by default, or may fail to set volume at the very beginning of playing audio
|
||||
let TIME_CONSTANT;
|
||||
if (cc.sys.browserType === cc.sys.BROWSER_TYPE_EDGE ||
|
||||
cc.sys.browserType === cc.sys.BROWSER_TYPE_BAIDU ||
|
||||
cc.sys.browserType === cc.sys.BROWSER_TYPE_UC) {
|
||||
TIME_CONSTANT = 0.01;
|
||||
}
|
||||
else {
|
||||
TIME_CONSTANT = 0;
|
||||
}
|
||||
|
||||
// Encapsulated WebAudio interface
|
||||
let WebAudioElement = function (buffer, audio) {
|
||||
this._audio = audio;
|
||||
this._context = sys.__audioSupport.context;
|
||||
this._buffer = buffer;
|
||||
|
||||
this._gainObj = this._context['createGain']();
|
||||
this.volume = 1;
|
||||
|
||||
this._gainObj['connect'](this._context['destination']);
|
||||
this._loop = false;
|
||||
// The time stamp on the audio time axis when the recording begins to play.
|
||||
this._startTime = -1;
|
||||
// Record the currently playing 'Source'
|
||||
this._currentSource = null;
|
||||
// Record the time has been played
|
||||
this.playedLength = 0;
|
||||
|
||||
this._currentTimer = null;
|
||||
|
||||
this._endCallback = function () {
|
||||
if (this.onended) {
|
||||
this.onended(this);
|
||||
}
|
||||
}.bind(this);
|
||||
};
|
||||
|
||||
(function (proto) {
|
||||
proto.play = function (offset) {
|
||||
// If repeat play, you need to stop before an audio
|
||||
if (this._currentSource && !this.paused) {
|
||||
this._currentSource.onended = null;
|
||||
this._currentSource.stop(0);
|
||||
this.playedLength = 0;
|
||||
}
|
||||
|
||||
let audio = this._context["createBufferSource"]();
|
||||
audio.buffer = this._buffer;
|
||||
audio["connect"](this._gainObj);
|
||||
audio.loop = this._loop;
|
||||
|
||||
this._startTime = this._context.currentTime;
|
||||
offset = offset || this.playedLength;
|
||||
if (offset) {
|
||||
this._startTime -= offset;
|
||||
}
|
||||
let duration = this._buffer.duration;
|
||||
|
||||
let startTime = offset;
|
||||
let endTime;
|
||||
if (this._loop) {
|
||||
if (audio.start)
|
||||
audio.start(0, startTime);
|
||||
else if (audio["notoGrainOn"])
|
||||
audio["noteGrainOn"](0, startTime);
|
||||
else
|
||||
audio["noteOn"](0, startTime);
|
||||
} else {
|
||||
endTime = duration - offset;
|
||||
if (audio.start)
|
||||
audio.start(0, startTime, endTime);
|
||||
else if (audio["noteGrainOn"])
|
||||
audio["noteGrainOn"](0, startTime, endTime);
|
||||
else
|
||||
audio["noteOn"](0, startTime, endTime);
|
||||
}
|
||||
|
||||
this._currentSource = audio;
|
||||
|
||||
audio.onended = this._endCallback;
|
||||
|
||||
// If the current audio context time stamp is 0 and audio context state is suspended
|
||||
// There may be a need to touch events before you can actually start playing audio
|
||||
if ((!audio.context.state || audio.context.state === "suspended") && this._context.currentTime === 0) {
|
||||
let self = this;
|
||||
clearTimeout(this._currentTimer);
|
||||
this._currentTimer = setTimeout(function () {
|
||||
if (self._context.currentTime === 0) {
|
||||
touchPlayList.push({
|
||||
instance: self._audio,
|
||||
offset: offset,
|
||||
audio: self
|
||||
});
|
||||
}
|
||||
}, 10);
|
||||
}
|
||||
|
||||
let sys = cc.sys;
|
||||
if (sys.os === sys.OS_IOS && sys.isBrowser && sys.isMobile) {
|
||||
// Audio context is suspended when you unplug the earphones,
|
||||
// and is interrupted when the app enters background.
|
||||
// Both make the audioBufferSource unplayable.
|
||||
if ((audio.context.state === "suspended" && this._context.currentTime !== 0)
|
||||
|| audio.context.state === 'interrupted') {
|
||||
// reference: https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/resume
|
||||
audio.context.resume();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
proto.pause = function () {
|
||||
clearTimeout(this._currentTimer);
|
||||
if (this.paused) return;
|
||||
// Record the time the current has been played
|
||||
this.playedLength = this._context.currentTime - this._startTime;
|
||||
// If more than the duration of the audio, Need to take the remainder
|
||||
this.playedLength %= this._buffer.duration;
|
||||
let audio = this._currentSource;
|
||||
if (audio) {
|
||||
if(audio.onended){
|
||||
audio.onended._binded = false;
|
||||
audio.onended = null;
|
||||
}
|
||||
audio.stop(0);
|
||||
}
|
||||
this._currentSource = null;
|
||||
this._startTime = -1;
|
||||
|
||||
};
|
||||
|
||||
Object.defineProperty(proto, 'paused', {
|
||||
get: function () {
|
||||
// If the current audio is a loop, paused is false
|
||||
if (this._currentSource && this._currentSource.loop)
|
||||
return false;
|
||||
|
||||
// startTime default is -1
|
||||
if (this._startTime === -1)
|
||||
return true;
|
||||
|
||||
// Current time - Start playing time > Audio duration
|
||||
return this._context.currentTime - this._startTime > this._buffer.duration;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
|
||||
Object.defineProperty(proto, 'loop', {
|
||||
get: function () {
|
||||
return this._loop;
|
||||
},
|
||||
set: function (bool) {
|
||||
if (this._currentSource)
|
||||
this._currentSource.loop = bool;
|
||||
|
||||
return this._loop = bool;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
|
||||
Object.defineProperty(proto, 'volume', {
|
||||
get: function () {
|
||||
return this._volume;
|
||||
},
|
||||
set: function (num) {
|
||||
this._volume = num;
|
||||
// https://www.chromestatus.com/features/5287995770929152
|
||||
if (this._gainObj.gain.setTargetAtTime) {
|
||||
try {
|
||||
this._gainObj.gain.setTargetAtTime(num, this._context.currentTime, TIME_CONSTANT);
|
||||
}
|
||||
catch (e) {
|
||||
// Some other unknown browsers may crash if TIME_CONSTANT is 0
|
||||
this._gainObj.gain.setTargetAtTime(num, this._context.currentTime, 0.01);
|
||||
}
|
||||
}
|
||||
else {
|
||||
this._gainObj.gain.value = num;
|
||||
}
|
||||
|
||||
if (sys.os === sys.OS_IOS && !this.paused && this._currentSource) {
|
||||
// IOS must be stop webAudio
|
||||
this._currentSource.onended = null;
|
||||
this.pause();
|
||||
this.play();
|
||||
}
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
|
||||
Object.defineProperty(proto, 'currentTime', {
|
||||
get: function () {
|
||||
if (this.paused) {
|
||||
return this.playedLength;
|
||||
}
|
||||
// Record the time the current has been played
|
||||
this.playedLength = this._context.currentTime - this._startTime;
|
||||
// If more than the duration of the audio, Need to take the remainder
|
||||
this.playedLength %= this._buffer.duration;
|
||||
return this.playedLength;
|
||||
},
|
||||
set: function (num) {
|
||||
if (!this.paused) {
|
||||
this.pause();
|
||||
this.playedLength = num;
|
||||
this.play();
|
||||
} else {
|
||||
this.playedLength = num;
|
||||
}
|
||||
return num;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
|
||||
Object.defineProperty(proto, 'duration', {
|
||||
get: function () {
|
||||
return this._buffer.duration;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
|
||||
})(WebAudioElement.prototype);
|
||||
|
||||
module.exports = cc._Audio = Audio;
|
791
engine/cocos2d/audio/CCAudioEngine.js
Normal file
791
engine/cocos2d/audio/CCAudioEngine.js
Normal file
@@ -0,0 +1,791 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2008-2010 Ricardo Quesada
|
||||
Copyright (c) 2011-2012 cocos2d-x.org
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
const Audio = require('./CCAudio');
|
||||
const AudioClip = require('../core/assets/CCAudioClip');
|
||||
const js = cc.js;
|
||||
|
||||
let _instanceId = 0;
|
||||
let _id2audio = js.createMap(true);
|
||||
let _url2id = {};
|
||||
let _audioPool = [];
|
||||
|
||||
let recycleAudio = function (audio) {
|
||||
// In case repeatly recycle audio when users call audio.stop when audio finish playing
|
||||
if (!audio._shouldRecycleOnEnded) {
|
||||
return;
|
||||
}
|
||||
audio._finishCallback = null;
|
||||
audio.off('ended');
|
||||
audio.off('stop');
|
||||
audio.src = null;
|
||||
// In case repeatly recycle audio
|
||||
if (!_audioPool.includes(audio)) {
|
||||
if (_audioPool.length < audioEngine._maxPoolSize) {
|
||||
_audioPool.push(audio);
|
||||
}
|
||||
else {
|
||||
audio.destroy();
|
||||
}
|
||||
}
|
||||
audio._shouldRecycleOnEnded = false;
|
||||
};
|
||||
|
||||
let getAudioFromPath = function (path) {
|
||||
var id = ++_instanceId;
|
||||
var list = _url2id[path];
|
||||
if (!list) {
|
||||
list = _url2id[path] = [];
|
||||
}
|
||||
if (audioEngine._maxAudioInstance <= list.length) {
|
||||
var oldId = list.shift();
|
||||
var oldAudio = getAudioFromId(oldId);
|
||||
// Stop will recycle audio automatically by event callback
|
||||
oldAudio.stop();
|
||||
}
|
||||
|
||||
var audio = _audioPool.pop() || new Audio();
|
||||
var callback = function () {
|
||||
var audioInList = getAudioFromId(this.id);
|
||||
if (audioInList) {
|
||||
delete _id2audio[this.id];
|
||||
var index = list.indexOf(this.id);
|
||||
cc.js.array.fastRemoveAt(list, index);
|
||||
}
|
||||
recycleAudio(this);
|
||||
};
|
||||
|
||||
audio.on('ended', function () {
|
||||
if (this._finishCallback) {
|
||||
this._finishCallback();
|
||||
}
|
||||
if(!this.getLoop()){
|
||||
callback.call(this);
|
||||
}
|
||||
}, audio);
|
||||
|
||||
audio.on('stop', callback, audio);
|
||||
audio.id = id;
|
||||
_id2audio[id] = audio;
|
||||
list.push(id);
|
||||
|
||||
return audio;
|
||||
};
|
||||
|
||||
let getAudioFromId = function (id) {
|
||||
return _id2audio[id];
|
||||
};
|
||||
|
||||
let handleVolume = function (volume) {
|
||||
if (volume === undefined) {
|
||||
// set default volume as 1
|
||||
volume = 1;
|
||||
}
|
||||
else if (typeof volume === 'string') {
|
||||
volume = Number.parseFloat(volume);
|
||||
}
|
||||
return volume;
|
||||
};
|
||||
|
||||
/**
|
||||
* !#en `cc.audioEngine` is the singleton object, it provide simple audio APIs.
|
||||
* !#zh
|
||||
* cc.audioengine是单例对象。<br/>
|
||||
* 主要用来播放音频,播放的时候会返回一个 audioID,之后都可以通过这个 audioID 来操作这个音频对象。<br/>
|
||||
* 不使用的时候,请使用 `cc.audioEngine.uncache(filePath);` 进行资源释放 <br/>
|
||||
* 注意:<br/>
|
||||
* 在 Android 系统浏览器上,不同浏览器,不同版本的效果不尽相同。<br/>
|
||||
* 比如说:大多数浏览器都需要用户物理交互才可以开始播放音效,有一些不支持 WebAudio,有一些不支持多音轨播放。总之如果对音乐依赖比较强,请做尽可能多的测试。
|
||||
* @class audioEngine
|
||||
* @static
|
||||
*/
|
||||
var audioEngine = {
|
||||
|
||||
AudioState: Audio.State,
|
||||
|
||||
_maxAudioInstance: 24,
|
||||
|
||||
_maxPoolSize: 32,
|
||||
|
||||
_id2audio: _id2audio,
|
||||
|
||||
/**
|
||||
* !#en Play audio.
|
||||
* !#zh 播放音频
|
||||
* @method play
|
||||
* @param {AudioClip} clip - The audio clip to play.
|
||||
* @param {Boolean} loop - Whether the music loop or not.
|
||||
* @param {Number} volume - Volume size.
|
||||
* @return {Number} audioId
|
||||
* @example
|
||||
* cc.resources.load(path, cc.AudioClip, null, function (err, clip) {
|
||||
* var audioID = cc.audioEngine.play(clip, false, 0.5);
|
||||
* });
|
||||
*/
|
||||
play: function (clip, loop, volume) {
|
||||
if (CC_EDITOR) {
|
||||
return;
|
||||
}
|
||||
if (!(clip instanceof AudioClip)) {
|
||||
return cc.error('Wrong type of AudioClip.');
|
||||
}
|
||||
let path = clip.nativeUrl;
|
||||
let audio = getAudioFromPath(path);
|
||||
audio.src = clip;
|
||||
clip._ensureLoaded();
|
||||
audio._shouldRecycleOnEnded = true;
|
||||
audio.setLoop(loop || false);
|
||||
volume = handleVolume(volume);
|
||||
audio.setVolume(volume);
|
||||
audio.play();
|
||||
return audio.id;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Set audio loop.
|
||||
* !#zh 设置音频是否循环。
|
||||
* @method setLoop
|
||||
* @param {Number} audioID - audio id.
|
||||
* @param {Boolean} loop - Whether cycle.
|
||||
* @example
|
||||
* cc.audioEngine.setLoop(id, true);
|
||||
*/
|
||||
setLoop: function (audioID, loop) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
if (!audio || !audio.setLoop)
|
||||
return;
|
||||
audio.setLoop(loop);
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Get audio cycle state.
|
||||
* !#zh 获取音频的循环状态。
|
||||
* @method isLoop
|
||||
* @param {Number} audioID - audio id.
|
||||
* @return {Boolean} Whether cycle.
|
||||
* @example
|
||||
* cc.audioEngine.isLoop(id);
|
||||
*/
|
||||
isLoop: function (audioID) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
if (!audio || !audio.getLoop)
|
||||
return false;
|
||||
return audio.getLoop();
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Set the volume of audio.
|
||||
* !#zh 设置音量(0.0 ~ 1.0)。
|
||||
* @method setVolume
|
||||
* @param {Number} audioID - audio id.
|
||||
* @param {Number} volume - Volume must be in 0.0~1.0 .
|
||||
* @example
|
||||
* cc.audioEngine.setVolume(id, 0.5);
|
||||
*/
|
||||
setVolume: function (audioID, volume) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
if (audio) {
|
||||
audio.setVolume(volume);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en The volume of the music max value is 1.0,the min value is 0.0 .
|
||||
* !#zh 获取音量(0.0 ~ 1.0)。
|
||||
* @method getVolume
|
||||
* @param {Number} audioID - audio id.
|
||||
* @return {Number}
|
||||
* @example
|
||||
* var volume = cc.audioEngine.getVolume(id);
|
||||
*/
|
||||
getVolume: function (audioID) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
return audio ? audio.getVolume() : 1;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Set current time
|
||||
* !#zh 设置当前的音频时间。
|
||||
* @method setCurrentTime
|
||||
* @param {Number} audioID - audio id.
|
||||
* @param {Number} sec - current time.
|
||||
* @return {Boolean}
|
||||
* @example
|
||||
* cc.audioEngine.setCurrentTime(id, 2);
|
||||
*/
|
||||
setCurrentTime: function (audioID, sec) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
if (audio) {
|
||||
audio.setCurrentTime(sec);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Get current time
|
||||
* !#zh 获取当前的音频播放时间。
|
||||
* @method getCurrentTime
|
||||
* @param {Number} audioID - audio id.
|
||||
* @return {Number} audio current time.
|
||||
* @example
|
||||
* var time = cc.audioEngine.getCurrentTime(id);
|
||||
*/
|
||||
getCurrentTime: function (audioID) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
return audio ? audio.getCurrentTime() : 0;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Get audio duration
|
||||
* !#zh 获取音频总时长。
|
||||
* @method getDuration
|
||||
* @param {Number} audioID - audio id.
|
||||
* @return {Number} audio duration.
|
||||
* @example
|
||||
* var time = cc.audioEngine.getDuration(id);
|
||||
*/
|
||||
getDuration: function (audioID) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
return audio ? audio.getDuration() : 0;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Get audio state
|
||||
* !#zh 获取音频状态。
|
||||
* @method getState
|
||||
* @param {Number} audioID - audio id.
|
||||
* @return {audioEngine.AudioState} audio duration.
|
||||
* @example
|
||||
* var state = cc.audioEngine.getState(id);
|
||||
*/
|
||||
getState: function (audioID) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
return audio ? audio.getState() : this.AudioState.ERROR;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Whether the audio is playing
|
||||
* !#zh 音乐是否正在播放
|
||||
* @method isPlaying
|
||||
* @return {Boolean}
|
||||
* @example
|
||||
* cc.audioEngine.isPlaying(audioID);
|
||||
*/
|
||||
isPlaying: function(audioID) {
|
||||
return this.getState(audioID) === this.AudioState.PLAYING;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Set Audio finish callback
|
||||
* !#zh 设置一个音频结束后的回调
|
||||
* @method setFinishCallback
|
||||
* @param {Number} audioID - audio id.
|
||||
* @param {Function} callback - loaded callback.
|
||||
* @example
|
||||
* cc.audioEngine.setFinishCallback(id, function () {});
|
||||
*/
|
||||
setFinishCallback: function (audioID, callback) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
if (!audio)
|
||||
return;
|
||||
audio._finishCallback = callback;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Pause playing audio.
|
||||
* !#zh 暂停正在播放音频。
|
||||
* @method pause
|
||||
* @param {Number} audioID - The return value of function play.
|
||||
* @example
|
||||
* cc.audioEngine.pause(audioID);
|
||||
*/
|
||||
pause: function (audioID) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
if (audio) {
|
||||
audio.pause();
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
_pauseIDCache: [],
|
||||
/**
|
||||
* !#en Pause all playing audio
|
||||
* !#zh 暂停现在正在播放的所有音频。
|
||||
* @method pauseAll
|
||||
* @example
|
||||
* cc.audioEngine.pauseAll();
|
||||
*/
|
||||
pauseAll: function () {
|
||||
for (var id in _id2audio) {
|
||||
var audio = _id2audio[id];
|
||||
var state = audio.getState();
|
||||
if (state === Audio.State.PLAYING) {
|
||||
this._pauseIDCache.push(id);
|
||||
audio.pause();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Resume playing audio.
|
||||
* !#zh 恢复播放指定的音频。
|
||||
* @method resume
|
||||
* @param {Number} audioID - The return value of function play.
|
||||
* @example
|
||||
* cc.audioEngine.resume(audioID);
|
||||
*/
|
||||
resume: function (audioID) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
if (audio) {
|
||||
audio.resume();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Resume all playing audio.
|
||||
* !#zh 恢复播放所有之前暂停的所有音频。
|
||||
* @method resumeAll
|
||||
* @example
|
||||
* cc.audioEngine.resumeAll();
|
||||
*/
|
||||
resumeAll: function () {
|
||||
for (var i = 0; i < this._pauseIDCache.length; ++i) {
|
||||
var id = this._pauseIDCache[i];
|
||||
var audio = getAudioFromId(id);
|
||||
if (audio)
|
||||
audio.resume();
|
||||
}
|
||||
this._pauseIDCache.length = 0;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Stop playing audio.
|
||||
* !#zh 停止播放指定音频。
|
||||
* @method stop
|
||||
* @param {Number} audioID - The return value of function play.
|
||||
* @example
|
||||
* cc.audioEngine.stop(audioID);
|
||||
*/
|
||||
stop: function (audioID) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
if (audio) {
|
||||
// Stop will recycle audio automatically by event callback
|
||||
audio.stop();
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Stop all playing audio.
|
||||
* !#zh 停止正在播放的所有音频。
|
||||
* @method stopAll
|
||||
* @example
|
||||
* cc.audioEngine.stopAll();
|
||||
*/
|
||||
stopAll: function () {
|
||||
for (var id in _id2audio) {
|
||||
var audio = _id2audio[id];
|
||||
if (audio) {
|
||||
// Stop will recycle audio automatically by event callback
|
||||
audio.stop();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Set up an audio can generate a few examples.
|
||||
* !#zh 设置一个音频可以设置几个实例
|
||||
* @method setMaxAudioInstance
|
||||
* @param {Number} num - a number of instances to be created from within an audio
|
||||
* @example
|
||||
* cc.audioEngine.setMaxAudioInstance(20);
|
||||
* @deprecated since v2.4.0
|
||||
*/
|
||||
setMaxAudioInstance: function (num) {
|
||||
if (CC_DEBUG) {
|
||||
cc.warn('Since v2.4.0, maxAudioInstance has become a read only property.\n'
|
||||
+ 'audioEngine.setMaxAudioInstance() method will be removed in the future');
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Getting audio can produce several examples.
|
||||
* !#zh 获取一个音频可以设置几个实例
|
||||
* @method getMaxAudioInstance
|
||||
* @return {Number} max number of instances to be created from within an audio
|
||||
* @example
|
||||
* cc.audioEngine.getMaxAudioInstance();
|
||||
*/
|
||||
getMaxAudioInstance: function () {
|
||||
return this._maxAudioInstance;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Unload the preloaded audio from internal buffer.
|
||||
* !#zh 卸载预加载的音频。
|
||||
* @method uncache
|
||||
* @param {AudioClip} clip
|
||||
* @example
|
||||
* cc.audioEngine.uncache(filePath);
|
||||
*/
|
||||
uncache: function (clip) {
|
||||
var filePath = clip;
|
||||
if (typeof clip === 'string') {
|
||||
// backward compatibility since 1.10
|
||||
cc.warnID(8401, 'cc.audioEngine', 'cc.AudioClip', 'AudioClip', 'cc.AudioClip', 'audio');
|
||||
filePath = clip;
|
||||
}
|
||||
else {
|
||||
if (!clip) {
|
||||
return;
|
||||
}
|
||||
filePath = clip.nativeUrl;
|
||||
}
|
||||
|
||||
var list = _url2id[filePath];
|
||||
if (!list) return;
|
||||
while (list.length > 0) {
|
||||
var id = list.pop();
|
||||
var audio = _id2audio[id];
|
||||
if (audio) {
|
||||
// Stop will recycle audio automatically by event callback
|
||||
audio.stop();
|
||||
delete _id2audio[id];
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Unload all audio from internal buffer.
|
||||
* !#zh 卸载所有音频。
|
||||
* @method uncacheAll
|
||||
* @example
|
||||
* cc.audioEngine.uncacheAll();
|
||||
*/
|
||||
uncacheAll: function () {
|
||||
this.stopAll();
|
||||
let audio;
|
||||
for (let id in _id2audio) {
|
||||
audio = _id2audio[id];
|
||||
if (audio) {
|
||||
audio.destroy();
|
||||
}
|
||||
}
|
||||
while (audio = _audioPool.pop()) {
|
||||
audio.destroy();
|
||||
}
|
||||
_id2audio = js.createMap(true);
|
||||
_url2id = {};
|
||||
},
|
||||
|
||||
_breakCache: null,
|
||||
_break: function () {
|
||||
this._breakCache = [];
|
||||
for (var id in _id2audio) {
|
||||
var audio = _id2audio[id];
|
||||
var state = audio.getState();
|
||||
if (state === Audio.State.PLAYING) {
|
||||
this._breakCache.push(id);
|
||||
audio.pause();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
_restore: function () {
|
||||
if (!this._breakCache) return;
|
||||
|
||||
while (this._breakCache.length > 0) {
|
||||
var id = this._breakCache.pop();
|
||||
var audio = getAudioFromId(id);
|
||||
if (audio && audio.resume)
|
||||
audio.resume();
|
||||
}
|
||||
this._breakCache = null;
|
||||
},
|
||||
|
||||
///////////////////////////////
|
||||
// Classification of interface
|
||||
|
||||
_music: {
|
||||
id: -1,
|
||||
loop: false,
|
||||
volume: 1,
|
||||
},
|
||||
|
||||
_effect: {
|
||||
volume: 1,
|
||||
pauseCache: [],
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Play background music
|
||||
* !#zh 播放背景音乐
|
||||
* @method playMusic
|
||||
* @param {AudioClip} clip - The audio clip to play.
|
||||
* @param {Boolean} loop - Whether the music loop or not.
|
||||
* @return {Number} audioId
|
||||
* @example
|
||||
* cc.resources.load(path, cc.AudioClip, null, function (err, clip) {
|
||||
* var audioID = cc.audioEngine.playMusic(clip, false);
|
||||
* });
|
||||
*/
|
||||
playMusic: function (clip, loop) {
|
||||
var music = this._music;
|
||||
this.stop(music.id);
|
||||
music.id = this.play(clip, loop, music.volume);
|
||||
music.loop = loop;
|
||||
return music.id;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Stop background music.
|
||||
* !#zh 停止播放背景音乐。
|
||||
* @method stopMusic
|
||||
* @example
|
||||
* cc.audioEngine.stopMusic();
|
||||
*/
|
||||
stopMusic: function () {
|
||||
this.stop(this._music.id);
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Pause the background music.
|
||||
* !#zh 暂停播放背景音乐。
|
||||
* @method pauseMusic
|
||||
* @example
|
||||
* cc.audioEngine.pauseMusic();
|
||||
*/
|
||||
pauseMusic: function () {
|
||||
this.pause(this._music.id);
|
||||
return this._music.id;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Resume playing background music.
|
||||
* !#zh 恢复播放背景音乐。
|
||||
* @method resumeMusic
|
||||
* @example
|
||||
* cc.audioEngine.resumeMusic();
|
||||
*/
|
||||
resumeMusic: function () {
|
||||
this.resume(this._music.id);
|
||||
return this._music.id;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Get the volume(0.0 ~ 1.0).
|
||||
* !#zh 获取音量(0.0 ~ 1.0)。
|
||||
* @method getMusicVolume
|
||||
* @return {Number}
|
||||
* @example
|
||||
* var volume = cc.audioEngine.getMusicVolume();
|
||||
*/
|
||||
getMusicVolume: function () {
|
||||
return this._music.volume;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Set the background music volume.
|
||||
* !#zh 设置背景音乐音量(0.0 ~ 1.0)。
|
||||
* @method setMusicVolume
|
||||
* @param {Number} volume - Volume must be in 0.0~1.0.
|
||||
* @example
|
||||
* cc.audioEngine.setMusicVolume(0.5);
|
||||
*/
|
||||
setMusicVolume: function (volume) {
|
||||
volume = handleVolume(volume);
|
||||
var music = this._music;
|
||||
music.volume = volume;
|
||||
this.setVolume(music.id, music.volume);
|
||||
return music.volume;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Background music playing state
|
||||
* !#zh 背景音乐是否正在播放
|
||||
* @method isMusicPlaying
|
||||
* @return {Boolean}
|
||||
* @example
|
||||
* cc.audioEngine.isMusicPlaying();
|
||||
*/
|
||||
isMusicPlaying: function () {
|
||||
return this.getState(this._music.id) === this.AudioState.PLAYING;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Play effect audio.
|
||||
* !#zh 播放音效
|
||||
* @method playEffect
|
||||
* @param {AudioClip} clip - The audio clip to play.
|
||||
* @param {Boolean} loop - Whether the music loop or not.
|
||||
* @return {Number} audioId
|
||||
* @example
|
||||
* cc.resources.load(path, cc.AudioClip, null, function (err, clip) {
|
||||
* var audioID = cc.audioEngine.playEffect(clip, false);
|
||||
* });
|
||||
*/
|
||||
playEffect: function (clip, loop) {
|
||||
return this.play(clip, loop || false, this._effect.volume);
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Set the volume of effect audio.
|
||||
* !#zh 设置音效音量(0.0 ~ 1.0)。
|
||||
* @method setEffectsVolume
|
||||
* @param {Number} volume - Volume must be in 0.0~1.0.
|
||||
* @example
|
||||
* cc.audioEngine.setEffectsVolume(0.5);
|
||||
*/
|
||||
setEffectsVolume: function (volume) {
|
||||
volume = handleVolume(volume);
|
||||
var musicId = this._music.id;
|
||||
this._effect.volume = volume;
|
||||
for (var id in _id2audio) {
|
||||
var audio = _id2audio[id];
|
||||
if (!audio || audio.id === musicId) continue;
|
||||
audioEngine.setVolume(id, volume);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en The volume of the effect audio max value is 1.0,the min value is 0.0 .
|
||||
* !#zh 获取音效音量(0.0 ~ 1.0)。
|
||||
* @method getEffectsVolume
|
||||
* @return {Number}
|
||||
* @example
|
||||
* var volume = cc.audioEngine.getEffectsVolume();
|
||||
*/
|
||||
getEffectsVolume: function () {
|
||||
return this._effect.volume;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Pause effect audio.
|
||||
* !#zh 暂停播放音效。
|
||||
* @method pauseEffect
|
||||
* @param {Number} audioID - audio id.
|
||||
* @example
|
||||
* cc.audioEngine.pauseEffect(audioID);
|
||||
*/
|
||||
pauseEffect: function (audioID) {
|
||||
return this.pause(audioID);
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Stop playing all the sound effects.
|
||||
* !#zh 暂停播放所有音效。
|
||||
* @method pauseAllEffects
|
||||
* @example
|
||||
* cc.audioEngine.pauseAllEffects();
|
||||
*/
|
||||
pauseAllEffects: function () {
|
||||
var musicId = this._music.id;
|
||||
var effect = this._effect;
|
||||
effect.pauseCache.length = 0;
|
||||
|
||||
for (var id in _id2audio) {
|
||||
var audio = _id2audio[id];
|
||||
if (!audio || audio.id === musicId) continue;
|
||||
var state = audio.getState();
|
||||
if (state === this.AudioState.PLAYING) {
|
||||
effect.pauseCache.push(id);
|
||||
audio.pause();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Resume effect audio.
|
||||
* !#zh 恢复播放音效音频。
|
||||
* @method resumeEffect
|
||||
* @param {Number} audioID - The return value of function play.
|
||||
* @example
|
||||
* cc.audioEngine.resumeEffect(audioID);
|
||||
*/
|
||||
resumeEffect: function (id) {
|
||||
this.resume(id);
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Resume all effect audio.
|
||||
* !#zh 恢复播放所有之前暂停的音效。
|
||||
* @method resumeAllEffects
|
||||
* @example
|
||||
* cc.audioEngine.resumeAllEffects();
|
||||
*/
|
||||
resumeAllEffects: function () {
|
||||
var pauseIDCache = this._effect.pauseCache;
|
||||
for (var i = 0; i < pauseIDCache.length; ++i) {
|
||||
var id = pauseIDCache[i];
|
||||
var audio = _id2audio[id];
|
||||
if (audio)
|
||||
audio.resume();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Stop playing the effect audio.
|
||||
* !#zh 停止播放音效。
|
||||
* @method stopEffect
|
||||
* @param {Number} audioID - audio id.
|
||||
* @example
|
||||
* cc.audioEngine.stopEffect(id);
|
||||
*/
|
||||
stopEffect: function (audioID) {
|
||||
return this.stop(audioID);
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Stop playing all the effects.
|
||||
* !#zh 停止播放所有音效。
|
||||
* @method stopAllEffects
|
||||
* @example
|
||||
* cc.audioEngine.stopAllEffects();
|
||||
*/
|
||||
stopAllEffects: function () {
|
||||
var musicId = this._music.id;
|
||||
for (var id in _id2audio) {
|
||||
var audio = _id2audio[id];
|
||||
if (!audio || audio.id === musicId) continue;
|
||||
var state = audio.getState();
|
||||
if (state === audioEngine.AudioState.PLAYING) {
|
||||
audio.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = cc.audioEngine = audioEngine;
|
Reference in New Issue
Block a user