iOS推送消息push,语音播报

该博客介绍了如何在Service Extension中处理iOS推送消息,通过AVSpeechSynthesisVoice将文字转换为语音播报。同时,当收到推送后,根据文字内容寻找对应的单个音频进行拼接,最终使用AudioServicesCreateSystemSoundID播放合成的音频。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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];





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值