iOS-后台语音播报

公司要求需要做一个类似支付宝的收款的语音播报功能,研究了一天才完成,记录下来

写在前面

语音播报功能的实现必须是推送语音合成,选择的推送是极光推送,本文最终实现的效果即使APP被杀死也可以进行语音播报

实现

  1. 配置推送证书,极光文档里面有,把极光推送集成进去就不说了

  2. 语音合成我用的是系统的方法,不过语音死板不好听,但是使用很简单,3行代码就可以,建议使用其他的SDK

    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"成功集成语音播报"];
     
    AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
     
    [synth speakUtterance:utterance];
    
  3. 在收到通知的时候使用上面的3行代码就可以进行语音播报,但是只限于APP前台运行,当后台运行的时候语音播报便不可以了,此时需要加入下面代码让语音播报可以在后台运行,但是杀死的情况下不能播报,杀死重新启动返回后台也不可以播报. 我是在AppDelegate里面写入这个方法的

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
    

    把下面的代码写入进去

    NSError *error = NULL;
     
    AVAudioSession *session = [AVAudioSession sharedInstance];
     
    [session setCategory:AVAudioSessionCategoryPlayback error:&error];
     
    if(error) {
        // Do some error handling
         
    }
     
    [session setActive:YES error:&error];
     
    if (error) {
        // Do some error handling
    }
     
    // 让app支持接受远程控制事件
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    
  4. 让语音播报在后台也可以进行的话就需要在

    // 在AppDelegate定义属性
    @property (nonatomic, unsafe_unretained) UIBackgroundTaskIdentifier backgroundTaskIdentifier;
    
    - (void)applicationWillResignActive:(UIApplication *)application;
    

    里面加入以下的方法

     - (void)applicationWillResignActive:(UIApplication *)application {
    
     // 开启后台处理多媒体事件
    
     [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    
     AVAudioSession *session=[AVAudioSession sharedInstance];
    
     [session setActive:YES error:nil];
    
     // 后台播放
    
     [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    
     // 这样做,可以在按home键进入后台后 ,播放一段时间,几分钟吧。但是不能持续播放网络歌曲,若需要持续播放网络歌曲,还需要申请后台任务id,具体做法是:
    
     _backgroundTaskIdentifier=[AppDelegate backgroundPlayerID:_backgroundTaskIdentifier];
    
     // 其中的_bgTaskId是后台任务UIBackgroundTaskIdentifier _bgTaskId;
     
     }
     //实现一下backgroundPlayerID:这个方法:
     
     +(UIBackgroundTaskIdentifier)backgroundPlayerID:(UIBackgroundTaskIdentifier)backTaskId{
     
     //设置并激活音频会话类别
    
     AVAudioSession *session=[AVAudioSession sharedInstance];
    
     [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    
     [session setActive:YES error:nil];
    
     //允许应用程序接收远程控制
    
     [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    
     //设置后台任务ID
    
     UIBackgroundTaskIdentifier newTaskId=UIBackgroundTaskInvalid;
    
     newTaskId=[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
    
     if(newTaskId!=UIBackgroundTaskInvalid&&backTaskId!=UIBackgroundTaskInvalid){
    
     [[UIApplication sharedApplication] endBackgroundTask:backTaskId];
    
     }
    
     return newTaskId;
     
    }
    
  5. 到这里就可以进行后台播报了,但是注意 到这一步只有在程序没有被杀死的情况下才可以播报, 杀死之后是不能播报的, 所有我们还要进行处理,这时需要使用 UNNotificationServiceExtension.

    • 创建 UNNotificationServiceExtension

      iOS-后台语音播报_第1张图片

      填写文件名,边创建好了

  • NotificationService.m 里面有一个方法

    - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler;
    

    此时把语音播报写进去就可以了

     - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
         
         self.contentHandler = contentHandler;
         
         self.bestAttemptContent = [request.content mutableCopy];
    
         // Modify the notification content here...
         
         self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
    
         NSString *content = request.content.userInfo[@"aps"][@"alert"][@"body"];
    
         AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:content];
    
         AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
    
         [synth speakUtterance:utterance];
    
         self.contentHandler(self.bestAttemptContent);
    }
    
  • 认为到这里就完成了?,NO!在发送推送的时候还需要在极光推送服务里面配置一下

    iOS-后台语音播报_第3张图片
    WX20170623-103137.png

到这一步的时候,后台播报就可以执行了, 但是 此播报服务只能在 iOS10 系统之后才可以进行, 如果想适配iOS9之前的只能做一个固定的音频文件放到项目里面,比如支付宝的的到账提醒, 然后在推送的时候

iOS-后台语音播报_第4张图片
WX20170623-103724.png

这时候就可以完美播放音频文件了, 提醒: 如果不需要动态的语音播放, 直接可以使用这个方法,不需要配置 UNNotificationServiceExtension 和后台播放了,因为这个方法是系统认为推送的提示音

你可能感兴趣的:(iOS-后台语音播报)