iOS 10 开始支持
1. xcode - >file -> new -> target
选择iOS下面的,Notification Service Extension
- Service Extension 中收到推送之后,用AVSpeechSynthesisVoice相关类,直接把推送过来需要播报相关的文字转化成语音播报
- ServiceExtension中收到推送之后,将要播报的数字,找到对应的单个音频,排序,用拼接音频的方式<通过推送过来的文字去查找相关的音频,然后拼接成一个音频>,然后使用AudioServicesCreateSystemSoundID播放
代码:
1. #import <AVFoundation/AVFoundation.h>
2. 添加方法
- (void)playVoiceWithAVSpeechSynthesisVoiceWithContent:(NSString *)content {
if (content.length == 0) {
return;
}
// 声音选择中文
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
// 创建语音合成器
AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
// 实例化发声的对象
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:content];
utterance.voice = voice;
utterance.rate = 0.5;
// 朗读的内容
[synthesizer speakUtterance:utterance];
}
3. 在- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {} 方法里面 调用此方法
[self playVoiceWithAVSpeechSynthesisVoiceWithContent:self.bestAttemptContent.body];