User:R4wrz0rz0r/audio.js

From Team Fortress Wiki
Jump to: navigation, search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/* Script to play audio files with the <audio> tag */

function audioInit() {
    var audioTest = document.createElement( 'audio' ); // create a new audio element
        if( audioTest.canPlayType( 'audio/wav; codecs="1"' ) == 'probably' ) { // take no chances with 'maybe'
            var links = document.getElementsByTagName( 'a' ), // find all links
            audioLinks = [], // we'll put all the audio links in here
            i = 0, // integer for loop
            linksLength = links.length; // how many links there are to check
                while( i < linksLength ) {
                    if( links[ i ].attributes[ 'href' ].substr( -4 ) == '.wav' ) // if the linked URI ends with .wav...
                        audioLinks.push( links[i] ); // add it to audioLinks
                    i++;
                }
                if( !!audioLinks ) { // if there are any .wav links
                    var audioLinksLength = audioLinks.length, // how many audio links there are
                    audioElements = [], // all audio elements
                    j = 0, // integer for loop
                    k = 0; // integer for loop
                        while( j < audioLinksLength ) {
                            audioElements.push( document.createElement( 'audio' ).setAtribute( 'src', audioLinks[ j ] ).setAttribute( 'autobuffer', 'true' ).setAttribute( 'preload', 'true').addEventListener( 'click', playAudio( audioElements[j] ) ) ) // create a new audio element, add attributes + event listener, and put it in the array
                            j++;
                        }
                        function playAudio( audioElement ) {
                            k = 0;
                                while( k < audioLinksLength ) { // this will stop all audio clips that are currently playing
                                    audioElements[ k ].
/*************************/
/*   WORK IN PROGRESS    */
/*    CHECK BACK SOON    */
/*  REWRITE WILL HAPPEN  */
/*************************/