title: Capture loaded source of audio tag, using Ruby on Rails
body: <p>I need to save the currently-loaded source file of an audio tag. Sounds simple, but here's the catch: the source gives a random sound file on every request.</p>
  
  <p>The audio tag is created, the source set, and the audio played with JavaScript, as seen here:</p>
  
  <pre><code>function createAudio() {
      var audio = document.createElement('audio');
  
      audio.setAttribute('id', 'file_audio')
      audio.setAttribute('controls', 'controls');
      audio.setAttribute('autoplay', 'true');
      audio.setAttribute('hidden', 'true');
      audio.appendChild(createSource());
      return audio;
  }
  
  function createSource() {
      var source = document.createElement('source');
      var d = new Date();
      source.setAttribute('id', 'file_audio_source')
      source.setAttribute('src', 'file.wav?r=' + d.getTime());
      source.setAttribute('type', 'audio/wav');
      return source;
  }
  
  this.switchAudio = function() {
      var d = new Date();
      $svjq("#file_audio").find('audio').remove();
      $svjq("#file_audio").find('source').remove();
      $svjq("#file_audio").find('embed').remove();
      if (Modernizr.audio.wav) {
          document.getElementById("file_audio").appendChild(createAudio());
      } else {
          $svjq("#file_audio").append('&lt;embed id="file_audio_embed" name="file_audio_embed" src="file.wav?r=' + d.getTime() + '" autostart="true" cache="false" type="audio/wav" hidden="true" loop="false" enablejavascript="true"&gt;');
      }
  
  };
  
  this.playAgain = function() {
      if (Modernizr.audio.wav) {
          document.getElementById('file_audio').play();
      } else {
          document.getElementById('file_audio_embed').play();
      }
  };
  </code></pre>
  
  <p>I need be able to save the currently-loaded file in the source. However, if you access the file URL in the browser it returns a different file. </p>
  
  <p>Automated processes such as Watir-WebDriver, Capybara (Capybara-Webkit), and Mechanize also return a random file. For example:</p>
  
  <pre><code>require 'capybara'
  session = Capybara::Session.new(:selenium)
  session.visit('url')
  session.click_link 'play sound' #on every click u get a new sound
  session.click_link 'play again'
  
  #file_audio_source
  e = session.find_by_id('file_audio_source')
  e[:src]
  
  #save the current open page and opens it
  #session.save_and_open_page
  
  #returns different file
  session.visit(e[:src])
  
  #returns different file
  session.execute_script("window.open('"+e[:src]+"')")
  
  require 'Mechanize' 
  agent = Mechanize.new{|agent| agent.ssl_version, agent.verify_mode = 'SSLv3', OpenSSL::SSL::VERIFY_NONE}
  
  filedata = agent.get(e[:src]).content
  aFile = File.new("/Users/me/Documents/test/test111.wav", 'wb')
  #aFile.syswrite(filedata)
  </code></pre>
  
  <p>Could the file be embedded into the HTML or cached? And is there a way to get the file and save it locally?</p>
  
  <p>Other options include recording from the sound device or using the mic to record the sound played, though this option is not at all ideal.</p>