AudioSessionObject.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // AudioSessionObject.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2023/10/24.
  6. //
  7. #import "AudioSessionObject.h"
  8. #import <AVFoundation/AVFoundation.h>
  9. #import <AudioToolbox/AudioToolbox.h>
  10. @interface AudioSessionObject()
  11. @property (strong, nonatomic) AVAudioPlayer *audioPlayer;
  12. @property (strong,nonatomic) NSTimer *timer;
  13. @property (nonatomic, assign) UIBackgroundTaskIdentifier backgrounTask;
  14. @end
  15. @implementation AudioSessionObject
  16. /// 创建单利
  17. + (AudioSessionObject *)shareManager{
  18. static AudioSessionObject *manager = nil;
  19. static dispatch_once_t onceToken;
  20. dispatch_once(&onceToken, ^{
  21. manager = [[AudioSessionObject alloc]init];
  22. });
  23. return manager;
  24. }
  25. - (instancetype)init {
  26. self = [super init];
  27. if (self) {
  28. [self addNoti];
  29. [self creatAVAudioSessionObject];
  30. }
  31. return self;
  32. }
  33. - (void)addNoti {
  34. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
  35. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
  36. }
  37. - (void)appWillEnterForeground {
  38. NSLog(@"%@ appWillEnterForeground",NSStringFromClass([self class]));
  39. [self stopPlayAudioSession];
  40. [self.timer invalidate];
  41. }
  42. - (void)appDidEnterBackground {
  43. NSLog(@"%@ appDidEnterBackground",NSStringFromClass([self class]));
  44. [self startPlayAudioSession];
  45. self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(applyToSystemForMoreTime) userInfo:nil repeats:YES];
  46. [self.timer setFireDate:[NSDate distantPast]];
  47. }
  48. /// 创建音乐播放器
  49. - (void)creatAVAudioSessionObject{
  50. //设置后台模式和锁屏模式下依然能够播放
  51. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
  52. [[AVAudioSession sharedInstance] setActive: YES error: nil];
  53. //初始化音频播放器
  54. NSError *playerError;
  55. NSURL *urlSound = [[NSURL alloc]initWithString:[[NSBundle mainBundle]pathForResource:@"RunInBackground" ofType:@"mp3"]];
  56. _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:urlSound error:&playerError];
  57. _audioPlayer.numberOfLoops = -1;//无限播放
  58. _audioPlayer.volume = 0;
  59. }
  60. /// 开始播放声音
  61. - (void)startPlayAudioSession{
  62. NSLog(@"%@ startPlayAudioSession",NSStringFromClass([self class]));
  63. [_audioPlayer play];
  64. }
  65. /// 停止播放声音
  66. - (void)stopPlayAudioSession{
  67. NSLog(@"%@ stopPlayAudioSession",NSStringFromClass([self class]));
  68. [_audioPlayer stop];
  69. }
  70. #pragma mark - 定时器设置判断后台保活时间,如果将要被后台杀死,重新唤醒
  71. - (void)applyToSystemForMoreTime {
  72. if ([UIApplication sharedApplication].backgroundTimeRemaining < 30.0) {//如果剩余时间小于30秒
  73. [[UIApplication sharedApplication] endBackgroundTask:self.self.backgrounTask];
  74. self.backgrounTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
  75. [[UIApplication sharedApplication] endBackgroundTask:self.self.backgrounTask];
  76. self.backgrounTask = UIBackgroundTaskInvalid;
  77. }];
  78. }
  79. }
  80. @end