@@ -31,6 +31,7 @@ define([
3131 this . waveLayoutOptions ;
3232 this . firstDrawing = true ;
3333 this . latestRange ;
34+ this . _plugins = [ ] ;
3435
3536 if ( options !== undefined ) {
3637
@@ -70,8 +71,6 @@ define([
7071
7172 fps = count ;
7273
73- console . log ( 'fps: ' + fps ) ;
74-
7574 count = 1 ;
7675
7776 } else {
@@ -364,6 +363,38 @@ define([
364363 this . waveLayoutOptions = layoutOptions ;
365364
366365 } ;
366+
367+ function callPlugin ( method , datas , breakValue ) {
368+
369+ for ( var i = 0 , len = this . _plugins . length ; i < len ; ++ i ) {
370+
371+ if ( ! ( method in this . _plugins [ i ] ) ) {
372+
373+ continue ;
374+
375+ }
376+
377+ var returnValue = this . _plugins [ i ] [ method ] . apply ( this , datas ) ;
378+
379+ if ( breakValue !== undefined && returnValue === breakValue ) {
380+
381+ return false ;
382+
383+ }
384+
385+ }
386+
387+ return true ;
388+
389+ }
390+
391+ waveform . prototype . addPlugin = function addPluginFunction ( plugin ) {
392+
393+ this . _plugins . push ( plugin ) ;
394+
395+ } ;
396+
397+ waveform . prototype . _plugins = null ;
367398
368399 var drawStop = false ;
369400 var frameHandle ;
@@ -378,11 +409,13 @@ define([
378409
379410 var that = this ;
380411
381- var frameHandle = requestFrame ( function ( ) {
412+ frameHandle = requestFrame ( function ( ) {
382413
383414 if ( ! drawStop ) {
384415
385- that . events . once ( that . events . constants . progressEvent , function ( range ) {
416+ that . events . once ( that . events . constants . progressEvent , function ( data ) {
417+
418+ var range = ( data . position / data . duration ) * 100 ;
386419
387420 // redraw wave with different color until song progress
388421 that . draw ( range ) ;
@@ -436,16 +469,24 @@ define([
436469 var that = this ;
437470
438471 // on play listen for progress
439- this . events . on ( this . events . constants . playEvent , function ( ) {
472+ this . events . on ( this . events . constants . playEvent , function waveformPlayerPlayListener ( isPlaying ) {
473+
474+ if ( isPlaying && callPlugin . call ( that , 'onPlay' , arguments , false ) ) {
440475
441- that . updateRangeStart ( ) ;
476+ that . updateRangeStart ( ) ;
477+
478+ }
442479
443480 } ) ;
444481
445482 // on stop, stop listening for progress
446- this . events . on ( this . events . constants . stopEvent , function ( ) {
483+ this . events . on ( this . events . constants . stopEvent , function waveformPlayerStopListener ( isPlaying ) {
447484
448- that . updateRangeStop ( ) ;
485+ if ( ! isPlaying ) {
486+
487+ that . updateRangeStop ( ) ;
488+
489+ }
449490
450491 } ) ;
451492
0 commit comments