@@ -36,6 +36,7 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
3636 _soundEnable :false ,
3737 _effectList :{ } ,
3838 _muiscList :{ } ,
39+ _soundList :{ } ,
3940 _isMusicPlaying :false ,
4041 _playingMusic :null ,
4142 _effectsVolume :1 ,
@@ -81,13 +82,13 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
8182 /**
8283 * Preload music resource.<br />
8384 * This method is called when cc.Loader preload resources.
84- * @param {String } path The path of the music file without filename extension.
85+ * @param {String } path The path of the music file with filename extension.
8586 */
86- preloadMusic :function ( path ) {
87+ preloadSound :function ( path ) {
8788 if ( this . _soundEnable ) {
8889 var extName = this . _getExtFromFullPath ( path ) ;
8990 var keyname = this . _getPathWithoutExt ( path ) ;
90- if ( this . _checkAudioFormatSupported ( extName ) && ! this . _muiscList . hasOwnProperty ( keyname ) ) {
91+ if ( this . _checkAudioFormatSupported ( extName ) && ! this . _soundList . hasOwnProperty ( keyname ) ) {
9192 var soundCache = new Audio ( path ) ;
9293 soundCache . preload = 'auto' ;
9394
@@ -96,21 +97,13 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
9697 } , false ) ;
9798
9899 soundCache . addEventListener ( "error" , function ( e ) {
100+ this . removeEventListener ( 'error' , arguments . callee , false ) ;
99101 cc . Loader . getInstance ( ) . onResLoadingErr ( ) ;
100102 } , false ) ;
101103
102- soundCache . addEventListener ( "playing" , function ( e ) {
103- cc . AudioEngine . _instance . _isMusicPlaying = true ;
104- } , false ) ;
105-
106- soundCache . addEventListener ( "pause" , function ( e ) {
107- cc . AudioEngine . _instance . _isMusicPlaying = false ;
108- } , false ) ;
109-
104+ this . _soundList [ keyname ] = true ;
110105 // load it
111106 soundCache . load ( ) ;
112-
113- this . _muiscList [ keyname ] = soundCache
114107 }
115108 }
116109 cc . Loader . getInstance ( ) . onResLoaded ( ) ;
@@ -126,14 +119,31 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
126119 */
127120 playMusic :function ( path , loop ) {
128121 var keyname = this . _getPathWithoutExt ( path ) ;
122+ var actExt = this . _supportedFormat [ 0 ] ;
123+ var au ;
129124 if ( this . _muiscList . hasOwnProperty ( this . _playingMusic ) ) {
130125 this . _muiscList [ this . _playingMusic ] . pause ( ) ;
131126 }
132127 this . _playingMusic = keyname ;
133128 if ( this . _muiscList . hasOwnProperty ( this . _playingMusic ) ) {
134- this . _muiscList [ this . _playingMusic ] . loop = loop || false ;
135- this . _muiscList [ this . _playingMusic ] . play ( ) ;
129+ au = this . _muiscList [ this . _playingMusic ] ;
136130 }
131+ else {
132+ au = new Audio ( keyname + "." + actExt ) ;
133+ au . preload = 'auto' ;
134+ this . _muiscList [ this . _playingMusic ] = au ;
135+
136+ au . addEventListener ( "playing" , function ( e ) {
137+ cc . AudioEngine . _instance . _isMusicPlaying = true ;
138+ } , false ) ;
139+
140+ au . addEventListener ( "pause" , function ( e ) {
141+ cc . AudioEngine . _instance . _isMusicPlaying = false ;
142+ } , false ) ;
143+ }
144+
145+ au . loop = loop || false ;
146+ au . play ( ) ;
137147 } ,
138148
139149 /**
@@ -145,9 +155,10 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
145155 */
146156 stopMusic :function ( releaseData ) {
147157 if ( this . _muiscList . hasOwnProperty ( this . _playingMusic ) ) {
148- this . _muiscList [ this . _playingMusic ] . pause ( ) ;
149- this . _muiscList [ this . _playingMusic ] . currentTime = 0 ;
150- if ( releaseData && this . _muiscList . hasOwnProperty ( this . _playingMusic ) ) {
158+ var au = this . _muiscList [ this . _playingMusic ] ;
159+ au . pause ( ) ;
160+ au . currentTime = au . duration ;
161+ if ( releaseData ) {
151162 delete this . _muiscList [ this . _playingMusic ] ;
152163 }
153164 }
@@ -189,6 +200,7 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
189200 this . _muiscList [ this . _playingMusic ] . play ( ) ;
190201 }
191202 } ,
203+
192204 willPlayMusic :function ( ) {
193205 return false ;
194206 } ,
@@ -245,26 +257,9 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
245257 }
246258 } ,
247259
248- /**
249- * Preload sound effect resource.
250- * This method is called when cc.Loader preload resources.
251- * @param {String } path The path of the sound effect file without filename extension.
252- */
253- preloadEffect :function ( path ) {
254- if ( this . _soundEnable ) {
255- var extName = this . _getExtFromFullPath ( path ) ;
256- var keyname = this . _getPathWithoutExt ( path ) ;
257- if ( this . _checkAudioFormatSupported ( extName ) && ! this . _effectList . hasOwnProperty ( keyname ) ) {
258- this . _effectList [ keyname ] = [ ] ;
259- this . _effectList [ keyname ] . push ( new Audio ( path ) ) ;
260- }
261- }
262- cc . Loader . getInstance ( ) . onResLoaded ( ) ;
263- } ,
264-
265260 /**
266261 * Play sound effect.
267- * @param {String } path The path of the sound effect without filename extension.
262+ * @param {String } path The path of the sound effect with filename extension.
268263 * @param {Boolean } loop Whether to loop the effect playing, default value is false
269264 * @example
270265 * //example
@@ -274,7 +269,7 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
274269 var keyname = this . _getPathWithoutExt ( path ) ;
275270 var actExt = this . _supportedFormat [ 0 ] ;
276271 var reclaim = this . _getEffectList ( keyname ) , au ;
277- if ( reclaim . length > 0 ) {
272+ if ( reclaim . length > 0 ) {
278273 for ( var i = 0 ; i < reclaim . length ; i ++ ) {
279274 //if one of the effect ended, play it
280275 if ( reclaim [ i ] . ended ) {
@@ -284,12 +279,13 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
284279 }
285280 }
286281 }
287- if ( ! au ) {
282+
283+ if ( ! au ) {
288284 if ( reclaim . length >= this . _maxAudioInstance ) {
289285 cc . log ( "Error: " + path + " greater than " + this . _maxAudioInstance ) ;
290286 return keyname ;
291287 }
292- au = new Audio ( keyname + "." + actExt ) ;
288+ au = new Audio ( keyname + "." + actExt ) ;
293289 reclaim . push ( au ) ;
294290 }
295291
@@ -478,6 +474,7 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
478474 unloadEffect :function ( path ) {
479475 var keyname = this . _getPathWithoutExt ( path ) ;
480476 if ( this . _effectList . hasOwnProperty ( keyname ) ) {
477+ this . stopEffect ( path ) ;
481478 delete this . _effectList [ keyname ] ;
482479 }
483480 } ,
@@ -498,7 +495,8 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
498495 return this . _effectList [ elt ] ;
499496 }
500497 else {
501- return [ ] ;
498+ this . _effectList [ elt ] = [ ] ;
499+ return this . _effectList [ elt ] ;
502500 }
503501 } ,
504502
0 commit comments