title: How to play NSData from Core Data in AVAudioPlayer
body: <p>I save mp3 file into Core Data as NSData binary data. Then I load this file with help of <code>NSFetchedResultsController</code>. Then I wrote following code but I got the error. How can I fix it?  </p>
  
  <blockquote>
    <p>fatal error: unexpectedly found nil while unwrapping an Optional value</p>
  </blockquote>
  
  <pre><code>var audioPlayer = AVAudioPlayer()
  func play() {
      if musicData != nil {
          audioPlayer = AVAudioPlayer(data: musicData, error: nil)
          AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
          AVAudioSession.sharedInstance().setActive(true, error: nil)
          audioPlayer.prepareToPlay()
          audioPlayer.play()
      }
  }
  </code></pre>
  
  <p><strong>UPDATE</strong>
  I checked it and I saw that <code>AVAudioPlayer</code> is nil but I init this player in beginning of this class.</p>
  
  <p>Still I changed code but I get the same error
  The second method</p>
  
  <pre><code>var av = AVAudioPlayer()
  func play() {
      let object = fetchedResultsController.fetchedObjects?.first as! Entity
      let songData = object.mp3
  
      var urlData: NSURL!
      songData.writeToURL(urlData, atomically: true)
      av = AVAudioPlayer(contentsOfURL: urlData, error: nil)
      av.prepareToPlay()
      av.play()
  }
  </code></pre>