博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[ios]app后台运行
阅读量:7203 次
发布时间:2019-06-29

本文共 2475 字,大约阅读时间需要 8 分钟。

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

1 使用

将文件加入工程,包括音频文件。
可以在源文件中加入单例,便于使用:
+ (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 应用在后台继续运行需要有充分的理由,否则可能审核不通过~~~

转载地址:http://qrwum.baihongyu.com/

你可能感兴趣的文章
消息中间件 RocketMQ源码解析:Message顺序发送与消费
查看>>
Thread interrupt
查看>>
博客搬家至简书
查看>>
Linux上的free命令详解
查看>>
linux主目录下各个子目录的作用
查看>>
[问题]javax.servlet不存在的问题
查看>>
Hive学习总结之五:HBase和Hive的集成
查看>>
Windows7系统中启动Windows Event Log服务提示拒绝访问,错误5的解决方法
查看>>
mybatis--缓存(一级和二级缓存)
查看>>
centos 配置 nginx + fcgiwrap + git
查看>>
Eclipse下Maven打包非法字符问题
查看>>
FreeMarker标签
查看>>
AngularJS 中的 Promise 和 设计模式
查看>>
《从面试题来看源码》,单参数,多参数,如何正确使用@Param
查看>>
《JavaScript设计模式》学习日志
查看>>
MySql 建表、添加字段、修改字段、添加索引SQL语句写法
查看>>
Core Bluetooth框架之三:最佳实践
查看>>
Gson序列化时@SerializedName的使用
查看>>
windows上pip install 报编码错误
查看>>
boost asio学习笔记 [1] - 同步通讯
查看>>