Ios swift\u尝试播放音频时出现意外错误

Ios swift\u尝试播放音频时出现意外错误,ios,swift,error-handling,try-catch,Ios,Swift,Error Handling,Try Catch,因此,我使用此函数设置AVAudioPlayer: func setupAudioPlayerWithFile(file: String) -> AVAudioPlayer? { var audioPlayer: AVAudioPlayer? if let sound = NSDataAsset(name: file) { do { try! AVAudioSession.sharedInstance().setCategory(AV

因此,我使用此函数设置AVAudioPlayer:

func setupAudioPlayerWithFile(file: String) -> AVAudioPlayer? {
    var audioPlayer: AVAudioPlayer?
    if let sound = NSDataAsset(name: file) {
        do {
            try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
            try! AVAudioSession.sharedInstance().setActive(true)
            try audioPlayer = AVAudioPlayer(data: sound.data, fileTypeHint: AVFileTypeWAVE)
        } catch {
            print("error initializing AVAudioPlayer")
        }
    }
    return audioPlayer
}
但我从用户那里收到了数百起报告的崩溃。我无法复制任何崩溃

碰撞发生在以下两条线路上:

try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
try! AVAudioSession.sharedInstance().setActive(true)

有时它在第一行崩溃,有时在第二行崩溃。我该如何解决这个问题?是什么导致了这些崩溃?

事实上,我不知道导致崩溃的原因是什么,但是为了防止它们,你应该更换
试试
do
块中尝试
,让
捕获
,以便能够处理任何预期错误。目前,
do catch
只处理
try audioPlayer=AVAudioPlayer
try!音频会话
试试!如果发生错误,音频会话应该会导致崩溃

更清楚的是,考虑下面的例子:

enum FirstError: Error {
    case FirstError
}

func throwFirstErrorFunction() throws {
    throw FirstError.FirstError
}

enum SecondError: Error {
    case SecondError
}

func throwSecondErrorFunction() throws {
    throw SecondError.SecondError
}
案例1:

try! throwFirstErrorFunction() // crash
do {
    try throwFirstErrorFunction()
    //try throwSecondErrorFunction()
} catch (let error) {
    print(error) // FirstError
}
do {
    try! throwFirstErrorFunction() // crash
    try throwSecondErrorFunction()
} catch (let error) {
    print(error)
}
do {
    try throwSecondErrorFunction()
    try throwFirstErrorFunction()
} catch (let error) {
    print(error) // SecondError
}
应用程序应该会崩溃

案例2:

try! throwFirstErrorFunction() // crash
do {
    try throwFirstErrorFunction()
    //try throwSecondErrorFunction()
} catch (let error) {
    print(error) // FirstError
}
do {
    try! throwFirstErrorFunction() // crash
    try throwSecondErrorFunction()
} catch (let error) {
    print(error)
}
do {
    try throwSecondErrorFunction()
    try throwFirstErrorFunction()
} catch (let error) {
    print(error) // SecondError
}
它应该打印
FirstError

案例3(你面临的是什么):

try! throwFirstErrorFunction() // crash
do {
    try throwFirstErrorFunction()
    //try throwSecondErrorFunction()
} catch (let error) {
    print(error) // FirstError
}
do {
    try! throwFirstErrorFunction() // crash
    try throwSecondErrorFunction()
} catch (let error) {
    print(error)
}
do {
    try throwSecondErrorFunction()
    try throwFirstErrorFunction()
} catch (let error) {
    print(error) // SecondError
}
应用程序应该会崩溃,为什么?因为
do catch
只处理
try
而不处理
try

案例4(解决方案):

try! throwFirstErrorFunction() // crash
do {
    try throwFirstErrorFunction()
    //try throwSecondErrorFunction()
} catch (let error) {
    print(error) // FirstError
}
do {
    try! throwFirstErrorFunction() // crash
    try throwSecondErrorFunction()
} catch (let error) {
    print(error)
}
do {
    try throwSecondErrorFunction()
    try throwFirstErrorFunction()
} catch (let error) {
    print(error) // SecondError
}
它应该打印
SecondError
。请注意,第一个捕获到的错误将由
catch
块处理,而另一个
try
s应跳过

另外,


我建议您检查您不应该使用
试试如果要将代码包装在
do
块中。只要使用
试试
。是的,这是问题的一半,但我也想知道是什么导致了这些崩溃。