简体   繁体   中英

AVAudioSession - catching setMode and setCategory errors

I'm using AVAudioSession in an app that only performs audio playback. I'm setting my audioSession's mode and category in applicationDidFinishLaunchingWithOptions: .

let audioSession = AVAudioSession.sharedInstance()

do {
   try audioSession.setMode(AVAudioSessionModeDefault)
   try audioSession.setCategory(AVAudioSessionCategoryPlayback)
}
catch {}

I never set a different mode or category during my app's lifetime, and I activate my audio session when the user taps a play button. In what scenario would either of these methods ( setMode:error: and setCategory:error ) throw an error? What should I expect to handle in my catch block?

A hypothetical (future?) device with no audio output might throw an invalid audio session category error. Or possibly the OS audio driver has crashed and can't be accessed. Perhaps inform the user that the device can't play audio for some unknown reason.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM