Play audio file using AVFoundation

Play Audio File in Swift

This tutorial will show you how to play sound in Swift using the native AVFoundation framework.

    var player: AVAudioPlayer?
    func playSound(audioName: String) {

        let url = NSBundle.mainBundle().URLForResource(audioName, withExtension: "mp3")!
            player = try AVAudioPlayer(contentsOfURL: url)
            guard let player = player else { return }
        } catch let error as NSError {

Don’t forget to import AVFoundation in your class.

Leave a Reply

Your email address will not be published. Required fields are marked *