title: Why doesn't AVAudioPlayer play any audio?
body: <p>At the moment I have two functions which are almost identical, but only one of them works and I can't figure out why. </p>
  
  <p>The playSound() function works and uses the class's audioPlayer: AVAudioPlayer object to play one audio file at a time. </p>
  
  <p>To make it so I could play multiple sounds simultaneously, I made a new function called playMySound() which creates its own instance of AVAudioPlayer instead of using the class's audioPlayer: AVAudioPlayer object.</p>
  
  <p>The weird thing is, the sound plays fine when I call playSound(), but it doesn't play when I call playMySound().</p>
  
  <p>Here's the code that works:</p>
  
  <pre><code>  var audioPlayer: AVAudioPlayer!
    override func viewDidLoad() {
        super.viewDidLoad()
        audioPlayer = AVAudioPlayer()
  
      }
  
    func playSound(audioFileName: String) {
      if let sound = NSDataAsset(name: audioFileName) {
        do {
          try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
          try! AVAudioSession.sharedInstance().setActive(true)
  
          try audioPlayer = AVAudioPlayer(data: sound.data)
          audioPlayer.delegate = self
          audioPlayer.play()
  
        } catch {
          print("Error playing sound: \(audioFileName)")
        }
      }
  
    }
  </code></pre>
  
  <p>And the code that doesn't work:</p>
  
  <pre><code>  func playMySound(audioFileName: String) {
      if let sound = NSDataAsset(name: audioFileName) {
        do {
          try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
          try! AVAudioSession.sharedInstance().setActive(true)
  
          var myAudioPlayer: AVAudioPlayer = AVAudioPlayer()
  
          myAudioPlayer = try AVAudioPlayer(data: sound.data)
          myAudioPlayer.delegate = self
  
          // Calling prepareToPlay() doesn't seem to fix it
          // myAudioPlayer.prepareToPlay()
  
          print("Trying to play my audio player")
          myAudioPlayer.play()
  
        } catch {
          print("Error playing sound: \(audioFileName)")
        }
      }
    }
  </code></pre>
  
  <p>"Trying to play my audio player" gets outputted to the console, so it's definitely running through the function, but for some reason calling myAudioPlayer.play() doesn't seem to work.</p>
  
  <p>Can anyone help? Thanks in advance!</p>