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

私はXcode 7.2.1を使っていることに留意して下さい。第16行であなたがもしtryを使わなければ、あなたはエラー・メッセージCall can throw, but it is not marked with `try’ and the error is not handled.を受け取るでしょう。

try

私はエラー伝搬を止めるために、try!を使っています。

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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.