Thomas Royal

Composer | Pianist | Technologist

Writings


Playing Sounds in Swift using AVAudioPlayer

June 03, 2014

Here is how I was able to play a sound in Swift. This information is current as of June 3, 2014. This code comprises the entirety of a Swift command-line application.

import AVFoundation

let filePath:NSURL = NSURL(fileURLWithPath: "*****HERE IS THE PATH TO YOUR FILE*****")
var er:NSError?
let audioPlayer:AVAudioPlayer = AVAudioPlayer(contentsOfURL: filePath, error: &er)


if er {
    println("There was an error: \(er)")
} else {
    println("playing...")
    audioPlayer.play()
    while audioPlayer.playing{}
}

Other methods, I don't know.