AVAudioSession+RQExtension.m 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. //
  2. // AVAudioSession+RQExtension.m
  3. // SDJK
  4. //
  5. // Created by 张嵘 on 2021/11/26.
  6. //
  7. #import "AVAudioSession+RQExtension.h"
  8. #import <objc/runtime.h>
  9. @implementation AVAudioSession (RQExtension)
  10. + (void)load {
  11. static dispatch_once_t onceToken;
  12. dispatch_once(&onceToken, ^{
  13. Class class = [self class];
  14. SEL originalSEL = @selector(setActive:withOptions:error:);
  15. SEL swizzledSEL = @selector(lby_setActive:withOptions:error:);
  16. Method originalMethod = class_getInstanceMethod(class, originalSEL);
  17. Method swizzledMethod = class_getInstanceMethod(class, swizzledSEL);
  18. BOOL didAddMethod = class_addMethod(class, originalSEL , method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
  19. if (didAddMethod) {
  20. class_replaceMethod(class, swizzledSEL, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
  21. } else {
  22. method_exchangeImplementations(originalMethod, swizzledMethod);
  23. }
  24. });
  25. }
  26. - (BOOL)lby_setActive:(BOOL)active withOptions:(AVAudioSessionSetActiveOptions)options error:(NSError * _Nullable __autoreleasing *)outError {
  27. BOOL realActive = RQ_Video_Module.playerController.isPlaying ? YES : active;
  28. return [self lby_setActive:realActive withOptions:options error:outError];
  29. }
  30. @end