后台播放无声音乐保活

目前iOS 13.0以上, 后台常驻时间很短, 一般只有30s时间, 这给用户造成不好的体验。尤其是,你在编辑文章的时,突然来个电话,当你电话结束的时候,你的APP已经被后台kill掉,编辑的文章内容被清空。 当然,你也可以给APP设计草稿箱功能,这个显然会增加你的工作量。所以,后台保活,相比较起来说,一个成本较低的方案。后台保活的方案用的较多有:

1.播放无声音乐
2.请求位置定位
3.Voip

目前我们项目中, 使用的是播放无声音乐,具体项目代码如下:
OFAudioManager.h文件

#import 

NS_ASSUME_NONNULL_BEGIN



@interface OFAudioManager : NSObject
/// 单例
+ (instancetype)sharedIntance;
/// 是否开启后台自动播放无声音乐
@property (nonatomic, assign) BOOL  openBackgroundAudioAutoPlay;
@end

NS_ASSUME_NONNULL_END

OFAudioManager.m文件

#import "OFAudioManager.h"
@interface OFAudioManager()
/// 播放会话
@property (nonatomic, strong) AVAudioSession * audioSession;
/// 背景播放器
@property (nonatomic, strong) AVAudioPlayer * backgroundAudioPlayer;
/// 后台运行时间
@property (nonatomic, assign) NSInteger  backgroundTimeLength;
/// timer
@property (nonatomic, strong) NSTimer * timer;
@end

@implementation OFAudioManager
// MARK: life cycle
- (void)dealloc{
    REMOVE_NOTIFY;
}

#pragma mark --------Notifications--------
+ (instancetype)sharedIntance{
    static OFAudioManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[OFAudioManager alloc] init];
    });
    return instance;
}

// MARK: app进入后台
- (void)applicationDidEnterBackground:(NSNotification *)notify{
    [self startTimer];
    if (self.openBackgroundAudioAutoPlay) {
        // 激活会话, 开始播放音乐
        [self.audioSession setActive:YES error:NULL];
        [self.backgroundAudioPlayer prepareToPlay];
        [self.backgroundAudioPlayer play];
    }
}

// MARK: app开始进入前台
- (void)applicationDidBecomeActive:(NSNotification *)notify{
    // 进入前台,暂停播放音乐
    [self removeTimer];
    self.backgroundTimeLength = 0;
    
    if (!self.openBackgroundAudioAutoPlay) {
        return;
    }
    
    [self.backgroundAudioPlayer pause];
    [self.audioSession setActive:NO error:nil];
}

// MARK: 声音被打断
- (void)audioSessionInterruption:(NSNotification *)notify{
    if (!self.openBackgroundAudioAutoPlay) {
        return;
    }
    NSDictionary *userInfo = notify.userInfo;
    if (VALID_DICT(userInfo)) {
        NSNumber * interruptionTypeNumber = userInfo[AVAudioSessionInterruptionTypeKey];
        if (interruptionTypeNumber) {
            AVAudioSessionInterruptionType interruptionType = interruptionTypeNumber.integerValue;
            if (interruptionType == AVAudioSessionInterruptionTypeBegan) {
                // 中断开始, 音乐被打断
            }
            else if(interruptionType == AVAudioSessionInterruptionTypeEnded){
                // 中断结束, 音乐继续
                if (!self.backgroundAudioPlayer.isPlaying) {
                    [self.audioSession setActive:YES error:NULL];
                    [self.backgroundAudioPlayer prepareToPlay];
                    [self.backgroundAudioPlayer play];
                }
            }
        }
    }
}

- (instancetype)init{
    if (self = [super init]) {
        // 设置
        self.audioSession = [AVAudioSession sharedInstance];
        
        [self setupListener];
    }
    return self;
}

// 设置监听者
- (void)setupListener{
    REGISTER_NOTIFY(UIApplicationDidEnterBackgroundNotification, @selector(applicationDidEnterBackground:));
    REGISTER_NOTIFY(UIApplicationDidBecomeActiveNotification, @selector(applicationDidBecomeActive:));
    REGISTER_NOTIFY(AVAudioSessionInterruptionNotification, @selector(audioSessionInterruption:))
}

// MARK: 配置会话
- (void)setupAudioSession{
    if ([self.audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:NULL]) {
        [self.audioSession setActive:NO error:NULL];
    }
}

// MARK: 配置音乐播放器
- (void)setupBackgroundAudioPlayer{
    // FIXME: 填写无声音乐的路径
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"silence" ofType:@"mp3"];
    NSURL *fileURL = [NSURL fileURLWithPath:filePath];
    if (self.backgroundAudioPlayer == nil) {
        self.backgroundAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];
    }
    
    self.backgroundAudioPlayer.numberOfLoops = -1;
    self.backgroundAudioPlayer.volume = 1;
    self.backgroundAudioPlayer.delegate = self;
    
}

- (void)setOpenBackgroundAudioAutoPlay:(BOOL)openBackgroundAudioAutoPlay{
    _openBackgroundAudioAutoPlay = openBackgroundAudioAutoPlay;
    if (openBackgroundAudioAutoPlay) {
        // 设置会话
        [self setupAudioSession];
        // 设置背景播放器
        [self setupBackgroundAudioPlayer];
    }
    else{
        // 关闭后台播放音乐
        if (self.backgroundAudioPlayer && self.backgroundAudioPlayer.isPlaying) {
            [self.backgroundAudioPlayer stop];
        }
        self.backgroundAudioPlayer = nil;
        // 设置激活状态为NO
        [self.audioSession setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:NULL];
    }
}

- (void)startTimer{
    if (self.timer == nil) {
        self.timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerRun:) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
    }
    [self.timer setFireDate:[NSDate distantPast]];
}

- (void)removeTimer{
    if (self.timer) {
        [self.timer setFireDate:[NSDate distantFuture]];
        [self.timer invalidate];
        self.timer = nil;
    }
}

- (void)timerRun:(NSTimer *)timer{
    self.backgroundTimeLength++;
    
    // 超过180秒, 退出播放
    if (self.backgroundTimeLength > 180) {
        [self stopBackMode];
    }
}

// MARK: 退出后台常驻模式
- (void)stopBackMode{
    [self removeTimer];
    self.backgroundTimeLength = 0;
    if (!self.openBackgroundAudioAutoPlay) {
        return;
    }
    [self.backgroundAudioPlayer pause];
    [self.audioSession setActive:NO error:nil];
}


#pragma mark --------AVAudioDelegate--------
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    // TODO: 播放完成
}

- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error{
    // TODO: 编码类型
}

@end

最后在Appdelegate.m文件中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // 后台播放无声音乐
    [OFAudioManager sharedIntance].openBackgroundAudioAutoPlay = YES;
    
    return YES;
}


避免过长时间占用后台资源, 后台停留时间大于180s时,停止播放无声音乐,释放后台任务,提升APP的流程度。

目前测试没有任何问题, 下版提交审核,看苹果爸爸给过不给了

你可能感兴趣的:(后台播放无声音乐保活)