美文网首页iOS经验总结
ios 推送添加自定义语音播报

ios 推送添加自定义语音播报

作者: MCWorld | 来源:发表于2019-06-24 17:56 被阅读0次

    该功能需要用到 苹果的 Notification Service Extension 这个是iOS10.0推出的。https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension

    实现该功能,一,添加 Notification Service Extension 类

    导入Notification Service Extension - 1 导入Notification Service Extension - 2

    创建之后 程序内会出现 NotificationService.h ,NotificationService.m 文件

    二,然后就是发送推送消息 ,以极光推送为例

    (iOS 10 新增的 Notification Service Extension 功能,用 mutable-content 字段来控制。 若使用极光的 Web 控制台,需勾选 “可选设置”中 mutable-content 选项;若使用 RESTFul API 需设置 mutable-content 字段为 true。)

    打印推送内容

    三、使用 语音合成 功能 AVSpeechUtterance,将文字转化成语音播报(机械语音比较声音,要求不高的可以这么做)在 NotificationService.m 中的 "- (void)didReceiveNotificationRequest:(UNNotificationRequest*)request withContentHandler:(void(^)(UNNotificationContent*_Nonnull))contentHandler" 方法中代码

    /** @"voice":是 自己自定义上传的字段,可以与后台商量

      self.aVSpeechSynthesizer = [[AVSpeechSynthesizer alloc] init];

            // Modify the notification content here...

            NSString*str =self.bestAttemptContent.userInfo[@"voice"];

            AVSpeechUtterance * aVSpeechUtterance = [[AVSpeechUtterance alloc] initWithString:str];

            aVSpeechUtterance.rate = AVSpeechUtteranceDefaultSpeechRate;

            aVSpeechUtterance.voice =[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];

            [self.aVSpeechSynthesizerspeakUtterance:aVSpeechUtterance];

    */

    极光web后台的推送页面,voice和tag都是可以更改的

    四、配置

    1、项目的target

    配置- 1

    2、扩展的target

    配置 - 2

    五、如果不想用系统 去合成语音,可以在本地 导入自定义语音文件 

    1、导入 语音文件 到项目  ,项目和扩展的 target都要勾选

    导入语音文件

    2、播放语音  //可以查考-- https://www.jianshu.com/p/d79bd8e6570e

      SystemSoundID soundID;

            NSString *path = [[NSBundle mainBundle] pathForResource:@"testVoice.m4a" ofType:nil];

            AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);

            AudioServicesPlaySystemSound(soundID);

            /*AudioServicesPlaySystemSoundWithCompletion*/

            AudioServicesPlayAlertSoundWithCompletion(soundID, ^{

                self.contentHandler(self.bestAttemptContent);

            });

    至此,功能就能实现自定义语音播报了

    六、最终的代码体现

    代码

    整体导入流程可以参考链接 :

    https://www.jianshu.com/p/ef344a294f99

    iOS,推送+后台语音播报,推送+程序杀死仍语音播报,看这一篇就够啦! - 简书

    http://www.jianshu.com/p/c06133d576e4

    iOS,APP退到后台,获取推送成功的内容并且语音播报内容。 - 简书

    相关文章

      网友评论

        本文标题:ios 推送添加自定义语音播报

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