Playing audio in Xcode 7 / Swift 2 Not Working

I am currently on Xcode 7 beta 4 with Swift 2, trying to play audio. I do not get any error in my code, but when I press the play button, the audio does not play. I cant figure out what is wrong.

import UIKit
import AVFoundation

class VesuviusViewController: UIViewController {

    @IBOutlet weak var PausePlay: UIButton!

    func playVes() {
        do {

        _ = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ves", ofType: "m4a")!))

        } catch {
            print("Error")
        }
    }

    @IBAction func playButtonPressed(sender: AnyObject) {
        playVes()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.

    }
}

UPDATE
Correct code:

  • Is Xcode debugging/crash report in Swift broken?
  • Xcode 7 GM can't commit (GIT)
  • Xcode 7.1 beta 2- Disable ATS
  • Enabling Bitcode for iOS 9 increases IPA size 3x, is this the size on the app store?
  • Error after updating to Xcode 7
  • How to check if a framework is BITCODE supported for Xcode7
  • import UIKit
    import AVFoundation
    
    class VesuviusViewController: UIViewController {
    
    
    @IBOutlet weak var PausePlay: UIButton!
    
    
    
    @IBAction func playPressed(sender: AnyObject) {
        VesSet()
    }
    var audioPlayer: AVAudioPlayer!
    
        func VesSet(){
    
    
            do {
                self.audioPlayer =  try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ves", ofType: "m4a")!))
                self.audioPlayer.play()
    
            } catch {
                print("Error")
            }
    
    
        }
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
    
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
    
        }
    }
    

    3 Solutions Collect From Internet About “Playing audio in Xcode 7 / Swift 2 Not Working”

    Try this:

    var audioPlayer: AVAudioPlayer! // Global variable
    

    And play method:

    func playVes() {
            do {
                self.audioPlayer =  try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Ves", ofType: "m4a")!))
                self.audioPlayer.play()
    
            } catch {
                print("Error")
            }
        }
    

    Hope this helps!

    Current code working as of xCode 7.0 Swift 2.0
    Still working as of October 9, 2015

    //Declaration
    var hitSoundPlayer: AVAudioPlayer?
    
    //Added into the didMoveToView(view: SKView) Function
    do {
        hitSoundPlayer = try AVAudioPlayer(contentsOfURL: hitSound)
            hitSoundPlayer!.prepareToPlay()
    } catch _ {
            hitSoundPlayer = nil
    }
    
    //When you want to play it
    hitSoundPlayer!.play()
    

    Not sure why this happening to you but there are multiple ways of implementing sound into your project. Try this way:

    class Hello : UIViewController {
    
      var Audio1 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("mySound", ofType: "wav")!)
      var AudioPlayer = AVAudioPlayer()
    
    override func viewDidLoad() {
    
      AudioPlayer = AVAudioPlayer(contentsOfURL: Audio1, error: nil)
    }
    
    func playsound() {
    
      AudioPlayer.play()
    
    }