title: Swift : Player audio file from another class file
body: <p>First of all, I just want to let you know I'm a newbie in iOS programming ;)</p>
  
  <p>I would like to know if the following scenario is possible or which way I need to update my code to make it work.
  I would like to play an audio file located in the <strong>player class file</strong> within the <strong>ViewController file</strong>.
  I added the file "test.wav" to the root of project.</p>
  
  <h2>My problem</h2>
  
  <p>When I play the sound by tapping a button, the program can't find the sound file. I have the following error message: 
  <strong>fatal error: unexpectedly found nil while unwrapping an Optional value
  (lldb)</strong></p>
  
  <h2>Main swift file: ViewController.swift</h2>
  
  <pre><code>import UIKit
  import AVFoundation
  
  class ViewController: UIViewController {
  
      var player1: Player!
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
      }
  
      @IBAction func playAudio(sender: AnyObject) {
          player1.playAudioFile()
      }
  
  }
  </code></pre>
  
  <h2>Class file: Player.swift</h2>
  
  <pre><code>import Foundation
  
  import AVFoundation
  
  class Player {
  
  // Variables
  
      var vc: ViewController!
      var audioFile: AVAudioPlayer!
  
  
  // Initializer
  
      init (){
  
      }
  
  // Methods
  
      func playAudioFile() {
  
          if audioFile.playing {
              audioFile.stop()
          } else {
              audioFile.play()
          }
      }
  
  // Intialize ViewController
  
      init (vc:ViewController!) {
  
          // Set path for the attack sound
  
          let audioSnd = NSBundle.mainBundle().pathForResource("test", ofType: "wav")
          let audioFileURL = NSURL(fileURLWithPath: audioSnd!)
  
          do {
  
              try audioFile = AVAudioPlayer(contentsOfURL: audioFileURL, fileTypeHint:  nil)
              audioFile.prepareToPlay()
  
          } catch let err as NSError {
              print(err.debugDescription)
          }
      }
  }
  </code></pre>
  
  <h2>More information</h2>
  
  <p>My program has a class "Player", with subclasses such as "Human" and "Monster".
  By default, my class player has some audio files for attacks, dying, etc. 
  Under some conditions, the "player" can become a human or a monster and get custom attack and dying sounds.</p>
  
  <p>Thanks a lot for your help! ;)</p>