title: How to play audio from an http data stream in swift
body: <p>I have an audio data stream coming in from a http response. I receive packets of bytes using the <strong>URLSessionDataDelegate</strong> method: </p>
  
  <p><code>urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data)</code></p>
  
  <p>I have successfully played the audio after appending all the data packets into a single Data object, using an <code>AVAudioPlayer</code> object and it's <code>initWithData:</code> initializer method. </p>
  
  <p>What I really want to do is start audio playback while data is still coming in - <em>streaming audio</em> effectively. I haven't seen any answers that seem elegant for this use-case. </p>
  
  <p>Options I've seen are:</p>
  
  <ol>
  <li>Using the AudioToolbox: Audio File Stream Services &amp; Audio Queues</li>
  <li>Using the NSStream API, writing to a file and playing audio from that file concurrently</li>
  </ol>
  
  <p>How would I achieve audio streaming playback from the Data packets coming in? </p>