From fd89b551f11ff899f2fc433019099d139630ea02 Mon Sep 17 00:00:00 2001 From: VisualSJ Date: Wed, 5 Jul 2017 16:29:37 +0800 Subject: [PATCH] #5920 Audio preload error --- cocos2d/audio/CCAudio.js | 8 ++++++++ cocos2d/core/components/CCAudioSource.js | 14 +++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/cocos2d/audio/CCAudio.js b/cocos2d/audio/CCAudio.js index e86787a786..247d9226f3 100644 --- a/cocos2d/audio/CCAudio.js +++ b/cocos2d/audio/CCAudio.js @@ -127,6 +127,14 @@ Audio.State = { } }; + proto.unmount = function () { + this._src = ''; + this._audioType = Audio.Type.UNKNOWN; + this._element = null; + this._state = Audio.State.INITIALZING; + this._loaded = false; + }; + proto.mount = function (elem) { if (elem instanceof HTMLElement) { this._element = document.createElement('audio'); diff --git a/cocos2d/core/components/CCAudioSource.js b/cocos2d/core/components/CCAudioSource.js index 91d2cd5a44..94ea539a9c 100644 --- a/cocos2d/core/components/CCAudioSource.js +++ b/cocos2d/core/components/CCAudioSource.js @@ -41,7 +41,11 @@ var AudioSource = cc.Class({ }, ctor: function () { - this.audio = new cc.Audio(this._clip); + this.audio = new cc.Audio(); + }, + + onLoad: function () { + this.clip = this._clip; }, properties: { @@ -93,8 +97,12 @@ var AudioSource = cc.Class({ this._clip = value; this.audio.stop(); this.audio.src = this._clip; - if (this.audio.preload) { - this.audio.preload(); + if (value) { + if (this.audio.preload) { + this.audio.preload(); + } + } else { + this.audio.unmount(); } }, url: cc.AudioClip,