diff --git a/cocos2d/audio/CCAudio.js b/cocos2d/audio/CCAudio.js index 04b31d16b1..39a85c8719 100644 --- a/cocos2d/audio/CCAudio.js +++ b/cocos2d/audio/CCAudio.js @@ -34,13 +34,17 @@ var touchPlayList = [ var Audio = function (src) { EventTarget.call(this); - this.src = src; + this._src = src; this._audioType = Audio.Type.UNKNOWN; this._element = null; this._eventList = {}; - this._state = Audio.State.ERROR; + this._state = Audio.State.INITIALZING; this._loaded = false; + + this._onended = function () { + this.emit('ended'); + }.bind(this); }; cc.js.extend(Audio, EventTarget); @@ -62,7 +66,7 @@ Audio.State = { (function (proto) { proto.preload = function () { - var src = this.src, + var src = this._src, audio = this; var item = cc.loader.getItem(src); @@ -75,11 +79,29 @@ Audio.State = { audio.emit('load'); } }); - } - else if (item.complete) { + } else if (item.complete) { 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) { + this._element.addEventListener('ended', callback); + } else { + this._element.onended = callback; + } + }; + + proto._unbindEnded = function () { + if (this._audioType === Audio.Type.DOM) { + this._element.removeEventListener('ended', this._onended); + } else { + this._element.onended = null; + } }; proto.mount = function (elem) { @@ -87,16 +109,11 @@ Audio.State = { this._element = document.createElement('audio'); this._element.src = elem.src; this._audioType = Audio.Type.DOM; - elem.addEventListener('ended', function () { - this.emit('ended', this); - }, this); } else { this._element = new WebAudioElement(elem); this._audioType = Audio.Type.WEBAUDIO; - elem.onended = function () { - this.emit('ended', this); - }; } + this._bindEnded(); this._state = Audio.State.INITIALZING; this._loaded = true; }; @@ -126,6 +143,7 @@ Audio.State = { proto.pause = function () { if (!this._element) return; + this._unbindEnded(); this._element.pause(); this.emit('pause'); this._state = Audio.State.PAUSED; @@ -133,18 +151,20 @@ Audio.State = { proto.resume = function () { if (!this._element || this._element.currentTime === 0) return; + this._bindEnded(); this._element.play(); - this.emit('play'); + this.emit('resume'); this._state = Audio.State.PLAYING; }; proto.stop = function () { if (!this._element) return; - this._element.pause(); try { this._element.currentTime = 0; } catch (error) {} - this.emit('pause'); + this._element.pause(); + this.emit('ended'); + this.emit('stop'); this._state = Audio.State.PAUSED; }; @@ -166,6 +186,10 @@ Audio.State = { proto.setCurrentTime = function (num) { if (!this._element) return; + this._unbindEnded(); + this._bindEnded(function () { + this._bindEnded(); + }.bind(this)); this._element.currentTime = num; }; proto.getCurrentTime = function () { @@ -184,6 +208,13 @@ Audio.State = { return this._state; }; + proto.__defineGetter__('src', function () { + return this._src; + }); + proto.__defineSetter__('src', function (string) { + return this._src = string; + }); + // setFinishCallback })(Audio.prototype); @@ -270,6 +301,7 @@ var WebAudioElement = function (buffer) { }; proto.pause = function () { + 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 @@ -326,6 +358,10 @@ var WebAudioElement = function (buffer) { return num; }); + proto.__defineGetter__('duration', function () { + return this._buffer.duration; + }); + })(WebAudioElement.prototype); -module.exports = cc.Audio = Audio; \ No newline at end of file +module.exports = cc.Audio = Audio; diff --git a/cocos2d/audio/CCAudioEngine.js b/cocos2d/audio/CCAudioEngine.js index 03e218db20..30e14af434 100644 --- a/cocos2d/audio/CCAudioEngine.js +++ b/cocos2d/audio/CCAudioEngine.js @@ -44,10 +44,20 @@ var getAudioFromPath = function (path) { var oldId = list.shift(); audio = id2audio[id] = id2audio[oldId]; delete id2audio[oldId]; + var index = list.indexOf(oldId); + index !== -1 && list.splice(index, 1); } audio.instanceId = id; list.push(id); + // + audio.on('ended', function () { + var id = this.instanceId; + delete id2audio[id]; + var index = list.indexOf(id); + index !== -1 && list.splice(index, 1); + }); + return audio; }; @@ -236,7 +246,7 @@ var audioEngine = { getState: function (audioID) { var audio = getAudioFromId(audioID); if (!audio || !audio.getState) - return 0; + return this.AudioState.ERROR; return audio.getState(); }, @@ -275,8 +285,8 @@ var audioEngine = { audio.pause(); return true; }, - _pauseIDCache: [], + _pauseIDCache: [], /** * !#en Pause all playing audio * !#zh 暂停现在正在播放的所有音频。 @@ -346,6 +356,7 @@ var audioEngine = { if (!audio || !audio.stop) return false; audio.stop(); + audio.emit('ended'); return true; }, @@ -362,6 +373,7 @@ var audioEngine = { var audio = id2audio[id]; if (audio && audio.stop) { audio.stop(); + audio.emit('ended'); } } }, @@ -466,6 +478,31 @@ var audioEngine = { // Because webAudio takes up too much memory,So allow users to manually choose setMaxWebAudioSize: function (kb) { this._maxWebAudioSize = kb * 1024; + }, + + _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; } }; diff --git a/cocos2d/core/CCGame.js b/cocos2d/core/CCGame.js index 197c58d99a..dcd501440d 100644 --- a/cocos2d/core/CCGame.js +++ b/cocos2d/core/CCGame.js @@ -254,7 +254,7 @@ var game = { this._paused = true; // Pause audio engine if (audioEngine) { - audioEngine.pauseAll(); + audioEngine._break(); } // Pause main loop if (this._intervalId) @@ -273,7 +273,7 @@ var game = { this._paused = false; // Resume audio engine if (audioEngine) { - audioEngine.resumeAll(); + audioEngine._restore(); } // Resume main loop this._runMainLoop(); diff --git a/cocos2d/core/components/CCAudioSource.js b/cocos2d/core/components/CCAudioSource.js index 5a8bb25609..7814f99385 100644 --- a/cocos2d/core/components/CCAudioSource.js +++ b/cocos2d/core/components/CCAudioSource.js @@ -41,7 +41,7 @@ var AudioSource = cc.Class({ }, ctor: function () { - this.audio = new cc.Audio(); + this.audio = new cc.Audio(this._clip); }, properties: { @@ -87,6 +87,7 @@ var AudioSource = cc.Class({ }, set: function (value) { this._clip = value; + this.audio.stop(); this.audio.src = this._clip; if (this.audio.preload) { this.audio.preload(); @@ -110,8 +111,14 @@ var AudioSource = cc.Class({ }, set: function (value) { this._volume = value; - if (this.audio && !this._mute) { - this.audio.setVolume(value); + var audio = this.audio; + if (audio && !this._mute) { + audio.setVolume(value); + if (!audio._loaded) { + audio.on('load', function () { + audio.setVolume(value); + }); + } } return value; }, @@ -173,6 +180,11 @@ var AudioSource = cc.Class({ default: false, tooltip: 'i18n:COMPONENT.audio.play_on_load', animatable: false + }, + + preload: { + default: false, + animatable: false } }, @@ -180,6 +192,10 @@ var AudioSource = cc.Class({ if ( this.playOnLoad ) { this.play(); } + if ( this.preload ) { + this.audio.src = this._clip; + this.audio.preload(); + } }, onDisable: function () { @@ -201,10 +217,19 @@ var AudioSource = cc.Class({ var volume = this._mute ? 0 : this._volume; var audio = this.audio; + var loop = this._loop; + + if (audio._loaded) { + audio.stop(); + audio.setCurrentTime(0); + audio.play(); + return; + } + audio.src = this._clip; - audio.setLoop(this._loop); - audio.setVolume(volume); audio.once('load', function () { + audio.setLoop(loop); + audio.setVolume(volume); audio.play(); }); audio.preload(); @@ -252,6 +277,51 @@ var AudioSource = cc.Class({ if (this.audio) { this.audio.setCurrentTime(0); } + }, + + /** + * !#en Get current time + * !#zh 获取当前的播放时间 + * @method rewind + */ + getCurrentTime: function () { + var time = 0; + if (this.audio) { + time = this.audio.getCurrentTime(); + } + return time; + }, + + /** + * !#en Set current time + * !#zh 设置当前的播放时间 + * @method rewind + */ + setCurrentTime: function (time) { + var audio = this.audio; + if (!audio) return time; + + if (!audio._loaded) { + audio.once('load', function () { + audio.setCurrentTime(time); + }); + return time; + } + audio.setCurrentTime(time); + return time; + }, + + /** + * !#en Get audio duration + * !#zh 获取当前音频的长度 + * @method rewind + */ + getDuration: function () { + var time = 0; + if (this.audio) { + time = this.audio.getDuration(); + } + return time; } }); diff --git a/jsb/jsb-audio.js b/jsb/jsb-audio.js index e79c41cb26..4bd3730eba 100644 --- a/jsb/jsb-audio.js +++ b/jsb/jsb-audio.js @@ -46,10 +46,12 @@ cc.Audio.Type = { // Using the new audioEngine cc.audioEngine = audioEngine; audioEngine.play = audioEngine.play2d; + audioEngine.setMaxWebAudioSize = function () {}; proto.State = audioEngine.AudioState; proto.play = function () { + audioEngine.stop(this.id); this.id = audioEngine.play2d(this.src, this.loop, this.volume); }; diff --git a/jsb/jsb-etc.js b/jsb/jsb-etc.js index 856f889a7e..e81097ae13 100644 --- a/jsb/jsb-etc.js +++ b/jsb/jsb-etc.js @@ -238,7 +238,3 @@ cc.formatStr = cc.js.formatStr; if (cc.Image && cc.Image.setPNGPremultipliedAlphaEnabled) { cc.Image.setPNGPremultipliedAlphaEnabled(false); } - -if (cc.audioEngine) { - cc.audioEngine.setMaxWebAudioSize = function () {}; -}