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