From 59c787ba0a625c470ee71f1030daf877506625ff Mon Sep 17 00:00:00 2001 From: VisualSJ Date: Mon, 16 Jan 2017 17:07:30 +0800 Subject: [PATCH 1/2] Fixed a bug about ios setVolume --- cocos2d/audio/CCAudio.js | 16 ++++++++++------ cocos2d/core/components/CCAudioSource.js | 5 +++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/cocos2d/audio/CCAudio.js b/cocos2d/audio/CCAudio.js index f3dfe05889..ecf8565f3b 100644 --- a/cocos2d/audio/CCAudio.js +++ b/cocos2d/audio/CCAudio.js @@ -269,6 +269,12 @@ var WebAudioElement = function (buffer, audio) { this.playedLength = 0; this._currextTimer = null; + + this._endCallback = function () { + if (this.onended) { + this.onended(this); + } + }.bind(this); }; (function (proto) { @@ -313,11 +319,7 @@ var WebAudioElement = function (buffer, audio) { this._currentSource = audio; - audio.onended = function () { - if (this.onended) { - this.onended(this); - } - }.bind(this); + audio.onended = this._endCallback; // If the current audio context time stamp is 0 // There may be a need to touch events before you can actually start playing audio @@ -374,10 +376,12 @@ 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 (cc.sys.os === cc.sys.OS_IOS && !this.paused) { + if (cc.sys.os === cc.sys.OS_IOS && !this.paused && this._currentSource) { // IOS must be stop webAudio + this._currentSource.onended = null; this.pause(); this.play(); + this._currentSource.onended = this._endCallback; } return num; }); diff --git a/cocos2d/core/components/CCAudioSource.js b/cocos2d/core/components/CCAudioSource.js index 7133a8ef34..ce0374a8f3 100644 --- a/cocos2d/core/components/CCAudioSource.js +++ b/cocos2d/core/components/CCAudioSource.js @@ -114,6 +114,11 @@ var AudioSource = cc.Class({ return this._volume; }, set: function (value) { + if (value < 0) { + value = 0; + } else if (value > 1) { + value = 1; + } this._volume = value; var audio = this.audio; if (audio && !this._mute) { From b2ef365eecb2297a8a4d720bbe1fcaa7b93242e0 Mon Sep 17 00:00:00 2001 From: VisualSJ Date: Mon, 16 Jan 2017 17:30:03 +0800 Subject: [PATCH 2/2] review code --- cocos2d/audio/CCAudio.js | 1 - cocos2d/core/components/CCAudioSource.js | 6 +----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/cocos2d/audio/CCAudio.js b/cocos2d/audio/CCAudio.js index ecf8565f3b..e86787a786 100644 --- a/cocos2d/audio/CCAudio.js +++ b/cocos2d/audio/CCAudio.js @@ -381,7 +381,6 @@ var WebAudioElement = function (buffer, audio) { this._currentSource.onended = null; this.pause(); this.play(); - this._currentSource.onended = this._endCallback; } return num; }); diff --git a/cocos2d/core/components/CCAudioSource.js b/cocos2d/core/components/CCAudioSource.js index ce0374a8f3..91d2cd5a44 100644 --- a/cocos2d/core/components/CCAudioSource.js +++ b/cocos2d/core/components/CCAudioSource.js @@ -114,11 +114,7 @@ var AudioSource = cc.Class({ return this._volume; }, set: function (value) { - if (value < 0) { - value = 0; - } else if (value > 1) { - value = 1; - } + value = cc.clamp01(value); this._volume = value; var audio = this.audio; if (audio && !this._mute) {