diff --git a/cocos2d/audio/CCAudioEngine.js b/cocos2d/audio/CCAudioEngine.js index 30e14af434..99fbd7fdc5 100644 --- a/cocos2d/audio/CCAudioEngine.js +++ b/cocos2d/audio/CCAudioEngine.js @@ -158,9 +158,15 @@ var audioEngine = { */ setVolume: function (audioID, volume) { var audio = getAudioFromId(audioID); - if (!audio || !audio.setVolume) - return; - audio.setVolume(volume); + if (!audio) return; + if (!audio._loaded) { + audio.once('load', function () { + if (audio.setVolume) + audio.setVolume(volume); + }); + } + if (audio.setVolume) + audio.setVolume(volume); }, /** @@ -193,9 +199,16 @@ var audioEngine = { */ setCurrentTime: function (audioID, sec) { var audio = getAudioFromId(audioID); - if (!audio || !audio.setCurrentTime) - return false; - audio.setCurrentTime(sec); + if (!audio) return false; + if (!audio._loaded) { + audio.once('load', function () { + if (audio.setCurrentTime) + audio.setCurrentTime(sec); + }); + return true; + } + if (audio.setCurrentTime) + audio.setCurrentTime(sec); return true; }, @@ -507,3 +520,8 @@ var audioEngine = { }; module.exports = cc.audioEngine = audioEngine; + +// deprecated +var Module = require('./deprecated'); +Module.removed(audioEngine); +Module.deprecated(audioEngine); diff --git a/cocos2d/audio/deprecated.js b/cocos2d/audio/deprecated.js new file mode 100644 index 0000000000..d75f89905c --- /dev/null +++ b/cocos2d/audio/deprecated.js @@ -0,0 +1,198 @@ +/**************************************************************************** + Copyright (c) 2008-2010 Ricardo Quesada + Copyright (c) 2011-2012 cocos2d-x.org + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +var js = cc.js; +var INFO = cc._LogInfos.deprecated; + +exports.removed = function (audioEngine) { + function willPlayMusicError () { + cc.error('Sorry, cc.audioEngine.willPlayMusic is removed.'); + } + js.getset(audioEngine, 'willPlayMusic', willPlayMusicError, willPlayMusicError); +}; + +exports.deprecated = function (audioEngine) { + + var musicId = -1; + var musicVolume = 1; + var effectsVolume = 1; + var pauseIDCache = []; + js.get(audioEngine, 'playMusic', function () { + cc.warn(INFO, 'audioEngine.playMusic', 'audioEngine.play'); + return function (url, loop) { + musicId = audioEngine.play(url, loop, musicVolume); + return musicId; + } + }); + js.get(audioEngine, 'stopMusic', function () { + cc.warn(INFO, 'audioEngine.stopMusic', 'audioEngine.stop'); + return function () { + audioEngine.stop(musicId); + return musicId; + } + }); + js.get(audioEngine, 'pauseMusic', function () { + cc.warn(INFO, 'audioEngine.pauseMusic', 'audioEngine.pause'); + return function () { + audioEngine.pause(musicId); + return musicId; + } + }); + js.get(audioEngine, 'resumeMusic', function () { + cc.warn(INFO, 'audioEngine.resumeMusic', 'audioEngine.resume'); + return function () { + audioEngine.resume(musicId); + return musicId; + } + }); + js.get(audioEngine, 'rewindMusic', function () { + cc.warn(INFO, 'audioEngine.rewindMusic', 'audioEngine.setCurrentTime'); + return function () { + audioEngine.setCurrentTime(musicId, 0); + return musicId; + } + }); + js.get(audioEngine, 'getMusicVolume', function () { + cc.warn(INFO, 'audioEngine.getMusicVolume', 'audioEngine.getVolume'); + return function () { + return musicVolume; + } + }); + js.get(audioEngine, 'setMusicVolume', function () { + cc.warn(INFO, 'audioEngine.setMusicVolume', 'audioEngine.setVolume'); + return function (volume) { + musicVolume = volume; + audioEngine.setVolume(musicId, musicVolume); + return musicVolume; + } + }); + js.get(audioEngine, 'isMusicPlaying', function () { + cc.warn(INFO, 'audioEngine.isMusicPlaying', 'audioEngine.getState'); + return function () { + return audioEngine.getState(musicId) === audioEngine.AudioState.PLAYING; + } + }); + js.get(audioEngine, 'playEffect', function () { + cc.warn(INFO, 'audioEngine.playEffect', 'audioEngine.play'); + + return function (url, loop, volume) { + return audioEngine.play(url, loop, volume === undefined ? effectsVolume : volume); + } + }); + js.get(audioEngine, 'setEffectsVolume', function (volume) { + cc.warn(INFO, 'audioEngine.setEffectsVolume', 'audioEngine.setVolume'); + return function (volume) { + effectsVolume = volume; + var id2audio = audioEngine._id2audio; + for (var id in id2audio) { + if (id === musicId) continue; + audioEngine.setVolume(id, volume); + } + } + }); + js.get(audioEngine, 'getEffectsVolume', function () { + cc.warn(INFO, 'audioEngine.getEffectsVolume', 'audioEngine.getVolume'); + return function () { + return effectsVolume; + } + }); + js.get(audioEngine, 'pauseEffect', function () { + cc.warn(INFO, 'audioEngine.pauseEffect', 'audioEngine.pause'); + + return function (id) { + return audioEngine.pause(id); + } + }); + js.get(audioEngine, 'pauseAllEffects', function () { + cc.warn(INFO, 'audioEngine.pauseAllEffects', 'audioEngine.pauseAll'); + + return function () { + var id2audio = audioEngine._id2audio; + for (var id in id2audio) { + if (id === musicId) continue; + var audio = id2audio[id]; + var state = audio.getState(); + if (state === audioEngine.AudioState.PLAYING) { + pauseIDCache.push(id); + audio.pause(); + } + } + } + }); + js.get(audioEngine, 'resumeEffect', function () { + cc.warn(INFO, 'audioEngine.resumeEffect', 'audioEngine.resume'); + return function (id) { + audioEngine.resume(id); + } + }); + js.get(audioEngine, 'resumeAllEffects', function () { + cc.warn(INFO, 'audioEngine.resumeEffect', 'audioEngine.resume'); + return function () { + var id2audio = audioEngine._id2audio; + while (pauseIDCache.length > 0) { + var id = pauseIDCache.pop(); + var audio = id2audio[id]; + if (audio && audio.resume) + audio.resume(); + } + } + }); + js.get(audioEngine, 'stopEffect', function () { + cc.warn(INFO, 'audioEngine.stopEffect', 'audioEngine.stop'); + return function (id) { + return audioEngine.stop(id); + } + }); + js.get(audioEngine, 'stopAllEffects', function () { + cc.warn(INFO, 'audioEngine.stopAllEffects', 'audioEngine.stopAll'); + return function () { + var id2audio = audioEngine._id2audio; + for (var id in id2audio) { + if (id === musicId) continue; + var audio = id2audio[id]; + var state = audio.getState(); + if (state === audioEngine.AudioState.PLAYING) { + audio.stop(); + } + } + } + }); + js.get(audioEngine, 'unloadEffect', function () { + cc.warn(INFO, 'audioEngine.unloadEffect', 'audioEngine.stop'); + return function (id) { + return audioEngine.stop(id); + } + }); + + if (!CC_JSB) { + js.get(audioEngine, 'end', function () { + cc.warn(INFO, 'audioEngine.end', 'audioEngine.stopAll'); + return function () { + return audioEngine.stopAll(); + } + }); + } +}; diff --git a/cocos2d/deprecated.js b/cocos2d/deprecated.js index b595e4cef7..a7dd95f045 100644 --- a/cocos2d/deprecated.js +++ b/cocos2d/deprecated.js @@ -667,176 +667,4 @@ if (CC_DEV) { }, }); - (function (audioEngine) { - if (typeof audioEngine === 'undefined') return; - - (function (prop) { - function error () { - cc.error('Sorry, %s.%s is removed.', 'cc.audioEngine', prop); - } - js.getset(audioEngine, prop, error, error); - })('willPlayMusic'); - - var musicId = -1; - var musicVolume = 1; - var effectsVolume = 1; - var pauseIDCache = []; - js.get(audioEngine, 'playMusic', function () { - cc.warn(INFO, 'audioEngine.playMusic', 'audioEngine.play'); - return function (url, loop) { - musicId = audioEngine.play(url, loop, musicVolume); - return musicId; - } - }); - js.get(audioEngine, 'stopMusic', function () { - cc.warn(INFO, 'audioEngine.stopMusic', 'audioEngine.stop'); - return function () { - audioEngine.stop(musicId); - return musicId; - } - }); - js.get(audioEngine, 'pauseMusic', function () { - cc.warn(INFO, 'audioEngine.pauseMusic', 'audioEngine.pause'); - return function () { - audioEngine.pause(musicId); - return musicId; - } - }); - js.get(audioEngine, 'resumeMusic', function () { - cc.warn(INFO, 'audioEngine.resumeMusic', 'audioEngine.resume'); - return function () { - audioEngine.resume(musicId); - return musicId; - } - }); - js.get(audioEngine, 'rewindMusic', function () { - cc.warn(INFO, 'audioEngine.rewindMusic', 'audioEngine.setCurrentTime'); - return function () { - audioEngine.setCurrentTime(musicId, 0); - return musicId; - } - }); - js.get(audioEngine, 'getMusicVolume', function () { - cc.warn(INFO, 'audioEngine.getMusicVolume', 'audioEngine.getVolume'); - return function () { - return musicVolume; - } - }); - js.get(audioEngine, 'setMusicVolume', function () { - cc.warn(INFO, 'audioEngine.setMusicVolume', 'audioEngine.setVolume'); - return function (volume) { - musicVolume = volume; - audioEngine.setVolume(musicId, musicVolume); - return musicVolume; - } - }); - js.get(audioEngine, 'isMusicPlaying', function () { - cc.warn(INFO, 'audioEngine.isMusicPlaying', 'audioEngine.getState'); - return function () { - return audioEngine.getState(musicId) === audioEngine.AudioState.PLAYING; - } - }); - js.get(audioEngine, 'playEffect', function () { - cc.warn(INFO, 'audioEngine.playEffect', 'audioEngine.play'); - - return function (url, loop, volume) { - return audioEngine.play(url, loop, volume === undefined ? effectsVolume : volume); - } - }); - js.get(audioEngine, 'setEffectsVolume', function (volume) { - cc.warn(INFO, 'audioEngine.setEffectsVolume', 'audioEngine.setVolume'); - return function (volume) { - effectsVolume = volume; - var id2audio = audioEngine._id2audio; - for (var id in id2audio) { - if (id === musicId) continue; - audioEngine.setVolume(id, volume); - } - } - }); - js.get(audioEngine, 'getEffectsVolume', function () { - cc.warn(INFO, 'audioEngine.getEffectsVolume', 'audioEngine.getVolume'); - return function () { - return effectsVolume; - } - }); - js.get(audioEngine, 'pauseEffect', function () { - cc.warn(INFO, 'audioEngine.pauseEffect', 'audioEngine.pause'); - - return function (id) { - return audioEngine.pause(id); - } - }); - js.get(audioEngine, 'pauseAllEffects', function () { - cc.warn(INFO, 'audioEngine.pauseAllEffects', 'audioEngine.pauseAll'); - - return function () { - var id2audio = audioEngine._id2audio; - for (var id in id2audio) { - if (id === musicId) continue; - var audio = id2audio[id]; - var state = audio.getState(); - if (state === audioEngine.AudioState.PLAYING) { - pauseIDCache.push(id); - audio.pause(); - } - } - } - }); - js.get(audioEngine, 'resumeEffect', function () { - cc.warn(INFO, 'audioEngine.resumeEffect', 'audioEngine.resume'); - return function (id) { - audioEngine.resume(id); - } - }); - js.get(audioEngine, 'resumeAllEffects', function () { - cc.warn(INFO, 'audioEngine.resumeEffect', 'audioEngine.resume'); - return function () { - var id2audio = audioEngine._id2audio; - while (pauseIDCache.length > 0) { - var id = pauseIDCache.pop(); - var audio = id2audio[id]; - if (audio && audio.resume) - audio.resume(); - } - } - }); - js.get(audioEngine, 'stopEffect', function () { - cc.warn(INFO, 'audioEngine.stopEffect', 'audioEngine.stop'); - return function (id) { - return audioEngine.stop(id); - } - }); - js.get(audioEngine, 'stopAllEffects', function () { - cc.warn(INFO, 'audioEngine.stopAllEffects', 'audioEngine.stopAll'); - return function () { - var id2audio = audioEngine._id2audio; - for (var id in id2audio) { - if (id === musicId) continue; - var audio = id2audio[id]; - var state = audio.getState(); - if (state === audioEngine.AudioState.PLAYING) { - audio.stop(); - } - } - } - }); - js.get(audioEngine, 'unloadEffect', function () { - cc.warn(INFO, 'audioEngine.unloadEffect', 'audioEngine.stop'); - return function (id) { - return audioEngine.stop(id); - } - }); - - if (!CC_JSB) { - js.get(audioEngine, 'end', function () { - cc.warn(INFO, 'audioEngine.end', 'audioEngine.stopAll'); - return function () { - return audioEngine.stopAll(); - } - }); - } - - })(cc.audioEngine); - } diff --git a/jsb/jsb-audio.js b/jsb/jsb-audio.js index 4bd3730eba..c97d58e13d 100644 --- a/jsb/jsb-audio.js +++ b/jsb/jsb-audio.js @@ -47,6 +47,10 @@ cc.Audio.Type = { cc.audioEngine = audioEngine; audioEngine.play = audioEngine.play2d; audioEngine.setMaxWebAudioSize = function () {}; + // deprecated + var Module = require('../cocos2d/audio/deprecated'); + Module.removed(audioEngine); + Module.deprecated(audioEngine); proto.State = audioEngine.AudioState; @@ -102,6 +106,7 @@ cc.Audio.Type = { }; proto.preload = function () { + this._loaded = true; this.emit('load'); };