From e638e9b9d71b70049bed2a17a4a5aa8b3257cc03 Mon Sep 17 00:00:00 2001 From: VisualSJ Date: Mon, 17 Oct 2016 15:07:33 +0800 Subject: [PATCH 1/4] Fixed a bug about audio volume --- cocos2d/audio/CCAudio.js | 10 +++++++++- cocos2d/core/components/CCAudioSource.js | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/cocos2d/audio/CCAudio.js b/cocos2d/audio/CCAudio.js index ce8a383cef..4821b006bf 100644 --- a/cocos2d/audio/CCAudio.js +++ b/cocos2d/audio/CCAudio.js @@ -346,7 +346,15 @@ var WebAudioElement = function (buffer, audio) { }); proto.__defineGetter__('volume', function () { return this._volume['gain'].value; }); - proto.__defineSetter__('volume', function (num) { return this._volume['gain'].value = num; }); + proto.__defineSetter__('volume', function (num) { + this._volume['gain'].value = num; + if (!this.paused) { + // IOS must be stop webAudio + this.pause(); + this.play(); + } + return num; + }); proto.__defineGetter__('currentTime', function () { if (this.paused) { diff --git a/cocos2d/core/components/CCAudioSource.js b/cocos2d/core/components/CCAudioSource.js index 48cf2a038d..588f008d3f 100644 --- a/cocos2d/core/components/CCAudioSource.js +++ b/cocos2d/core/components/CCAudioSource.js @@ -188,6 +188,20 @@ var AudioSource = cc.Class({ } }, + _breakCallback: function () { + if (!this.audio || this.audio.paused) return; + this.audio.pause(); + this._breakFlag = true; + }, + + _restoreCallback: function () { + if (!this.audio) return; + if (this._breakFlag) { + this.audio.resume(); + } + this._breakFlag = false; + }, + onEnable: function () { if ( this.playOnLoad ) { this.play(); @@ -196,10 +210,15 @@ var AudioSource = cc.Class({ this.audio.src = this._clip; this.audio.preload(); } + this._breakFlag = false; + cc.game.on(cc.game.EVENT_HIDE, this._breakCallback, this); + cc.game.on(cc.game.EVENT_SHOW, this._restoreCallback, this); }, onDisable: function () { this.stop(); + cc.game.off(cc.game.EVENT_HIDE, this._breakCallback, this); + cc.game.off(cc.game.EVENT_SHOW, this._restoreCallback, this); }, onDestroy: function () { From d808d98b66b582da7a9e4754306c822b65927b86 Mon Sep 17 00:00:00 2001 From: VisualSJ Date: Mon, 17 Oct 2016 16:46:45 +0800 Subject: [PATCH 2/4] review move _breakFlag --- cocos2d/core/components/CCAudioSource.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos2d/core/components/CCAudioSource.js b/cocos2d/core/components/CCAudioSource.js index 588f008d3f..e09aa75b57 100644 --- a/cocos2d/core/components/CCAudioSource.js +++ b/cocos2d/core/components/CCAudioSource.js @@ -52,6 +52,7 @@ var AudioSource = cc.Class({ _volume: 1, _mute: false, _loop: false, + _breakFlag: false, /** * !#en @@ -210,7 +211,6 @@ var AudioSource = cc.Class({ this.audio.src = this._clip; this.audio.preload(); } - this._breakFlag = false; cc.game.on(cc.game.EVENT_HIDE, this._breakCallback, this); cc.game.on(cc.game.EVENT_SHOW, this._restoreCallback, this); }, From 116a78e78a2b3c804f3b0d4e46a62cdcdfe43b0c Mon Sep 17 00:00:00 2001 From: VisualSJ Date: Mon, 17 Oct 2016 17:05:19 +0800 Subject: [PATCH 3/4] Fixed a bug about audio volume --- cocos2d/core/components/CCAudioSource.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/components/CCAudioSource.js b/cocos2d/core/components/CCAudioSource.js index e09aa75b57..38cfd431bf 100644 --- a/cocos2d/core/components/CCAudioSource.js +++ b/cocos2d/core/components/CCAudioSource.js @@ -52,7 +52,10 @@ var AudioSource = cc.Class({ _volume: 1, _mute: false, _loop: false, - _breakFlag: false, + _breakFlag: { + default: false, + serializable: false + }, /** * !#en From dede874f089b9358f595acb5624e129eace71ecc Mon Sep 17 00:00:00 2001 From: VisualSJ Date: Mon, 17 Oct 2016 18:14:37 +0800 Subject: [PATCH 4/4] Panda review code --- cocos2d/audio/CCAudio.js | 2 +- cocos2d/core/components/CCAudioSource.js | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cocos2d/audio/CCAudio.js b/cocos2d/audio/CCAudio.js index 4821b006bf..d12299d5db 100644 --- a/cocos2d/audio/CCAudio.js +++ b/cocos2d/audio/CCAudio.js @@ -348,7 +348,7 @@ var WebAudioElement = function (buffer, audio) { proto.__defineGetter__('volume', function () { return this._volume['gain'].value; }); proto.__defineSetter__('volume', function (num) { this._volume['gain'].value = num; - if (!this.paused) { + if (cc.sys.os === cc.sys.OS_IOS && !this.paused) { // IOS must be stop webAudio this.pause(); this.play(); diff --git a/cocos2d/core/components/CCAudioSource.js b/cocos2d/core/components/CCAudioSource.js index 38cfd431bf..8b05eb44c2 100644 --- a/cocos2d/core/components/CCAudioSource.js +++ b/cocos2d/core/components/CCAudioSource.js @@ -52,7 +52,7 @@ var AudioSource = cc.Class({ _volume: 1, _mute: false, _loop: false, - _breakFlag: { + _pausedFlag: { default: false, serializable: false }, @@ -192,18 +192,18 @@ var AudioSource = cc.Class({ } }, - _breakCallback: function () { + _pausedCallback: function () { if (!this.audio || this.audio.paused) return; this.audio.pause(); - this._breakFlag = true; + this._pausedFlag = true; }, _restoreCallback: function () { if (!this.audio) return; - if (this._breakFlag) { + if (this._pausedFlag) { this.audio.resume(); } - this._breakFlag = false; + this._pausedFlag = false; }, onEnable: function () { @@ -214,13 +214,13 @@ var AudioSource = cc.Class({ this.audio.src = this._clip; this.audio.preload(); } - cc.game.on(cc.game.EVENT_HIDE, this._breakCallback, this); + cc.game.on(cc.game.EVENT_HIDE, this._pausedCallback, this); cc.game.on(cc.game.EVENT_SHOW, this._restoreCallback, this); }, onDisable: function () { this.stop(); - cc.game.off(cc.game.EVENT_HIDE, this._breakCallback, this); + cc.game.off(cc.game.EVENT_HIDE, this._pausedCallback, this); cc.game.off(cc.game.EVENT_SHOW, this._restoreCallback, this); },