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