title: iOS Safari issue with Audio from Server
body: <p>So currently working on a project and experiencing a strange issue with the ios version of Safari involving the playback of an audio file from a server. </p>
  
  <p>I'm currently facing the following issue:</p>
  
  <ol>
  <li>Person comes onto the page which has a standard html5 audio tag, and a direct link to the audio file for downloading purposes.</li>
  <li>Person tries to listen to audio from audio tag, content plays for x number of minutes, cuts off then repeats (the x number of minutes is NOT the length of the recording, and is not consistent).</li>
  <li>Person tries direct link of recording, rather then downloading the recording, Safari appears to go to a new page and wraps the download url in a video element, and the same issue as step 2 occurs.</li>
  </ol>
  
  <p>Now the audio file is served up via a java scriptlet, which serves the file with the following code snippet:</p>
  
  <pre><code>String fn = saveTo + file_name;
  f = new File(fn);
  
  String fname = f.getName();
  String contentType = "audio/wav";
  if(fname.endsWith("mp3")){
      contentType = "audio/mp3";
  }
  
  response.setContentType(contentType);
  response.setHeader("Content-Transfer-Encoding", "binary");
  response.setHeader("Content-disposition", "attachment;filename="+f.getName());
  response.setHeader("Content-Length", ""+f.length());
  
  FileInputStream fin = null;
  
  try{
      fin = new FileInputStream(f.getCanonicalFile());
  
      byte[] data = new byte[1024];
      int x = 0;
  
      while((x = fin.read(data, 0, 1024))&gt;=0){
          response.getOutputStream().write(data, 0, x);
          Thread.sleep(1);
      }
  } finally {
      if(fin != null) {
          try{
              fin.close();
          }catch(Exception ex){}
      }
  }
  </code></pre>
  
  <p>Now I know the code isn't the best by any measure, it isn't my code, and we're obviously working on the assumption that the file is found. </p>
  
  <p>I'm finding when debugging on the iPhone with debug mode on a mac, it doesn't seem to show a return status code. It shows no response headers but it obviously must be receiving something. The server log seems to think its returning a status 200, this showing in Chrome and Firefox.</p>
  
  <p>The code above appears to work fine with Chrome, and Firefox, but not Safari.</p>
  
  <p>The only thing I am guessing is it has something to do with how the file is being pushed to the output stream that Safari isn't liking, or maybe its getting confused and should have a different status code. I've been banging my head against this for a good few days, and reading as much as I can about Safari, though most of the documentation I'm finding is on its "unique" implementation of web audio, and the use of a single channel which seems to be irrelevant to this.</p>
  
  <p>Any Help would be appreciated.</p>