AVAudioPlayer

//
//  AppDelegate.swift
//

import Cocoa
import AVFoundation

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!
    
    func applicationDidFinishLaunching(aNotification: NSNotification) {
        let audioPath = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("test", ofType: "wav")!)
        let player : AVAudioPlayer
        player = try! AVAudioPlayer(contentsOfURL: audioPath, fileTypeHint: nil)
        player.prepareToPlay()
        player.play()
    }
}

Note that I am using Xcode 7.2.1. At line 16, if you do not use try, you will get the error message, Call can throw, but it is not marked with `try’ and the error is not handled.

try

I am using try! to disable error propagation.

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html

trybang

Leave a Reply

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