// import this at the app beginning to be able to play Audio
import AVFoundation</code>
class ViewController: UIViewController {
// declare the path to the audio file and the audio Player
var boing = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("bong", ofType: "wav")!)
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
// Delay Function == Pretty Cool :)
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time( DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), closure)
}
//Audio Function with the delay built in
func playAudio (time: Double) {
delay(time) {
self.audioPlayer = AVAudioPlayer(contentsOfURL: self.boing, error: nil) // point to the audio file
self.audioPlayer.prepareToPlay() // prepare the Audio Player
self.audioPlayer.play()
}
}
// Call the Audio and select the delay ( seconds )
playAudio(0.2)
playAudio(1.15)