我有一些 SIP 应用程序。在我使用音频之前,只有一切正常,我在必要时收到 AVAudioSessionInterruptionNotification
。
使用视频时出现问题(接收和发送摄像头画面)。一旦我将 session 与视频一起使用,通知就再也不会触发,即使稍后仅使用音频也是如此。
我该如何解决?我找到了类似的 topic ,但答案是提示,我不完全明白。此外,我没有“相机/捕获设备”和“AVCaptureSession”,因为音频和视频流是由封闭的第三方库提供的,但我的代码必须处理中断。
我是否必须更改某些属性才能始终触发此通知(链接主题建议),或者我应该使用替代通知。
我一直在挖掘文档,但没有找到任何对我有用的东西。
我试过使用 AVCaptureSession
的虚拟对象,但这并没有解决问题。
编辑: 第三方库有一些额外的崩溃暴露了他们使用
AVCAptureSession
。我已联系他们要求更改属性 usesApplicationAudioSession
,如 other question 中所述并“乞求”他们修复它。经过长时间的斗争,他们同意了 :)。
最佳答案
我使用类别和 method swizzling它就像魅力一样。
#import "AVCaptureSession+MethodSwizzling.h"
#import <objc/runtime.h>
static void MethodSwizzle(Class c, SEL origSEL, SEL overrideSEL) {
Method origMethod = class_getInstanceMethod(c, origSEL);
Method overrideMethod = class_getInstanceMethod(c, overrideSEL);
if(class_addMethod(c, origSEL, method_getImplementation(overrideMethod), method_getTypeEncoding(overrideMethod))) {
class_replaceMethod(c, overrideSEL, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
} else {
method_exchangeImplementations(origMethod, overrideMethod);
}
}
@implementation AVCaptureSession (MethodSwizzling)
- (id)initMethodSwizzling {
self = [self initMethodSwizzling]; // it is not recursion it is method swizzling
self.usesApplicationAudioSession = NO;
return self;
}
+ (void)load {
if (class_getInstanceMethod(self, @selector(setUsesApplicationAudioSession:))) {
// Swizzle methods only when it is possible to change usesApplicationAudioSession property.
MethodSwizzle(self, @selector(init), @selector(initMethodSwizzling));
}
}
@end
关于ios - 使用视频/相机时未触发 AVAudioSessionInterruptionNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24205304/