저는 이것에 상당히 익숙하며 제목에서 오류를 해결하기위한 올바른 형식을 찾으려고합니다. 내가 줄을 : audioPath = NSBundle.mainBundle(). pathForResource ("Pugs.m4a", ofType : nil)하자!오류 : 치명적인 오류 : 예기치 않게 찾지 못했습니다. 선택 값을 래핑하지 않았습니다.
내가 어디에서 확실하지 않은 것을 놓치고 있어야한다는 것을 알고 있습니다.
수입 UIKit 수입 AVFoundation
클래스의 ViewController :의 UIViewController {
@IBOutlet var playButton: UIButton!
var playPug = 1
var player: AVAudioPlayer!
@IBAction func playPressed(sender: AnyObject) {
let audioPath = NSBundle.mainBundle().pathForResource("Pugs.m4a", ofType: nil)!
let url = NSURL(fileURLWithPath: audioPath)
do {
if playPug == 1 {
let sound = try AVAudioPlayer(contentsOfURL: url)
player = sound
sound.play()
playPug = 2
playButton.setImage(UIImage(named:"pause_Icon.png"),forState:UIControlState.Normal)
} else {
player.pause()
playPug = 1
playButton.setImage(UIImage(named:"play_Icon.png"),forState:UIControlState.Normal)
}
} catch {
print(error)
}
}
Apple의 문서를 보면이 메소드가'init (contentsOfURL url : NSURL)'이고 Swift 2 오류 처리를 사용함을 알 수 있습니다. 문서는 언제나 가장 먼저보아야합니다. 'AVAudioPlayer' 문서에 링크 : https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/#//apple_ref/occ/instm/AVAudioPlayer/initWithContentsOfURL:error :. Swift Error Handling 문서에 링크 : https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html –
감사합니다. init (contentsOfURL url : NSURL) 메소드 예제가 있습니다. 나는 아직도 그것을 바꾸는 방법을 확신하지 못한다. 내가 전에 "던져"본 적이있다 .. – Lou
그래서 자습서를위한 좋은 장소가 아닙니다. 신속한 오류 처리에 대한 자습서를 인터넷으로 검색하는 것이 좋습니다. 예 : https://www.hackingwithswift.com/new-syntax-swift-2-error-handling-try-catch –