保持后臺(tái)活躍性

//
//  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(@"有錯(cuò)誤1");
        
    }
    
    NSError *activeError;
    [[AVAudioSession sharedInstance] setActive:YES error:&activeError];
    
    if (activeError) {
        
        NSLog(@"有錯(cuò)誤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;
    }];
    
    //開(kāi)啟定時(shí)器 不斷向系統(tǒng)請(qǐng)求后臺(tái)任務(wù)執(zhí)行的時(shí)間
    self.timer = [NSTimer scheduledTimerWithTimeInterval:25.0 target:self selector:@selector(applyForMoreTime) userInfo:nil repeats:YES];
    [self.timer fire];
    
}

- (void)applyForMoreTime {
    
    //如果系統(tǒng)給的剩余時(shí)間小于60秒 就終止當(dāng)前的后臺(tái)任務(wù),再重新初始化一個(gè)后臺(tái)任務(wù),重新讓系統(tǒng)分配時(shí)間,這樣一直循環(huán)下去,保持APP在后臺(tái)一直處于active狀態(tài)。
    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(@"來(lái)電話了");
    
    [self creatPlayer];
    
}

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

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

@end

image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前不久在微信上跟蔚蔚有一場(chǎng)短暫的聊天。她吐槽她忙得火燒眉毛的工作,我吐槽我磕磕碰碰的學(xué)業(yè)。期間提到我畢業(yè)之后的打算...
    阿豆阿豆閱讀 956評(píng)論 0 1
  • 8 男孩要窮養(yǎng) 女孩要富養(yǎng)? 之前我對(duì)這種教育孩子的方式一直是持有支持的態(tài)度,男孩子窮養(yǎng),不僅可以培養(yǎng)他一...
    李杰新閱讀 786評(píng)論 0 0
  • 想象周泰守著濡須口的樣子,那個(gè)時(shí)候他已經(jīng)不再年輕,握著刀孤狼一樣在滂沱大雨中廝殺,烏黑的鎧甲滴著水,盔上的纓子紅得...
    洞庭府君閱讀 435評(píng)論 0 7

友情鏈接更多精彩內(nèi)容