@@ -9,6 +9,7 @@ import User from './user';
99import Lrc from './lrc' ;
1010import Controller from './controller' ;
1111import Timer from './timer' ;
12+ import Events from './events' ;
1213
1314const instances = [ ] ;
1415
@@ -27,17 +28,7 @@ class APlayer {
2728 this . audios = [ ] ;
2829 this . mode = this . options . mode ;
2930
30- // define APlayer events
31- const eventTypes = [ 'play' , 'pause' , 'canplay' , 'playing' , 'ended' , 'error' ] ;
32- this . event = { } ;
33- for ( let i = 0 ; i < eventTypes . length ; i ++ ) {
34- this . event [ eventTypes [ i ] ] = [ ] ;
35- }
36- this . trigger = ( type ) => {
37- for ( let i = 0 ; i < this . event [ type ] . length ; i ++ ) {
38- this . event [ type ] [ i ] ( ) ;
39- }
40- } ;
31+ this . events = new Events ( ) ;
4132
4233 // multiple music
4334 this . playIndex = 0 ;
@@ -145,6 +136,12 @@ class APlayer {
145136 }
146137 } ) ;
147138
139+ for ( let i = 0 ; i < this . events . audioEvents . length ; i ++ ) {
140+ this . audio . addEventListener ( this . events . audioEvents [ i ] , ( ) => {
141+ this . events . trigger ( this . events . audioEvents [ i ] ) ;
142+ } ) ;
143+ }
144+
148145 // show audio time: the metadata has loaded or changed
149146 this . audio . addEventListener ( 'durationchange' , ( ) => {
150147 if ( this . audio . duration !== 1 ) { // compatibility: Android browsers will output 1 at first
@@ -344,12 +341,10 @@ class APlayer {
344341 }
345342
346343 /**
347- * attach event
344+ * bind events
348345 */
349- on ( name , func ) {
350- if ( typeof func === 'function' ) {
351- this . event [ name ] . push ( func ) ;
352- }
346+ on ( name , callback ) {
347+ this . events . on ( name , callback ) ;
353348 }
354349
355350 /**
0 commit comments