0

I currently run into a error with that bit of code since i updated Xcode

{!}Call can throw, but is not marked with 'try' and the error is not handled

   let soundURL = NSBundle.mainBundle().URLForResource("jump", withExtension: "caf")
            audioPlayer = AVAudioPlayer(contentsOfURL: soundURL!, fileTypeHint: nil)
            audioPlayer.numberOfLoops = -1
            audioPlayer.play()
Fabian Boulegue
  • 6,476
  • 14
  • 47
  • 72
  • Possible duplicate of [Issue with "try?" and AVAudioPlayer](http://stackoverflow.com/questions/32805153/issue-with-try-and-avaudioplayer) – Eric Aya Oct 24 '15 at 12:21
  • the problem is this answer did not work for my code? I did tried it but run in several errors – Fabian Boulegue Oct 24 '15 at 12:23
  • 1
    Possible duplicate of [Error-Handling in Swift-Language](http://stackoverflow.com/questions/24010569/error-handling-in-swift-language) – mixel Oct 24 '15 at 12:40

2 Answers2

2

The method is now throwable so you have handle it this way:

do {
    audioPlayer = try AVAudioPlayer(contentsOfURL: soundURL!, fileTypeHint: nil)
    audioPlayer.numberOfLoops = -1
    audioPlayer.play()
} catch {

}
joern
  • 27,354
  • 7
  • 90
  • 105
1

With Swift 2, you should be handling exceptions. Below code should fix your problem:

do {
    audioPlayer = try AVAudioPlayer(contentsOfURL: soundURL!, fileTypeHint: nil)
} catch (_) {

}
Abhinav
  • 37,684
  • 43
  • 191
  • 309
  • Please post a reason for down voting. I did not post complete code but surely pointed out the cause of error here. – Abhinav Oct 24 '15 at 12:26
  • 1
    This is a correct answer as it fixes the error you were getting. – joern Oct 24 '15 at 12:27
  • Indeed, this is a good answer in that it shows how to fix the problem without providing complete, drop-in code. (voted). – Duncan C Oct 24 '15 at 13:10