Return to Snippet

Revision: 52897
at November 4, 2011 07:32 by parkerkrhoyt


Initial Code
$( '#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();      
    }      
  }
} );

Initial URL


Initial Description


Initial Title
Play an Audio File with the HTML Audio Tag

Initial Tags
file, html, web

Initial Language
JavaScript