title: How can I play a sound from the asset catalog using AVAudioPlayerNode?
body: <p>I'm trying to use an <code>AVAudioPlayerNode</code> to play sounds from the <code>Assets.xcassets</code> asset catalog, but I can't figure out how to do it.</p>
  
  <p>I've been using <code>AVAudioPlayer</code>, which can be initialized with an <code>NSDataAsset</code> like this:</p>
  
  <pre><code>let sound = NSDataAsset(name: "beep")!
  do {
      let player = try AVAudioPlayer(data: sound.data, fileTypeHint: AVFileTypeWAVE)
      player.prepareToPlay()
      player.play()
  } catch {
      print("Failed to create AVAudioPlayer")
  }
  </code></pre>
  
  <p>I want to use an <code>AVAudioPlayerNode</code> instead (for pitch shifting and other reasons). I can create the engine and hook up the node OK:</p>
  
  <pre><code>var engine = AVAudioEngine()
  func playSound(named name: String) {
      let mixer = engine.mainMixerNode
      let playerNode = AVAudioPlayerNode()
      engine.attach(playerNode)
      engine.connect(playerNode, to: mixer, format: mixer.outputFormat(forBus: 0))
  
      // play the file (this is what I don't know how to do)
  }
  </code></pre>
  
  <p>It looks like the method to use for playing the file is <code>playerNode.scheduleFile()</code>. It takes an <code>AVAudioFile</code>, so I thought I'd try to make one. But the initializer for <code>AVAudioFile</code> wants a URL. As far as I can tell, assets in the asset catalog are not available by URL. I can get the data directly using <code>NSDataAsset</code>, but there doesn't seem to be any way to use it to populate an <code>AVAudioFile</code>.</p>
  
  <p>Is it possible to play sounds from the asset catalog with an <code>AVAudioPlayerNode</code>? And if so, how?</p>