Error playing audio

Asked 2 years ago, Updated 2 years ago, 31 views

When I try to play the data sample.mp3, I get the error "Insufficient Arguments".If you know, please let me know.

override func viewDidLoad(){
    super.viewDidLoad()

    let sound_data = NSURL (fileURLWithPath: NSBundle.mainBundle().pathForResource("sample", ofType:"mp3")!)
    varaudioPlayer:AVAudioPlayer=AVAudioPlayer(contentsOfURL:sound_data, error:nil) // This is where the error occurs.
    audioPlayer.play()
}

swift

2022-09-30 15:34

1 Answers

Swift2 introduced new syntaxes such as try, catch, throw, which greatly changed the way error handling works.

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42-ID508

At the same time, the NSError argument disappears because the method of taking the NSError double pointer is now automatically converted to throws.Instead, you must invoke the method with try.

let sound_data=NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource("sample", ofType:"mp3")!)
do{
    let audioPlayer=try AVAudioPlayer(contentsOfURL:sound_data)
    audioPlayer.play()
} catchlet error as NSError {
    print(error)
}


2022-09-30 15:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.