1 /**************************************************************************** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 4 http://www.cocos2d-x.org 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy 7 of this software and associated documentation files (the "Software"), to deal 8 in the Software without restriction, including without limitation the rights 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 copies of the Software, and to permit persons to whom the Software is 11 furnished to do so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in 14 all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 THE SOFTWARE. 23 ****************************************************************************/ 24 25 /** 26 * Base class for ccs.ComAudio 27 * @class 28 * @extends ccs.Component 29 */ 30 ccs.ComAudio = ccs.Component.extend(/** @lends ccs.ComAudio# */{ 31 _filePath: "", 32 _loop: false, 33 ctor: function () { 34 cc.Component.prototype.ctor.call(this); 35 this._name = "Audio"; 36 }, 37 init: function () { 38 return true; 39 }, 40 41 onEnter: function () { 42 }, 43 44 onExit: function () { 45 this.stopBackgroundMusic(true); 46 this.stopAllEffects(); 47 }, 48 49 end: function () { 50 cc.audioEngine.end(); 51 }, 52 53 /** 54 * Preload background music resource 55 * @param {String} pszFilePath 56 */ 57 preloadBackgroundMusic: function (pszFilePath) { 58 cc.loader.load(pszFilePath); 59 }, 60 61 /** 62 * Play background music 63 * @param {String} pszFilePath 64 * @param {Boolean} loop 65 */ 66 playBackgroundMusic: function (pszFilePath, loop) { 67 if(pszFilePath){ 68 cc.audioEngine.playMusic(pszFilePath, loop); 69 }else{ 70 cc.audioEngine.playMusic(this._filePath, this._loop); 71 } 72 }, 73 74 /** 75 * Stop background music 76 * @param {String} releaseData 77 */ 78 stopBackgroundMusic: function (releaseData) { 79 cc.audioEngine.stopMusic(releaseData); 80 }, 81 82 /** 83 * Pause background music 84 */ 85 pauseBackgroundMusic: function () { 86 cc.audioEngine.pauseMusic(); 87 }, 88 89 /** 90 * Resume background music 91 */ 92 resumeBackgroundMusic: function () { 93 cc.audioEngine.resumeMusic(); 94 }, 95 96 /** 97 * Rewind background music 98 */ 99 rewindBackgroundMusic: function () { 100 cc.audioEngine.rewindMusic(); 101 }, 102 103 /** 104 * Indicates whether any background music can be played or not. 105 * @returns {boolean} 106 */ 107 willPlayBackgroundMusic: function () { 108 return cc.audioEngine.willPlayMusic(); 109 }, 110 111 /** 112 * Whether the music is playing. 113 * @returns {Boolean} 114 */ 115 isBackgroundMusicPlaying: function () { 116 return cc.audioEngine.isMusicPlaying(); 117 }, 118 119 /** 120 * The volume of the music max value is 1.0,the min value is 0.0 . 121 * @returns {Number} 122 */ 123 getBackgroundMusicVolume: function () { 124 return cc.audioEngine.getMusicVolume(); 125 }, 126 127 /** 128 * Set the volume of music. 129 * @param {Number} volume must be in 0.0~1.0 . 130 */ 131 setBackgroundMusicVolume: function (volume) { 132 cc.audioEngine.setMusicVolume(volume); 133 }, 134 135 /** 136 * The volume of the effects max value is 1.0,the min value is 0.0 . 137 * @returns {Number} 138 */ 139 getEffectsVolume: function () { 140 return cc.audioEngine.getEffectsVolume(); 141 }, 142 143 /** 144 * Set the volume of sound effects. 145 * @param {Number} volume 146 */ 147 setEffectsVolume: function (volume) { 148 cc.audioEngine.setEffectsVolume(volume); 149 }, 150 151 /** 152 * Play sound effect. 153 * @param {String} pszFilePath 154 * @param {Boolean} loop 155 * @returns {Boolean} 156 */ 157 playEffect: function (pszFilePath, loop) { 158 if (pszFilePath) { 159 return cc.audioEngine.playEffect(pszFilePath, loop); 160 } else { 161 return cc.audioEngine.playEffect(this._filePath, this._loop); 162 } 163 }, 164 165 /** 166 * Pause playing sound effect. 167 * @param {Number} soundId 168 */ 169 pauseEffect: function (soundId) { 170 cc.audioEngine.pauseEffect(soundId); 171 }, 172 173 /** 174 * Pause all effects 175 */ 176 pauseAllEffects: function () { 177 cc.audioEngine.pauseAllEffects(); 178 }, 179 180 /** 181 * Resume effect 182 * @param {Number} soundId 183 */ 184 resumeEffect: function (soundId) { 185 cc.audioEngine.resumeEffect(soundId); 186 }, 187 188 /** 189 * Resume all effects 190 */ 191 resumeAllEffects: function () { 192 cc.audioEngine.resumeAllEffects(); 193 }, 194 195 /** 196 * Stop effect 197 * @param {Number} soundId 198 */ 199 stopEffect: function (soundId) { 200 cc.audioEngine.stopEffect(soundId); 201 }, 202 203 /** 204 * stop all effects 205 */ 206 stopAllEffects: function () { 207 cc.audioEngine.stopAllEffects(); 208 }, 209 210 /** 211 * Preload effect 212 * @param {String} pszFilePath 213 */ 214 preloadEffect: function (pszFilePath) { 215 cc.loader.getRes(pszFilePath); 216 this.setFile(pszFilePath); 217 this.setLoop(false); 218 }, 219 220 /** 221 * Unload effect 222 * @param {String} pszFilePath 223 */ 224 unloadEffect: function (pszFilePath) { 225 cc.audioEngine.unloadEffect(pszFilePath); 226 }, 227 228 /** 229 * File path setter 230 * @param {String} pszFilePath 231 */ 232 setFile: function (pszFilePath) { 233 this._filePath = pszFilePath; 234 }, 235 236 /** 237 * Set loop 238 * @param {Boolean} loop 239 */ 240 setLoop: function (loop) { 241 this._loop = loop; 242 }, 243 244 /** 245 * File path Getter 246 * @returns {string} 247 */ 248 getFile: function () { 249 return this._filePath; 250 }, 251 252 /** 253 * Whether is loop 254 * @returns {boolean} 255 */ 256 isLoop: function () { 257 return this._loop; 258 } 259 }); 260 /** 261 * allocates and initializes a ComAudio. 262 * @constructs 263 * @return {ccs.ComAudio} 264 * @example 265 * // example 266 * var com = ccs.ComAudio.create(); 267 */ 268 ccs.ComAudio.create = function () { 269 var com = new ccs.ComAudio(); 270 if (com && com.init()) { 271 return com; 272 } 273 return null; 274 };