/ Published in: JavaScript
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
$( '#translate' ).bind( 'touchstart', function( e ) { var phrases = [ '1-great-presentation.mp3', '2-really-liked.mp3', '3-really-amazing.mp3', '4-dirty-thoughts.mp3', '5-have-baby.mp3' ]; $( '#response' ).empty(); $( '<source>' ) .attr( 'src', 'phrases/' + phrases[phrase] ) .appendTo( '#response' ); loaded = 0; document.getElementById( 'response' ).load(); if( phrase == 4 ) { phrase = 0; } else { phrase = phrase + 1; } } ); $( '#response' ).bind( 'progress', function( e ) { var audio = document.getElementById( 'response' ); var duration = audio.duration; var end = audio.buffered.end( 0 ); // Progress gets fired twice // Only want to play once if( loaded < 100 ) { loaded = parseInt( ( ( end / duration ) * 100 ) ) // console.log( loaded ); if( loaded == 100 ) { document.getElementById( 'response' ).play(); } } } );