我开发了一个 iPhone 应用程序,可以播放不同的音乐文件。一切工作正常,除非来电中断播放。这会使应用程序崩溃并显示以下日志:
启动崩溃日志
Thread 0 Crashed:
0 libobjc.A.dylib 0x32d98f20 objc_msgSend + 20
1 AVFoundation 0x30739ddc AVAudioSessionPropertyListener(void*, unsigned long, unsigned long, void const*) + 284
2 AudioToolbox 0x36d78bec SSServer_AudioSessionInterruptionListenerMessage + 316
3 AudioToolbox 0x36d017bc _XAudioSessionInterruptionListenerMessage + 128
4 AudioToolbox 0x36c68584 mshMIGPerform + 224
5 CoreFoundation 0x3752f5c4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 22
6 CoreFoundation 0x3752f582 __CFRunLoopDoSource1 + 158
7 CoreFoundation 0x3752182e __CFRunLoopRun + 574
8 CoreFoundation 0x37521504 CFRunLoopRunSpecific + 220
9 CoreFoundation 0x37521412 CFRunLoopRunInMode + 54
10 GraphicsServices 0x33e76d1c GSEventRunModal + 188
11 UIKit 0x3591d574 -[UIApplication _run] + 580
12 UIKit 0x3591a550 UIApplicationMain + 964
结束崩溃日志
我已将其范围缩小到应用程序委托(delegate)中的 applicationWillResignActive 方法中的调用,但我似乎无法弄清楚,因为如果应用程序进入后台模式(通过按主页按钮),应用程序将正常工作,仅当接到电话时才会崩溃。
导致 applicationWillResignActive 方法崩溃的行是:
[navController popToRootViewControllerAnimated:NO];//navController是UINavigationController的一个实例;我使用此语句删除所有 View Controller ,当应用程序恢复时,我推送第一个 View Controller ,以便应用程序从头开始。
有人知道为什么只有在接到电话时才会发生此崩溃吗?
最佳答案
我们刚刚跟踪了一次非常类似的崩溃。
我们的结果如下所述:
https://github.com/mattgallagher/AudioStreamer/issues/6
特别是,MyAudioSessionInterruptionListener(或传递给的回调的名称) AudioSessionInitialize)并且它的 inClientData 在注册后就无法更改,因此即使底层对象已被释放,回调也必须始终执行一些明智的操作。
为 AudioStream 建议的解决方案是使用静态变量,并确保它指向当前对回调感兴趣的对象,并且永远不会指向已释放的对象 - 重要的是不要使用 inClientData。
关于使用 AVAudioPlayer 播放音频文件时 iPhone 崩溃且来电中断播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4647938/