yzet00
yzet00

Reputation: 511

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:

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.

    }
}

Upvotes: 0

Views: 3611

Answers (3)

Jozey
Jozey

Reputation: 1740

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()

Upvotes: 1

Long Pham
Long Pham

Reputation: 7582

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!

Upvotes: 12

Lucas Azzopardi
Lucas Azzopardi

Reputation: 1181

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()

}

Upvotes: 0

Related Questions