美文网首页
保持后台活跃性

保持后台活跃性

作者: Luyc_Han | 来源:发表于2018-05-03 16:00 被阅读10次
//
//  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

相关文章

  • 保持后台活跃性

  • 保持思维的活跃性

    本文原创首发于微信公众号:夜渔识读,免费领取10本经典著作 文/夜渔 前几天有读者留言,她说自己失恋了,特别后悔,...

  • 保持活跃

    这里的活跃,我希望更多是头脑的活跃,自大学后,我因为从事的工作跟计算无多大关联,至此我仿佛很难很难再体会到那种头脑...

  • 保持活跃

    保持活跃是维持身心健康必不可少的。这里仅举一例,对芒格这样的人来说,没有什么比学习的乐趣更大,也没有什么比阅读更有...

  • iOS 关于app后台保活

    最近项目需要在后台保持活跃,先说下结果:使用后台播放无声音乐保活。 1.需要先打开Background Modes...

  • 头脑保持活跃

    头脑保持活跃 文/素商陶菊 这今天在读一本有关音乐的书。 读到科学家达尔文的一段话,他是这样说的“如果我活第二次,...

  • 保持活跃状态

    有了意义,有了决策依据和方法,有了从决策到行为的理念支撑。还欠缺的,是做出行为的能量。 没有能量,就什么都不想做,...

  • 芒果口袋

    保持活跃,保持健康 ...

  • 后台保持

    使用block的可以让程序在后台较长久地运行.在以前,当应用被按Home键退出后,应用仅有最多5秒钟的时间做一些保...

  • 保持活跃的心

    每当我在朋友圈晒出图片的时候,总是会很快的弹出聊天窗口,“啊,7姨,今天又拍好照片了啊”。“7姨,这是在哪儿拍的啊...

网友评论

      本文标题:保持后台活跃性

      本文链接:https://www.haomeiwen.com/subject/kbflrftx.html