AVAudioSessionInterruptionNotification not fired when Video/Camera is used

泄露秘密 提交于 2019-12-02 05:32:42

I used category and method swizzling and it works like charm.

#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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!