@@ -33,7 +33,11 @@ var cc = cc || {};
3333 * @extends cc.Class
3434 */
3535cc . AudioEngine = cc . Class . extend ( /** @lends cc.AudioEngine# */ {
36-
36+ _audioID :0 ,
37+ _audioIDList :null ,
38+ ctor :function ( ) {
39+ this . _audioIDList = { } ;
40+ } ,
3741 /**
3842 * Check each type to see if it can be played by current browser
3943 * @param {Object } capabilities The results are filled into this dict
@@ -129,8 +133,9 @@ cc.SimpleAudioEngine = cc.AudioEngine.extend(/** @lends cc.SimpleAudioEngine# */
129133 * Constructor
130134 */
131135 ctor :function ( ) {
132- this . _supportedFormat = [ ] ;
136+ cc . AudioEngine . prototype . ctor . call ( this ) ;
133137
138+ this . _supportedFormat = [ ] ;
134139 this . _checkCanPlay ( this . _capabilities ) ;
135140
136141 // enable sound if any of the audio format is supported
@@ -399,8 +404,10 @@ cc.SimpleAudioEngine = cc.AudioEngine.extend(/** @lends cc.SimpleAudioEngine# */
399404 au . loop = loop ;
400405 }
401406 au . play ( ) ;
407+ var audioID = this . _audioID ++ ;
408+ this . _audioIDList [ audioID ] = au ;
402409
403- return path ;
410+ return audioID ;
404411 } ,
405412
406413 /**
@@ -446,21 +453,18 @@ cc.SimpleAudioEngine = cc.AudioEngine.extend(/** @lends cc.SimpleAudioEngine# */
446453
447454 /**
448455 * Pause playing sound effect.
449- * @param {String } path The return value of function playEffect.
456+ * @param {Number } audioID The return value of function playEffect.
450457 * @example
451458 * //example
452- * cc.AudioEngine.getInstance().pauseEffect(path );
459+ * cc.AudioEngine.getInstance().pauseEffect(audioID );
453460 */
454- pauseEffect :function ( path ) {
455- if ( ! path ) return ;
456- var keyname = this . _getPathWithoutExt ( path ) ;
457- if ( this . _effectList . hasOwnProperty ( keyname ) ) {
458- var tmpArr = this . _effectList [ keyname ] , au ;
459- for ( var i = tmpArr . length - 1 ; i >= 0 ; i -- ) {
460- au = tmpArr [ i ] ;
461- if ( ! au . ended ) {
462- au . pause ( ) ;
463- }
461+ pauseEffect :function ( audioID ) {
462+ if ( audioID == null ) return ;
463+
464+ if ( this . _audioIDList . hasOwnProperty ( audioID ) ) {
465+ var au = this . _audioIDList [ audioID ] ;
466+ if ( ! au . ended ) {
467+ au . pause ( ) ;
464468 }
465469 }
466470 } ,
@@ -486,24 +490,18 @@ cc.SimpleAudioEngine = cc.AudioEngine.extend(/** @lends cc.SimpleAudioEngine# */
486490
487491 /**
488492 * Resume playing sound effect.
489- * @param {String } path The return value of function playEffect.
490- * @example
493+ * @param {Number } audioID The return value of function playEffect.
494+ * @audioID
491495 * //example
492- * cc.AudioEngine.getInstance().resumeEffect(path );
496+ * cc.AudioEngine.getInstance().resumeEffect(audioID );
493497 */
494- resumeEffect :function ( path ) {
495- if ( ! path ) return ;
496- var tmpArr , au ;
497- var keyname = this . _getPathWithoutExt ( path ) ;
498- if ( this . _effectList . hasOwnProperty ( keyname ) ) {
499- tmpArr = this . _effectList [ keyname ] ;
500- if ( tmpArr . length > 0 ) {
501- for ( var i = 0 ; i < tmpArr . length ; i ++ ) {
502- au = tmpArr [ i ] ;
503- if ( ! au . ended ) {
504- au . play ( ) ;
505- }
506- }
498+ resumeEffect :function ( audioID ) {
499+ if ( audioID == null ) return ;
500+
501+ if ( this . _audioIDList . hasOwnProperty ( audioID ) ) {
502+ var au = this . _audioIDList [ audioID ] ;
503+ if ( ! au . ended ) {
504+ au . play ( ) ;
507505 }
508506 }
509507 } ,
@@ -531,25 +529,19 @@ cc.SimpleAudioEngine = cc.AudioEngine.extend(/** @lends cc.SimpleAudioEngine# */
531529
532530 /**
533531 * Stop playing sound effect.
534- * @param {String } path The return value of function playEffect.
532+ * @param {Number } audioID The return value of function playEffect.
535533 * @example
536534 * //example
537- * cc.AudioEngine.getInstance().stopEffect(path );
535+ * cc.AudioEngine.getInstance().stopEffect(audioID );
538536 */
539- stopEffect :function ( path ) {
540- if ( ! path ) return ;
541- var tmpArr , au ;
542- var keyname = this . _getPathWithoutExt ( path ) ;
543- if ( this . _effectList . hasOwnProperty ( keyname ) ) {
544- tmpArr = this . _effectList [ keyname ] ;
545- if ( tmpArr . length > 0 ) {
546- for ( var i = 0 ; i < tmpArr . length ; i ++ ) {
547- au = tmpArr [ i ] ;
548- if ( ! au . ended ) {
549- au . loop = false ;
550- au . currentTime = au . duration ;
551- }
552- }
537+ stopEffect :function ( audioID ) {
538+ if ( audioID == null ) return ;
539+
540+ if ( this . _audioIDList . hasOwnProperty ( audioID ) ) {
541+ var au = this . _audioIDList [ audioID ] ;
542+ if ( ! au . ended ) {
543+ au . loop = false ;
544+ au . currentTime = au . duration ;
553545 }
554546 }
555547 } ,
@@ -588,6 +580,15 @@ cc.SimpleAudioEngine = cc.AudioEngine.extend(/** @lends cc.SimpleAudioEngine# */
588580 this . stopEffect ( path ) ;
589581 delete this . _effectList [ keyname ] ;
590582 }
583+
584+ var au , pathName ;
585+ for ( var k in this . _audioIDList ) {
586+ au = this . _audioIDList [ k ] ;
587+ pathName = this . _getPathWithoutExt ( au . src ) ;
588+ if ( pathName == keyname ) {
589+ delete this . _audioIDList [ k ] ;
590+ }
591+ }
591592 } ,
592593
593594 _getEffectList :function ( elt ) {
@@ -701,7 +702,9 @@ cc.WebAudioEngine = cc.AudioEngine.extend(/** @lends cc.WebAudioEngine# */{
701702 /**
702703 * Constructor
703704 */
704- ctor : function ( ) { } ,
705+ ctor : function ( ) {
706+ cc . AudioEngine . prototype . ctor . call ( this ) ;
707+ } ,
705708
706709 /**
707710 * Initialization
@@ -1165,8 +1168,12 @@ cc.WebAudioEngine = cc.AudioEngine.extend(/** @lends cc.WebAudioEngine# */{
11651168 */
11661169 } ) ;
11671170 }
1171+
11681172 // cc.SimpleAudioEngine returns path, just do the same for backward compatibility. DO NOT rely on this, though!
1169- return path ;
1173+ var audioID = this . _audioID ++ ;
1174+ this . _audioIDList [ audioID ] = this . _effects [ keyName ] ;
1175+
1176+ return audioID ;
11701177 } ,
11711178
11721179 /**
@@ -1223,23 +1230,19 @@ cc.WebAudioEngine = cc.AudioEngine.extend(/** @lends cc.WebAudioEngine# */{
12231230
12241231 /**
12251232 * Pause playing sound effect.
1226- * @param {String } path The return value of function playEffect.
1233+ * @param {Number } audioID The return value of function playEffect.
12271234 * @example
12281235 * //example
1229- * cc.AudioEngine.getInstance().pauseEffect(path );
1236+ * cc.AudioEngine.getInstance().pauseEffect(audioID );
12301237 */
1231- pauseEffect : function ( path ) {
1232- if ( ! path ) {
1238+ pauseEffect : function ( audioID ) {
1239+ if ( audioID == null ) {
12331240 return ;
12341241 }
12351242
1236- var keyName = this . _getPathWithoutExt ( path ) ;
1237- if ( ! ( keyName in this . _effects ) ) {
1238- // the effect is not even in the playing list
1239- return ;
1243+ if ( this . _audioIDList . hasOwnProperty ( audioID ) ) {
1244+ this . _pauseSoundList ( this . _audioIDList [ audioID ] ) ;
12401245 }
1241-
1242- this . _pauseSoundList ( this . _effects [ keyName ] ) ;
12431246 } ,
12441247
12451248 /**
@@ -1270,23 +1273,19 @@ cc.WebAudioEngine = cc.AudioEngine.extend(/** @lends cc.WebAudioEngine# */{
12701273
12711274 /**
12721275 * Resume playing sound effect.
1273- * @param {String } path The return value of function playEffect.
1276+ * @param {Number } audioID The return value of function playEffect.
12741277 * @example
12751278 * //example
1276- * cc.AudioEngine.getInstance().resumeEffect(path );
1279+ * cc.AudioEngine.getInstance().resumeEffect(audioID );
12771280 */
1278- resumeEffect : function ( path ) {
1279- if ( ! path ) {
1281+ resumeEffect : function ( audioID ) {
1282+ if ( audioID == null ) {
12801283 return ;
12811284 }
12821285
1283- var keyName = this . _getPathWithoutExt ( path ) ;
1284- if ( ! ( keyName in this . _effects ) ) {
1285- // the effect is not even in the playing list
1286- return ;
1286+ if ( this . _audioIDList . hasOwnProperty ( audioID ) ) {
1287+ this . _resumeSoundList ( this . _audioIDList [ audioID ] , this . getEffectsVolume ( ) ) ;
12871288 }
1288-
1289- this . _resumeSoundList ( this . _effects [ keyName ] , this . getEffectsVolume ( ) ) ;
12901289 } ,
12911290
12921291 /**
@@ -1303,27 +1302,19 @@ cc.WebAudioEngine = cc.AudioEngine.extend(/** @lends cc.WebAudioEngine# */{
13031302
13041303 /**
13051304 * Stop playing sound effect.
1306- * @param {String } path The return value of function playEffect.
1305+ * @param {Number } audioID The return value of function playEffect.
13071306 * @example
13081307 * //example
1309- * cc.AudioEngine.getInstance().stopEffect(path );
1308+ * cc.AudioEngine.getInstance().stopEffect(audioID );
13101309 */
1311- stopEffect : function ( path ) {
1312- if ( ! path ) {
1310+ stopEffect : function ( audioID ) {
1311+ if ( audioID == null ) {
13131312 return ;
13141313 }
13151314
1316- var keyName = this . _getPathWithoutExt ( path ) ;
1317- if ( ! ( keyName in this . _effects ) ) {
1318- // can stop only when it's playing/paused, in other words, when it's on the list
1319- return ;
1320- }
1321-
1322- var effectList = this . _effects [ keyName ] ;
1323- for ( var idx in effectList ) {
1324- this . _endSound ( effectList [ idx ] ) ;
1315+ if ( this . _audioIDList . hasOwnProperty ( audioID ) ) {
1316+ this . _endSound ( this . _audioIDList [ audioID ] ) ;
13251317 }
1326- delete this . _effects [ keyName ] ;
13271318 } ,
13281319
13291320 /**
@@ -1383,7 +1374,8 @@ cc.AudioEngine.isMusicPlaying = false;
13831374 */
13841375cc . AudioEngine . getInstance = function ( ) {
13851376 if ( ! this . _instance ) {
1386- if ( cc . Browser . supportWebAudio ) {
1377+ var ua = navigator . userAgent ;
1378+ if ( cc . Browser . supportWebAudio && ( / i P h o n e O S / . test ( ua ) || / i P a d / . test ( ua ) ) ) {
13871379 this . _instance = new cc . WebAudioEngine ( ) ;
13881380 } else {
13891381 this . _instance = new cc . SimpleAudioEngine ( ) ;
0 commit comments