From 8c0a7a013c6b6320161f6028f306f463e49fb224 Mon Sep 17 00:00:00 2001 From: VisualSJ Date: Tue, 23 Jan 2018 11:49:54 +0800 Subject: [PATCH 1/2] The problem that the audio in load cannot play --- cocos2d/audio/CCAudio.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos2d/audio/CCAudio.js b/cocos2d/audio/CCAudio.js index d7e1b4e892..86de73b2a2 100644 --- a/cocos2d/audio/CCAudio.js +++ b/cocos2d/audio/CCAudio.js @@ -104,7 +104,7 @@ Audio.State = { } // If the resource does not exist - if (!item) { + if (!item || !item.complete) { return cc.loader.load(src, function (error) { if (!error) { var item = cc.loader.getItem(src); @@ -112,7 +112,7 @@ Audio.State = { audio.emit('load'); } }); - } else if (item.complete) { + } else { audio.mount(item.element || item.buffer); audio.emit('load'); } From 781efe4ab682e8b11121e33d99da8fe34e76f2fb Mon Sep 17 00:00:00 2001 From: VisualSJ Date: Tue, 23 Jan 2018 15:00:51 +0800 Subject: [PATCH 2/2] Use setTargetAtTime --- cocos2d/audio/CCAudio.js | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/cocos2d/audio/CCAudio.js b/cocos2d/audio/CCAudio.js index 86de73b2a2..62b59ed33f 100644 --- a/cocos2d/audio/CCAudio.js +++ b/cocos2d/audio/CCAudio.js @@ -282,9 +282,17 @@ var WebAudioElement = function (buffer, audio) { this._audio = audio; this._context = sys.__audioSupport.context; this._buffer = buffer; - this._volume = this._context['createGain'](); - this._volume['gain'].value = 1; - this._volume['connect'](this._context['destination']); + + this._gainObj = this._context['createGain'](); + this._volume = 1; + // https://www.chromestatus.com/features/5287995770929152 + if (this._gainObj['gain'].setTargetAtTime) { + this._gainObj['gain'].setTargetAtTime(this._volume, this._context.currentTime, 0.01); + } else { + this._gainObj['gain'].value = 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; @@ -313,7 +321,7 @@ var WebAudioElement = function (buffer, audio) { var audio = this._context["createBufferSource"](); audio.buffer = this._buffer; - audio["connect"](this._volume); + audio["connect"](this._gainObj); audio.loop = this._loop; this._startTime = this._context.currentTime; @@ -398,9 +406,16 @@ var WebAudioElement = function (buffer, audio) { return this._loop = bool; }); - proto.__defineGetter__('volume', function () { return this._volume['gain'].value; }); + proto.__defineGetter__('volume', function () { + return this._volume; + }); proto.__defineSetter__('volume', function (num) { - this._volume['gain'].value = num; + this._volume = num; + if (this._gainObj['gain'].setTargetAtTime) { + this._gainObj['gain'].setTargetAtTime(this._volume, this._context.currentTime, 0.01); + } else { + this._volume['gain'].value = num; + } if (sys.os === sys.OS_IOS && !this.paused && this._currentSource) { // IOS must be stop webAudio this._currentSource.onended = null;