diff --git a/DebugInfos.js b/DebugInfos.js index 07725313e1..d9c08fcd8a 100644 --- a/DebugInfos.js +++ b/DebugInfos.js @@ -377,8 +377,8 @@ if (CC_DEBUG) { " set %s (value) {\n" + " ...\n" + " }", - "3656": "The default value of %s.%s must be an empty string. (changed since 1.8)", - "3657": "The value assigned to %s should be Texture2D object, not url string. Since 1.8,\n" + + "3656": "The default value of %s.%s must be an empty string. (changed since 1.10)", + "3657": "The value assigned to %s should be Texture2D object, not url string. Since 1.10`,\n" + "you can declare a texture object directly in properties by using:\n" + "{\n" + " default: null,\n" + @@ -725,5 +725,6 @@ if (CC_DEBUG) { "8301": "Camera does not support Canvas Mode.", //cc.Camera.enabled // AudioEngine: 8400 "8400": "Wrong type arguments, 'filePath' must be a String.", // cc.audioEngine.play + "8401": "Since 1.10, `%s` accept cc.AudioClip instance directly, not a URL string. Please directly reference the AudioClip object in your script, or load cc.AudioClip by loader first. Don't use audio's URL anymore.", }; } diff --git a/cocos2d/audio/CCAudio.js b/cocos2d/audio/CCAudio.js index 00b78624cd..b358465b38 100644 --- a/cocos2d/audio/CCAudio.js +++ b/cocos2d/audio/CCAudio.js @@ -27,6 +27,7 @@ var EventTarget = require('../core/event/event-target'); var sys = require('../core/platform/CCSys'); +var LoadMode = require('../core/assets/CCAudioClip').LoadMode; var touchBinded = false; var touchPlayList = [ @@ -37,14 +38,14 @@ var Audio = function (src) { EventTarget.call(this); this._src = src; - this._audioType = Audio.Type.UNKNOWN; this._element = null; + this.id = 0; + + this._volume = 1; + this._loop = false; + this._nextTime = 0; // playback position to set - this._eventList = {}; this._state = Audio.State.INITIALZING; - this._loaded = false; - - this._isLoading = false; this._onended = function () { this.emit('ended'); @@ -53,20 +54,13 @@ var Audio = function (src) { cc.js.extend(Audio, EventTarget); -Audio.Type = { - DOM: 'AUDIO', - WEBAUDIO: 'WEBAUDIO', - NATIVE: 'NATIVE', - UNKNOWN: 'UNKNOWN' -}; - /** * !#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 @@ -88,49 +82,9 @@ Audio.State = { (function (proto) { - proto.preload = function () { - var src = this._src, audio = this; - - if (this._isLoading) { - return; - } - this._isLoading = true; - - if (!src) { - this._src = ''; - this._audioType = Audio.Type.UNKNOWN; - this._element = null; - this._state = Audio.State.INITIALZING; - this._loaded = false; - return; - } - - var item = cc.loader.getItem(src); - - if (!item) { - item = cc.loader.getItem(src + '?useDom=1'); - } - - // If the resource does not exist - if (!item || !item.complete) { - return cc.loader.load(src, function (error) { - if (!error && src === audio._src) { - var item = cc.loader.getItem(src); - audio.mount(item.element || item.buffer); - audio.emit('load'); - } - }); - } else { - audio.mount(item.element || item.buffer); - audio.emit('load'); - } - - // current and volume - }; - proto._bindEnded = function (callback) { callback = callback || this._onended; - if (this._audioType === Audio.Type.DOM) { + if (this._src && this._src.loadMode === LoadMode.DOM_AUDIO) { this._element.addEventListener('ended', callback); } else { this._element.onended = callback; @@ -138,35 +92,54 @@ Audio.State = { }; proto._unbindEnded = function () { - if (this._audioType === Audio.Type.DOM) { + if (this._src && this._src.loadMode === LoadMode.DOM_AUDIO) { this._element.removeEventListener('ended', this._onended); } else { this._element.onended = null; } }; - proto.mount = function (elem) { - if (sys.platform === sys.WECHAT_GAME || elem instanceof HTMLElement) { + // proto.mount = function (elem) { + // if (CC_DEBUG) { + // cc.warn('Audio.mount(value) is deprecated. Please use Audio._onLoaded().'); + // } + // }; + + proto._onLoaded = function () { + if (this._src.loadMode === LoadMode.DOM_AUDIO) { this._element = document.createElement('audio'); - this._element.src = elem.src; - this._audioType = Audio.Type.DOM; - } else { - this._element = new WebAudioElement(elem, this); - this._audioType = Audio.Type.WEBAUDIO; + this._element.src = this._src._nativeAsset.src; + } + else { + this._element = new WebAudioElement(this._src._nativeAsset, this); + } + + this.setVolume(this._volume); + this.setLoop(this._loop); + if (this._nextTime !== 0) { + this.setCurrentTime(this._nextTime); + } + if (this._state === Audio.State.PLAYING) { + this.play(); + } + else { + this._state = Audio.State.INITIALZING; } - this._state = Audio.State.INITIALZING; - this._loaded = true; }; proto.play = function () { - if (!this._element) return; - this._bindEnded(); - this._element.play(); - this.emit('play'); + // marked as playing so it will playOnLoad this._state = Audio.State.PLAYING; - if (sys.platform !== sys.WECHAT_GAME && - this._audioType === Audio.Type.DOM && + if (!this._element) { + return; + } + + this._bindEnded(); + this._element.play(); + + if (sys.platform !== sys.WECHAT_GAME && + this._src && this._src.loadMode === LoadMode.DOM_AUDIO && this._element.paused) { touchPlayList.push({ instance: this, offset: 0, audio: this._element }); } @@ -193,7 +166,6 @@ Audio.State = { if (!this._element) return; this._unbindEnded(); this._element.pause(); - this.emit('pause'); this._state = Audio.State.PAUSED; }; @@ -201,7 +173,6 @@ Audio.State = { if (!this._element || this._element.currentTime === 0) return; this._bindEnded(); this._element.play(); - this.emit('resume'); this._state = Audio.State.PLAYING; }; @@ -232,23 +203,34 @@ Audio.State = { }; proto.setLoop = function (loop) { - if (!this._element) return; - this._element.loop = loop; + this._loop = loop; + if (this._element) { + this._element.loop = loop; + } }; proto.getLoop = function () { - return this._element && this._element.loop; + return this._loop; }; proto.setVolume = function (num) { - if (!this._element) return; - this._element.volume = num; + this._volume = num; + if (this._element) { + this._element.volume = num; + } }; proto.getVolume = function () { - return this._element ? this._element.volume : 1; + return this._volume; }; proto.setCurrentTime = function (num) { - if (!this._element) return; + if (this._element) { + this._nextTime = 0; + } + else { + this._nextTime = num; + return; + } + this._unbindEnded(); if (sys.platform !== sys.WECHAT_GAME) { this._bindEnded(function () { @@ -257,7 +239,8 @@ Audio.State = { } try { this._element.currentTime = num; - } catch (err) { + } + catch (err) { var _element = this._element; if (_element.addEventListener) { var func = function () { @@ -278,7 +261,7 @@ Audio.State = { proto.getState = function () { var elem = this._element; - if (Audio.State.PLAYING === this._state && elem.paused) { + if (elem && Audio.State.PLAYING === this._state && elem.paused) { this._state = Audio.State.PAUSED; } return this._state; @@ -287,11 +270,27 @@ Audio.State = { proto.__defineGetter__('src', function () { return this._src; }); - proto.__defineSetter__('src', function (string) { - if (string !== this._src) { - this._isLoading = false; + proto.__defineSetter__('src', function (clip) { + if (clip) { + this._src = clip; + if (clip.loaded) { + this._onLoaded(); + } + else { + var self = this; + clip.once('load', function () { + if (clip === self._src) { + self._onLoaded(); + } + }); + } } - return this._src = string; + else { + this._src = null; + this._element = null; + this._state = Audio.State.INITIALZING; + } + return clip; }); proto.__defineGetter__('paused', function () { diff --git a/cocos2d/audio/CCAudioEngine.js b/cocos2d/audio/CCAudioEngine.js index b8b6a96495..9f5dddd478 100644 --- a/cocos2d/audio/CCAudioEngine.js +++ b/cocos2d/audio/CCAudioEngine.js @@ -26,6 +26,7 @@ ****************************************************************************/ var Audio = require('./CCAudio'); +var AudioClip = require('../core/assets/CCAudioClip'); var instanceId = 0; var id2audio = {}; @@ -37,25 +38,23 @@ var getAudioFromPath = function (path) { if (!list) { list = url2id[path] = []; } - var audio; if (audioEngine._maxAudioInstance <= list.length) { var oldId = list.shift(); var oldAudio = id2audio[oldId]; oldAudio.stop(); } - audio = new Audio(path); + var audio = new Audio(); var callback = function () { - var id = this.instanceId; - delete id2audio[id]; - var index = list.indexOf(id); + delete id2audio[this.id]; + var index = list.indexOf(this.id); cc.js.array.fastRemoveAt(list, index); }; audio.on('ended', callback); audio.on('stop', callback); id2audio[id] = audio; - audio.instanceId = id; + audio.id = id; list.push(id); return audio; @@ -91,33 +90,47 @@ var audioEngine = { * !#en Play audio. * !#zh 播放音频 * @method play - * @param {String} filePath - The path of the audio file without filename extension. + * @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 - * var audioID = cc.audioEngine.play(path, false, 0.5); + * cc.loader.loadRes(url, cc.AudioClip, function (err, clip) { + * var audioID = cc.audioEngine.play(clip, false, 0.5); + * }); */ - play: function (filePath, loop, volume/*, profile*/) { - if (CC_DEBUG && (typeof filePath !== 'string')) { - cc.errorID(8400); - return; + play: function (clip, loop, volume/*, profile*/) { + var path = clip; + var audio; + if (typeof clip === 'string') { + // backward compatibility since 1.10 + cc.warnID(8401, 'cc.audioEngine'); + path = clip; + // load clip + audio = getAudioFromPath(path); + AudioClip._loadByUrl(path, function (err, clip) { + if (clip) { + audio.src = clip; + } + }); + } + else { + if (!clip) { + return; + } + path = clip.nativeUrl; + audio = getAudioFromPath(path); + audio.src = clip; } - var audio = getAudioFromPath(filePath); - var callback = function () { - audio.setLoop(loop || false); - if (typeof volume != 'number' || isNaN(volume)) { - volume = 1; - } - audio.setVolume(volume); - audio.play(); - }; - audio.__callback = callback; - audio.on('load', callback); - audio.preload(); + audio.setLoop(loop || false); + if (typeof volume !== 'number') { + volume = 1; + } + audio.setVolume(volume); + audio.play(); - return audio.instanceId; + return audio.id; }, /** @@ -163,15 +176,9 @@ var audioEngine = { */ setVolume: function (audioID, volume) { var audio = getAudioFromId(audioID); - if (!audio) return; - if (!audio._loaded) { - audio.once('load', function () { - if (audio.setVolume) - audio.setVolume(volume); - }); - } - if (audio.setVolume) + if (audio) { audio.setVolume(volume); + } }, /** @@ -185,9 +192,7 @@ var audioEngine = { */ getVolume: function (audioID) { var audio = getAudioFromId(audioID); - if (!audio || !audio.getVolume) - return 1; - return audio.getVolume(); + return audio ? audio.getVolume() : 1; }, /** @@ -202,17 +207,13 @@ var audioEngine = { */ setCurrentTime: function (audioID, sec) { var audio = getAudioFromId(audioID); - if (!audio) return false; - if (!audio._loaded) { - audio.once('load', function () { - if (audio.setCurrentTime) - audio.setCurrentTime(sec); - }); + if (audio) { + audio.setCurrentTime(sec); return true; } - if (audio.setCurrentTime) - audio.setCurrentTime(sec); - return true; + else { + return false; + } }, /** @@ -226,9 +227,7 @@ var audioEngine = { */ getCurrentTime: function (audioID) { var audio = getAudioFromId(audioID); - if (!audio || !audio.getCurrentTime) - return 0; - return audio.getCurrentTime(); + return audio ? audio.getCurrentTime() : 0; }, /** @@ -242,9 +241,7 @@ var audioEngine = { */ getDuration: function (audioID) { var audio = getAudioFromId(audioID); - if (!audio || !audio.getDuration) - return 0; - return audio.getDuration(); + return audio ? audio.getDuration() : 0; }, /** @@ -258,9 +255,7 @@ var audioEngine = { */ getState: function (audioID) { var audio = getAudioFromId(audioID); - if (!audio || !audio.getState) - return this.AudioState.ERROR; - return audio.getState(); + return audio ? audio.getState() : this.AudioState.ERROR; }, /** @@ -292,10 +287,13 @@ var audioEngine = { */ pause: function (audioID) { var audio = getAudioFromId(audioID); - if (!audio || !audio.pause) + if (audio) { + audio.pause(); + return true; + } + else { return false; - audio.pause(); - return true; + } }, _pauseIDCache: [], @@ -327,12 +325,13 @@ var audioEngine = { */ resume: function (audioID) { var audio = getAudioFromId(audioID); - if (!audio || !audio.resume) - return false; - if (audio.getCurrentTime() === 0) { - audio.play(); - } else { - audio.resume(); + if (audio) { + if (audio.getCurrentTime() === 0) { + audio.play(); + } + else { + audio.resume(); + } } }, @@ -344,12 +343,13 @@ var audioEngine = { * cc.audioEngine.resumeAll(); */ resumeAll: function () { - while (this._pauseIDCache.length > 0) { - var id = this._pauseIDCache.pop(); + for (var i = 0; i < this._pauseIDCache.length; ++i) { + var id = this._pauseIDCache[i]; var audio = getAudioFromId(id); - if (audio && audio.resume) + if (audio) audio.resume(); } + this._pauseIDCache.length = 0; }, /** @@ -362,11 +362,13 @@ var audioEngine = { */ stop: function (audioID) { var audio = getAudioFromId(audioID); - if (!audio || !audio.stop) + if (audio) { + audio.stop(); + return true; + } + else { return false; - audio.off('load', audio.__callback); - audio.stop(); - return true; + } }, /** @@ -379,9 +381,8 @@ var audioEngine = { stopAll: function () { for (var id in id2audio) { var audio = id2audio[id]; - if (audio && audio.stop) { + if (audio) { audio.stop(); - audio.off('load', audio.__callback); } } }, @@ -414,11 +415,24 @@ var audioEngine = { * !#en Unload the preloaded audio from internal buffer. * !#zh 卸载预加载的音频。 * @method uncache - * @param {String} filePath + * @param {AudioClip} clip * @example * cc.audioEngine.uncache(filePath); */ - uncache: function (filePath) { + uncache: function (clip) { + var filePath = clip; + if (typeof clip === 'string') { + // backward compatibility since 1.10 + cc.warnID(8401, 'cc.audioEngine'); + filePath = clip; + } + else { + if (!clip) { + return; + } + filePath = clip.nativeUrl; + } + var list = url2id[filePath]; if (!list) return; while (list.length > 0) { @@ -467,8 +481,13 @@ var audioEngine = { * @param {Function} [callback] - The callback of an audio. * @example * cc.audioEngine.preload(path); + * @deprecated `cc.audioEngine.preload` is deprecated, use `cc.loader.loadRes(url, cc.AudioClip)` instead please. */ preload: function (filePath, callback) { + if (CC_DEBUG) { + cc.warn('`cc.audioEngine.preload` is deprecated, use `cc.loader.loadRes(url, cc.AudioClip)` instead please.'); + } + cc.loader.load(filePath, callback && function (error) { if (!error) { callback(); @@ -532,16 +551,18 @@ var audioEngine = { * !#en Play background music * !#zh 播放背景音乐 * @method playMusic - * @param {String} filePath - The path of the audio file without filename extension. + * @param {AudioClip} clip - The audio clip to play. * @param {Boolean} loop - Whether the music loop or not. * @return {Number} audioId * @example - * var audioID = cc.audioEngine.playMusic(path, false); + * cc.loader.loadRes(url, cc.AudioClip, function (err, clip) { + * var audioID = cc.audioEngine.playMusic(clip, false); + * }); */ - playMusic: function (filePath, loop) { + playMusic: function (clip, loop) { var music = this._music; this.stop(music.id); - music.id = this.play(filePath, loop, music.volume); + music.id = this.play(clip, loop, music.volume); music.loop = loop; return music.id; }, @@ -624,15 +645,16 @@ var audioEngine = { * !#en Play effect audio. * !#zh 播放音效 * @method playEffect - * @param {String} filePath - The path of the audio file without filename extension. + * @param {AudioClip} clip - The audio clip to play. * @param {Boolean} loop - Whether the music loop or not. * @return {Number} audioId * @example - * var audioID = cc.audioEngine.playEffect(path, false); + * cc.loader.loadRes(url, cc.AudioClip, function (err, clip) { + * var audioID = cc.audioEngine.playEffect(clip, false); + * }); */ - playEffect: function (filePath, loop) { - var effect = this._effect; - return this.play(filePath, loop || false, effect.volume); + playEffect: function (clip, loop) { + return this.play(clip, loop || false, this._effect.volume); }, /** @@ -644,8 +666,8 @@ var audioEngine = { * cc.audioEngine.setEffectsVolume(0.5); */ setEffectsVolume: function (volume) { + var musicId = this._music.id; this._effect.volume = volume; - var id2audio = audioEngine._id2audio; for (var id in id2audio) { if (id === musicId) continue; audioEngine.setVolume(id, volume); @@ -686,7 +708,6 @@ var audioEngine = { pauseAllEffects: function () { var musicId = this._music.id; var effect = this._effect; - var id2audio = this._id2audio; effect.pauseCache.length = 0; for (var id in id2audio) { @@ -721,11 +742,10 @@ var audioEngine = { */ resumeAllEffects: function () { var pauseIDCache = this._effect.pauseCache; - var id2audio = this._id2audio; - while (pauseIDCache.length > 0) { - var id = pauseIDCache.pop(); + for (var i = 0; i < pauseIDCache.length; ++i) { + var id = pauseIDCache[i]; var audio = id2audio[id]; - if (audio && audio.resume) + if (audio) audio.resume(); } }, @@ -751,7 +771,6 @@ var audioEngine = { */ stopAllEffects: function () { var musicId = this._music.id; - var id2audio = this._id2audio; for (var id in id2audio) { if (id === musicId) continue; var audio = id2audio[id]; diff --git a/cocos2d/audio/deprecated.js b/cocos2d/audio/deprecated.js index 81f360a479..e2b2c44a9e 100644 --- a/cocos2d/audio/deprecated.js +++ b/cocos2d/audio/deprecated.js @@ -37,11 +37,6 @@ exports.removed = function (audioEngine) { exports.deprecated = function (audioEngine) { var musicId = -1; - var musicPath = 1; - var musicLoop = 1; - var musicVolume = 1; - var effectsVolume = 1; - var pauseIDCache = []; js.get(audioEngine, 'rewindMusic', function () { cc.warnID(1400, 'audioEngine.rewindMusic', 'audioEngine.setCurrentTime'); return function () { diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 6ac523b94b..1ce1616ed4 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -1262,7 +1262,7 @@ var Node = cc.Class({ * !#zh 删除之前与同类型,回调,目标或 useCapture 注册的回调。 * @method off * @param {String} type - A string representing the event type being removed. - * @param {Function} callback - The callback to remove. + * @param {Function} [callback] - The callback to remove. * @param {Object} [target] - The target (this object) to invoke the callback, if it's not given, only callback without target will be removed * @param {Boolean} [useCapture=false] - Specifies whether the callback being removed was registered as a capturing callback or not. * If not specified, useCapture defaults to false. If a callback was registered twice, diff --git a/cocos2d/core/assets/CCAsset.js b/cocos2d/core/assets/CCAsset.js index 0481fcfe62..8a08a95b71 100644 --- a/cocos2d/core/assets/CCAsset.js +++ b/cocos2d/core/assets/CCAsset.js @@ -24,7 +24,7 @@ THE SOFTWARE. ****************************************************************************/ -var RawAsset = require('./CCRawAsset'); +const RawAsset = require('./CCRawAsset'); /** * !#en @@ -50,6 +50,19 @@ var RawAsset = require('./CCRawAsset'); cc.Asset = cc.Class({ name: 'cc.Asset', extends: RawAsset, + ctor () { + /** + * !#en + * Whether the asset is loaded or not + * !#zh + * 该资源是否已经成功加载 + * + * @property loaded + * @type {Boolean} + */ + this.loaded = true; + }, + properties: { /** * !#en @@ -63,9 +76,14 @@ cc.Asset = cc.Class({ nativeUrl: { get: function () { if (this._native) { + var name = this._native; + if (name.charCodeAt(0) === 47) { // '/' + // remove library tag + // not imported in library, just created on-the-fly + return name.slice(1); + } if (cc.AssetLibrary) { var base = cc.AssetLibrary.getLibUrlNoExt(this._uuid); - var name = this._native; if (name.charCodeAt(0) === 46) { // '.' // imported in dir where json exist return base + name; @@ -92,11 +110,6 @@ cc.Asset = cc.Class({ */ _native: "", - // __nativeAsset: { - // default: null, - // serializable: false - // }, - /** * The underlying native asset of this asset if one is available. * This property can be used to access additional details or functionality releated to the asset. @@ -106,18 +119,8 @@ cc.Asset = cc.Class({ * @private */ _nativeAsset: { - get () { - if (CC_EDITOR) { - cc.errorID('0100', 'getter of ' + JS.getClassName(this) + '._nativeAsset'); - } - // return this.__nativeAsset; - }, - set (obj) { - if (CC_EDITOR) { - cc.errorID('0100', 'setter of ' + JS.getClassName(this) + '._nativeAsset'); - } - // this.__nativeAsset = obj; - } + get () {}, + set (obj) {} }, }, @@ -156,6 +159,20 @@ cc.Asset = cc.Class({ preventPreloadNativeObject: false }, + /** + * Returns the asset's url. + * + * The `Asset` object overrides the `toString()` method of the `Object` object. + * For `Asset` objects, the toString() method returns a string representation of the object. + * JavaScript calls the toString() method automatically when an asset is to be represented as a text value or when a texture is referred to in a string concatenation. + * + * @method toString + * @return {String} + */ + toString () { + return this.nativeUrl; + }, + /** * 应 AssetDB 要求提供这个方法 * @@ -188,10 +205,16 @@ cc.Asset = cc.Class({ * * @method _setRawAsset * @param {String} filename + * @param {Boolean} [inLibrary=true] * @private */ - _setRawAsset: function (filename) { - this._native = filename || undefined; + _setRawAsset: function (filename, inLibrary) { + if (inLibrary !== false) { + this._native = filename || undefined; + } + else { + this._native = '/' + filename; // simply use '/' to tag location where is not in the library + } } }); diff --git a/cocos2d/core/assets/CCAudioClip.js b/cocos2d/core/assets/CCAudioClip.js index f5f23332bd..73e30ceddc 100644 --- a/cocos2d/core/assets/CCAudioClip.js +++ b/cocos2d/core/assets/CCAudioClip.js @@ -24,17 +24,93 @@ THE SOFTWARE. ****************************************************************************/ +const Asset = require('./CCAsset'); +const EventTarget = require('../event/event-target'); + +var LoadMode = cc.Enum({ + WEB_AUDIO: 0, + DOM_AUDIO: 1, +}); + /** * !#en Class for audio data handling. * !#zh 音频资源类。 * @class AudioClip - * @extends RawAsset - * + * @extends Asset + * @uses EventTarget */ var AudioClip = cc.Class({ name: 'cc.AudioClip', - extends: cc.RawAsset, + extends: Asset, + mixins: [EventTarget], + + ctor () { + this.loaded = false; + + // the web audio buffer or