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 * Base class for ccs.ComAudio 28 * @class 29 * @extends ccs.Component 30 */ 31 ccs.ComAudio = ccs.Component.extend(/** @lends ccs.ComAudio# */{ 32 _filePath: "", 33 _loop: false, 34 ctor: function () { 35 cc.Component.prototype.ctor.call(this); 36 this._name = "Audio"; 37 }, 38 init: function () { 39 return true; 40 }, 41 42 onEnter: function () { 43 }, 44 45 onExit: function () { 46 this.stopBackgroundMusic(true); 47 this.stopAllEffects(); 48 }, 49 50 end: function () { 51 cc.audioEngine.end(); 52 }, 53 54 /** 55 * Preload background music resource 56 * @param {String} pszFilePath 57 */ 58 preloadBackgroundMusic: function (pszFilePath) { 59 cc.loader.load(pszFilePath); 60 }, 61 62 /** 63 * Play background music 64 * @param {String} pszFilePath 65 * @param {Boolean} loop 66 */ 67 playBackgroundMusic: function (pszFilePath, loop) { 68 if(pszFilePath){ 69 cc.audioEngine.playMusic(pszFilePath, loop); 70 }else{ 71 cc.audioEngine.playMusic(this._filePath, this._loop); 72 } 73 }, 74 75 /** 76 * Stop background music 77 * @param {String} releaseData 78 */ 79 stopBackgroundMusic: function (releaseData) { 80 cc.audioEngine.stopMusic(releaseData); 81 }, 82 83 /** 84 * Pause background music 85 */ 86 pauseBackgroundMusic: function () { 87 cc.audioEngine.pauseMusic(); 88 }, 89 90 /** 91 * Resume background music 92 */ 93 resumeBackgroundMusic: function () { 94 cc.audioEngine.resumeMusic(); 95 }, 96 97 /** 98 * Rewind background music 99 */ 100 rewindBackgroundMusic: function () { 101 cc.audioEngine.rewindMusic(); 102 }, 103 104 /** 105 * Indicates whether any background music can be played or not. 106 * @returns {boolean} 107 */ 108 willPlayBackgroundMusic: function () { 109 return cc.audioEngine.willPlayMusic(); 110 }, 111 112 /** 113 * Whether the music is playing. 114 * @returns {Boolean} 115 */ 116 isBackgroundMusicPlaying: function () { 117 return cc.audioEngine.isMusicPlaying(); 118 }, 119 120 /** 121 * The volume of the music max value is 1.0,the min value is 0.0 . 122 * @returns {Number} 123 */ 124 getBackgroundMusicVolume: function () { 125 return cc.audioEngine.getMusicVolume(); 126 }, 127 128 /** 129 * Set the volume of music. 130 * @param {Number} volume must be in 0.0~1.0 . 131 */ 132 setBackgroundMusicVolume: function (volume) { 133 cc.audioEngine.setMusicVolume(volume); 134 }, 135 136 /** 137 * The volume of the effects max value is 1.0,the min value is 0.0 . 138 * @returns {Number} 139 */ 140 getEffectsVolume: function () { 141 return cc.audioEngine.getEffectsVolume(); 142 }, 143 144 /** 145 * Set the volume of sound effects. 146 * @param {Number} volume 147 */ 148 setEffectsVolume: function (volume) { 149 cc.audioEngine.setEffectsVolume(volume); 150 }, 151 152 /** 153 * Play sound effect. 154 * @param {String} pszFilePath 155 * @param {Boolean} loop 156 * @returns {Boolean} 157 */ 158 playEffect: function (pszFilePath, loop) { 159 if (pszFilePath) { 160 return cc.audioEngine.playEffect(pszFilePath, loop); 161 } else { 162 return cc.audioEngine.playEffect(this._filePath, this._loop); 163 } 164 }, 165 166 /** 167 * Pause playing sound effect. 168 * @param {Number} soundId 169 */ 170 pauseEffect: function (soundId) { 171 cc.audioEngine.pauseEffect(soundId); 172 }, 173 174 /** 175 * Pause all effects 176 */ 177 pauseAllEffects: function () { 178 cc.audioEngine.pauseAllEffects(); 179 }, 180 181 /** 182 * Resume effect 183 * @param {Number} soundId 184 */ 185 resumeEffect: function (soundId) { 186 cc.audioEngine.resumeEffect(soundId); 187 }, 188 189 /** 190 * Resume all effects 191 */ 192 resumeAllEffects: function () { 193 cc.audioEngine.resumeAllEffects(); 194 }, 195 196 /** 197 * Stop effect 198 * @param {Number} soundId 199 */ 200 stopEffect: function (soundId) { 201 cc.audioEngine.stopEffect(soundId); 202 }, 203 204 /** 205 * stop all effects 206 */ 207 stopAllEffects: function () { 208 cc.audioEngine.stopAllEffects(); 209 }, 210 211 /** 212 * Preload effect 213 * @param {String} pszFilePath 214 */ 215 preloadEffect: function (pszFilePath) { 216 cc.loader.getRes(pszFilePath); 217 this.setFile(pszFilePath); 218 this.setLoop(false); 219 }, 220 221 /** 222 * Unload effect 223 * @param {String} pszFilePath 224 */ 225 unloadEffect: function (pszFilePath) { 226 cc.audioEngine.unloadEffect(pszFilePath); 227 }, 228 229 /** 230 * File path setter 231 * @param {String} pszFilePath 232 */ 233 setFile: function (pszFilePath) { 234 this._filePath = pszFilePath; 235 }, 236 237 /** 238 * Set loop 239 * @param {Boolean} loop 240 */ 241 setLoop: function (loop) { 242 this._loop = loop; 243 }, 244 245 /** 246 * File path Getter 247 * @returns {string} 248 */ 249 getFile: function () { 250 return this._filePath; 251 }, 252 253 /** 254 * Whether is loop 255 * @returns {boolean} 256 */ 257 isLoop: function () { 258 return this._loop; 259 } 260 }); 261 /** 262 * allocates and initializes a ComAudio. 263 * @constructs 264 * @return {ccs.ComAudio} 265 * @example 266 * // example 267 * var com = ccs.ComAudio.create(); 268 */ 269 ccs.ComAudio.create = function () { 270 var com = new ccs.ComAudio(); 271 if (com && com.init()) { 272 return com; 273 } 274 return null; 275 };