AVFoundationでのBGMの再生について(Swift)
import UIKit
import AVFoundation
class ViewController: UIViewController, AVAudioPlayerDelegate {
var audioPlayer:AVAudioPlayer!
@IBOutlet var button:UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let audioPath = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sample", ofType: "mp3")!)
//audioPlayer = AVAudioPlayer(contentsOfURL:audioPath)
audioPlayer.delegate = self
audioPlayer.prepareToPlay()
}
@IBAction func buttonTapped(sender : AnyObject) {
if ( audioPlayer.playing ){
audioPlayer.stop()
button.setTitle("Stop", forState: UIControlState.Normal)
}
else{
audioPlayer.play()
button.setTitle("Play", forState: UIControlState.Normal)
}
}
}
コメントアウトの部分で「Call can throw, but it is not marked with 'try' and the error is not handled」というエラーが出てしまいます。どなたか解決方法をお教えください。