javascript - Play/pause using howler.js with meteor framework -


okay, i'm trying let users play/pause when click on gif once or twice. have set user play sound once without stopping it.

i'm using javascript audio library howler.js , meteor framework.

below code:

template.gif.rendered = function () {     freezeframe_options = {     trigger_event: "click" };  $.getscript("/client/scripts/freezeframe.js", function () {     $(".gif").click(function () {         if (!$(this).hasclass('played')) {             var gifid = $(this).attr("data-gif-id");  // return gif id number             var soundfile = $(this).attr("data-sound-file"); // return sound file              var fileformat = "mp3";             var mp3test = new audio();             var canplaymp3 = (typeof mp3test.canplaytype === "function" && mp3test.canplaytype("audio/mpeg") !== "");             if (!canplaymp3) {                 fileformat = "ogg";             }              var sound = new howl({                 urls: ['sounds/' + soundfile + '.' + fileformat]             }).play();              $(this).addclass('played');         }         ;     });   });  }; 

i'm using few classes track current playback state:

  • playing = sound being played
  • paused = sound paused
  • played = sound has been listened @ least once

i've created howlers object store howl instances, keyed off of data-gif-id (so key data-gif-id , value howl object). if data-gif-id key not in howlers object, create new howl object, otherwise call play() , pause() methods on corresponding value in howlers object.

here code:

template.gif.rendered = function () {   freezeframe_options = {     trigger_event: "click"   };    howlers = {}; // set object hold howl instances    // moved these static lines out of click function   var fileformat = "mp3";   var mp3test = new audio();   var canplaymp3 = (typeof mp3test.canplaytype === "function" && mp3test.canplaytype("audio/mpeg") !== "");   if (!canplaymp3) {     fileformat = "ogg";   }    $.getscript("/client/scripts/freezeframe.js", function () {     $(".gif").click(function () {       var e = $(this);       var soundfile = e.attr("data-sound-file") + '.' + fileformat; // return sound file       var gifid = e.attr("data-gif-id");  // return gif id number       if (gifid in howlers){         if (e.hasclass('paused')){ // if paused, unpause           e.removeclass('paused');           e.addclass('playing');           howlers[gifid].play();         } else if (e.hasclass('playing')) {  // if playing, pause           e.removeclass('playing');           e.addclass('paused');           howlers[gifid].pause();         } else { // if not playing , not paused, play           e.addclass('playing');           howlers[gifid].play();         }       } else { // new instance, add howlers object , start playing         howlers[gifid] = new howl({           urls: ['sounds/' + soundfile],           onend: function(){ // when playing ends, add 'played' class track sounds have been played             e.removeclass('playing');             e.addclass('played');           }         });         e.addclass('playing');         howlers[gifid].play();       }     });   });  }; 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -