Play audio file using AVAudioPlayer swift
<p>I am downloading the audio message first then play it using Avaudioplayer</p>

<pre><code>downloadTask = NSURLSession.sharedSession().downloadTaskWithURL(urlStr, completionHandler: { (URL, response, error) -&gt; Void in

           self.play(URL!)


})
downloadTask
</code></pre>

<p>This works fine, but if I call play(URL) method after saving the response URL that I have stored in NSUserDefaults. The URL is same in both cases. I have checked it. its something like:</p>

<blockquote>
  <p>file:///Users/mymac/Library/Developer/CoreSimulator/Devices/X-CD91-XXXXXXX-XXX-XXXXXX/data/Containers/Data/Application/XXXXXXX-XXXX-XXXXXXXXXX/tmp/CFNetworkDownload_InUTA3.tmp</p>
</blockquote>

<p>Passing this file url to:</p>

<pre><code>func play(url : NSURL) {

        do {
            player = try AVAudioPlayer(contentsOfURL: url)
            player.prepareToPlay()
            player.play()

        } catch let error as NSError {
            //self.player = nil
            print(error.localizedDescription)

        } catch {

            print("AVAudioPlayer init failed")
        }

    }
</code></pre>

<p>Url path is same, but the audioPlayer is unable to play the sound, it shows the following error:</p>

<blockquote>
  <p>The operation couldn’t be completed. (OSStatus error 2003334207.)</p>
</blockquote>

<p>Can anyone guide me in this regard?</p>

<p>Thanks.</p>