From f378941da44240b1f79e57ba49e6cc98d01f0628 Mon Sep 17 00:00:00 2001 From: Knox Date: Tue, 19 Jun 2018 18:48:31 +0800 Subject: [PATCH] fix failed to trigger the audio ended event (#2873) --- cocos2d/audio/CCAudio.js | 16 +++++++++------- cocos2d/audio/CCAudioEngine.js | 4 +++- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/cocos2d/audio/CCAudio.js b/cocos2d/audio/CCAudio.js index bc5bbef33b..37cee0ac31 100644 --- a/cocos2d/audio/CCAudio.js +++ b/cocos2d/audio/CCAudio.js @@ -84,18 +84,20 @@ Audio.State = { proto._bindEnded = function (callback) { callback = callback || this._onended; - if (this._src && this._src.loadMode === LoadMode.DOM_AUDIO) { - this._element.addEventListener('ended', callback); + let elem = this._element; + if (this._src && (elem instanceof HTMLAudioElement)) { + elem.addEventListener('ended', callback); } else { - this._element.onended = callback; + elem.onended = callback; } }; proto._unbindEnded = function () { - if (this._src && this._src.loadMode === LoadMode.DOM_AUDIO) { - this._element.removeEventListener('ended', this._onended); + let elem = this._element; + if (this._src && (elem instanceof HTMLAudioElement)) { + elem.removeEventListener('ended', this._onended); } else { - this._element.onended = null; + elem.onended = null; } }; @@ -107,7 +109,7 @@ Audio.State = { proto._onLoaded = function () { let elem = this._src._nativeAsset; - if (elem instanceof HTMLElement) { + if (elem instanceof HTMLAudioElement) { this._element = document.createElement('audio'); this._element.src = elem.src; } diff --git a/cocos2d/audio/CCAudioEngine.js b/cocos2d/audio/CCAudioEngine.js index cffe06ddf6..1f9ddbd7b6 100644 --- a/cocos2d/audio/CCAudioEngine.js +++ b/cocos2d/audio/CCAudioEngine.js @@ -41,13 +41,15 @@ var getAudioFromPath = function (path) { } if (audioEngine._maxAudioInstance <= list.length) { var oldId = list.shift(); - var oldAudio = id2audio[oldId]; + var oldAudio = getAudioFromId(oldId); oldAudio.stop(); oldAudio.destroy(); } var audio = new Audio(); var callback = function () { + var oldAudio = getAudioFromId(this.id); + oldAudio.destroy(); delete id2audio[this.id]; var index = list.indexOf(this.id); cc.js.array.fastRemoveAt(list, index);