123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- //
- // AudioSessionObject.m
- // 隐私保护
- //
- // Created by xd h on 2023/10/24.
- //
- #import "AudioSessionObject.h"
- #import <AVFoundation/AVFoundation.h>
- #import <AudioToolbox/AudioToolbox.h>
- #import "DFPlayer.h"//这个播放音乐的第三方 这个播放静音会影响到音乐后台播放
- @interface AudioSessionObject()
- @property (strong, nonatomic) AVAudioPlayer *audioPlayer;
- @property (strong,nonatomic) NSTimer *timer;
- @property (nonatomic, assign) UIBackgroundTaskIdentifier backgrounTask;
- @end
- @implementation AudioSessionObject
- /// 创建单利
- + (AudioSessionObject *)shareManager{
- static AudioSessionObject *manager = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- manager = [[AudioSessionObject alloc]init];
- });
- return manager;
- }
- - (instancetype)init {
- self = [super init];
- if (self) {
- [self addNoti];
- [self creatAVAudioSessionObject];
- }
- return self;
- }
- - (void)addNoti {
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
-
- }
- - (void)appWillEnterForeground {
- NSLog(@"%@ appWillEnterForeground",NSStringFromClass([self class]));
- [self stopPlayAudioSession];
-
- [self.timer invalidate];
- _isBackgroundType = NO;
-
- // 恢复音频播放 AVAudioSessionCategoryPlayback 可以在开启静音后播放声音 但是 停止后台播放后 [[AVAudioSession sharedInstance] setActive: No error: nil] 后无声音了 AVAudioSessionCategorySoloAmbient 可以 但是开启静音后无声
-
- //[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
- //[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];//ok
- //[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil]; // ok
- //[[AVAudioSession sharedInstance] setActive: YES error: nil];
-
- [[AVAudioSession sharedInstance] setActive:YES error: nil];
- [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
-
- }
- - (void)appDidEnterBackground {
- NSLog(@"%@ appDidEnterBackground",NSStringFromClass([self class]));
-
- if([DFPlayer sharedPlayer].state == DFPlayerStateBuffering
- ||[DFPlayer sharedPlayer].state == DFPlayerStatePlaying){
- return;
- }
-
- _isBackgroundType = YES;
-
- BOOL isBackground = [HWDataManager getBoolWithKey:stringKeyAddSn(Const_file_Transfe_working_background)];
- if (!isBackground) {
- [self stopBackgroundActiveFun];
- return;
- }
-
- [[AVAudioSession sharedInstance] setActive: YES error: nil];
- [self startPlayAudioSession];
-
-
- //申请后台
- self.backgrounTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
- [[UIApplication sharedApplication] endBackgroundTask:self.backgrounTask];
- self.backgrounTask = UIBackgroundTaskInvalid;
- }];
-
- self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(applyToSystemForMoreTime) userInfo:nil repeats:YES];
- [self.timer setFireDate:[NSDate distantPast]];
- }
- /// 创建音乐播放器
- - (void)creatAVAudioSessionObject{
- //设置后台模式和锁屏模式下依然能够播放
- [[AVAudioSession sharedInstance] setActive: YES error: nil];
- [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
- //初始化音频播放器
- NSError *playerError;
- NSString*pathStr = [[NSBundle mainBundle] pathForResource:@"RunInBackground" ofType:@"mp3"];
-
- NSURL *urlSound11 = [[NSURL alloc] initFileURLWithPath:pathStr];
- // HLog(@"%@---%@",pathStr,urlSound11);
- //NSURL *urlSound = [[NSURL alloc]initWithString:pathStr];
- _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:urlSound11 error:&playerError];
- _audioPlayer.numberOfLoops = -1;//无限播放
- //_audioPlayer.volume = 0;
- }
- /// 开始播放声音
- - (void)startPlayAudioSession{
- NSLog(@"%@ startPlayAudioSession",NSStringFromClass([self class]));
- [_audioPlayer play];
- _isBackgroundType = YES;
- }
- /// 停止播放声音
- - (void)stopPlayAudioSession{
- NSLog(@"%@ stopPlayAudioSession",NSStringFromClass([self class]));
- [_audioPlayer stop];
- _isBackgroundType = NO;
- }
- - (void)stopBackgroundActiveFun
- {
- if(!_isBackgroundType){
- return;
- }
-
- [self stopPlayAudioSession];
- [self.timer invalidate];
- [[AVAudioSession sharedInstance] setActive:NO withOptions: AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error: nil];
- //[[AVAudioSession sharedInstance] setActive:NO error: nil];
- [[UIApplication sharedApplication] endBackgroundTask:self.backgrounTask];
- self.backgrounTask = UIBackgroundTaskInvalid;
- }
-
- #pragma mark - 定时器设置判断后台保活时间,如果将要被后台杀死,重新唤醒
- - (void)applyToSystemForMoreTime {
- //HLog(@"我还在后台运行 哈哈");
- // if ([UIApplication sharedApplication].backgroundTimeRemaining < 20.0) {//如果剩余时间小于30秒
- // [[UIApplication sharedApplication] endBackgroundTask:self.backgrounTask];
- // self.backgrounTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
- // [[UIApplication sharedApplication] endBackgroundTask:self.backgrounTask];
- // self.backgrounTask = UIBackgroundTaskInvalid;
- // }];
- // }
- }
- @end
|