保持后台活跃性

//
//  ViewController.m
//  TheBackground
//
//  Created by 王木木 on 2018/4/27.
//  Copyright © 2018年 wangmumu. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

/**  */
@property (nonatomic,strong) AVAudioPlayer *audioPlayer;

@property (nonatomic, assign) UIBackgroundTaskIdentifier bgTask;

/**  */
@property (nonatomic,strong) NSTimer *timer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
#ifdef HAN
    
    NSLog(@"1111");
    
#endif
    
    NSError *error;
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
    
    if (error) {
        
        NSLog(@"有错误1");
        
    }
    
    NSError *activeError;
    [[AVAudioSession sharedInstance] setActive:YES error:&activeError];
    
    if (activeError) {
        
        NSLog(@"有错误2");
        
    }
    
    NSError *audioError;
    
    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[self getUrl] error:&audioError];
    
    [self.audioPlayer prepareToPlay];
    [self.audioPlayer setNumberOfLoops:-1];
    self.audioPlayer.volume = 1.0;
    
    if (audioError) {
        
        NSLog(@"播放器初始化失败");
        
        self.audioPlayer = nil;
        
    }
    
    [self creatPlayer];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(breakAudioSessionEvent:) name:AVAudioSessionInterruptionNotification object:nil];
    
    
    self.bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
        self.bgTask = UIBackgroundTaskInvalid;
    }];
    
    //开启定时器 不断向系统请求后台任务执行的时间
    self.timer = [NSTimer scheduledTimerWithTimeInterval:25.0 target:self selector:@selector(applyForMoreTime) userInfo:nil repeats:YES];
    [self.timer fire];
    
}

- (void)applyForMoreTime {
    
    //如果系统给的剩余时间小于60秒 就终止当前的后台任务,再重新初始化一个后台任务,重新让系统分配时间,这样一直循环下去,保持APP在后台一直处于active状态。
    if ([UIApplication sharedApplication].backgroundTimeRemaining < 60) {
        
        [[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
        
        self.bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
            
            [[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
            
            self.bgTask = UIBackgroundTaskInvalid;
            
        }];
        
    }
    
    

}

- (void)breakAudioSessionEvent:(NSNotificationCenter *)sender {
    
    NSLog(@"来电话了");
    
    [self creatPlayer];
    
}

- (void)creatPlayer {
    
  [self.audioPlayer play];
    
}

- (NSURL *)getUrl {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"群星-昨日重现" ofType:@"mp3"];
    
    NSURL *pathUrl = [NSURL fileURLWithPath:path];
    
    return pathUrl;
    
}

@end

image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 前不久在微信上跟蔚蔚有一场短暂的聊天。她吐槽她忙得火烧眉毛的工作,我吐槽我磕磕碰碰的学业。期间提到我毕业之后的打算...
    阿豆阿豆阅读 4,369评论 0 1
  • 8 男孩要穷养 女孩要富养? 之前我对这种教育孩子的方式一直是持有支持的态度,男孩子穷养,不仅可以培养他一...
    李杰新阅读 4,073评论 0 0
  • 想象周泰守着濡须口的样子,那个时候他已经不再年轻,握着刀孤狼一样在滂沱大雨中厮杀,乌黑的铠甲滴着水,盔上的缨子红得...
    洞庭府君阅读 3,012评论 0 7

友情链接更多精彩内容