[ios]app后台运行

时间:2023-03-08 23:43:16
[ios]app后台运行

参考:http://www.douban.com/note/375127736/

1 使用开源代码MMPDeepSleepPreventer
将文件加入工程,包括音频文件。
可以在源文件中加入单例,便于使用:
+ (MMPDeepSleepPreventer *)sharedSingleton
{
    static MMPDeepSleepPreventer * _sharedSingleton = nil;
    @synchronized([MMPDeepSleepPreventer class]){
                if (_sharedSingleton == nil) {
                        _sharedSingleton = [MMPDeepSleepPreventer alloc] init];
                }
        }
        return _sharedSingleton;
}

2 导入AVFoundation.framework
3 用文本编辑器打开你工程的info.plist文件
加入
        <key>UIBackgroundModes</key>
        <array>
                <string>audio</string>
        </array>
后保存。
4 在AppDelegate.m中
- (void)applicationDidEnterBackground:(UIApplication *)application里加入
    [MMPDeepSleepPreventer sharedSingleton] startPreventSleep]; (或者切换到后台的时候加入更严格的判断,需要应用在后台运行时才调用)

- (void)applicationDidBecomeActive:(UIApplication *)application里加入
    [MMPDeepSleepPreventer sharedSingleton] stopPreventSleep];
5 如果正在听音乐,比如百度音乐,这个时候启动的你的app,然后再切到后台,这个时候百度音乐会停。解决这个问题,需要调整MMPDeepSleepPreventer.m中代码的顺序。具体是mmp_setUpAudioSession中,先执行:
        NSError *categorySetError = nil;
        [audioSession setCategory:AVAudioSessionCategoryPlayback
                      withOptions:AVAudioSessionCategoryOptionMixWithOthers
                            error:&categorySetError];
        if (categorySetError) {
            MMPALog(@"Error setting AVAudioSession category: %@", categorySetError);
        }
再执行:
        NSError *activeSetError = nil;
        [audioSession setActive:YES
                          error:&activeSetError];
        
        if (activeSetError) {
            MMPALog(@"Error activating AVAudioSession: %@", activeSetError);
        }
在else分支里,把
        // Activate audio session
        OSStatus activationResult = 0;
        activationResult = AudioSessionSetActive(true);
        
        if (activationResult)
        {
            MMPDLog(@"AudioSession is active");
        }
放到最后执行。
6 修改启动和终止函数为:
- (void)startPreventSleep
{
    [self.audioPlayer play];
}

- (void)stopPreventSleep
{
    [self.audioPlayer stop];
}

由于初始化过程中
    self.audioPlayer.numberOfLoops = -1;
即无限循环播放,所以源码中的定时器根本没起作用,而且调用stopPreventSleep也没有真正stop,实际上一直在播放,所以要如上修改。

7 订阅通知:
 [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionWasInterrupted:) name:AVAudioSessionInterruptionNotification object:nil];

- (void)dealloc
{
    [NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)audioSessionWasInterrupted:(NSNotification *)notification
{
    if ([notification.userInfo count] == 0)
    {
        return;
    }
    
    if (AVAudioSessionInterruptionTypeEnded == [notification.userInfo[AVAudioSessionInterruptionTypeKey] intValue])
    {
        NSLog(@"received AVAudioSessionInterruptionTypeEnded -------------------");
        dispatch_async(dispatch_get_main_queue(), ^{
            [self startPreventSleep];
        });
    }
}
这样接打电话和闹钟响后音乐会恢复播放,后台任务可继续运行。

8 应用在后台继续运行需要有充分的理由,否则可能审核不通过~~~