diff --git a/cocos2d/audio/CCAudio.js b/cocos2d/audio/CCAudio.js index ce8a383cef..d12299d5db 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 (cc.sys.os === cc.sys.OS_IOS && !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..8b05eb44c2 100644 --- a/cocos2d/core/components/CCAudioSource.js +++ b/cocos2d/core/components/CCAudioSource.js @@ -52,6 +52,10 @@ var AudioSource = cc.Class({ _volume: 1, _mute: false, _loop: false, + _pausedFlag: { + default: false, + serializable: false + }, /** * !#en @@ -188,6 +192,20 @@ var AudioSource = cc.Class({ } }, + _pausedCallback: function () { + if (!this.audio || this.audio.paused) return; + this.audio.pause(); + this._pausedFlag = true; + }, + + _restoreCallback: function () { + if (!this.audio) return; + if (this._pausedFlag) { + this.audio.resume(); + } + this._pausedFlag = false; + }, + onEnable: function () { if ( this.playOnLoad ) { this.play(); @@ -196,10 +214,14 @@ var AudioSource = cc.Class({ this.audio.src = this._clip; this.audio.preload(); } + 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._pausedCallback, this); + cc.game.off(cc.game.EVENT_SHOW, this._restoreCallback, this); }, onDestroy: function () {