iOS系统语音播报文字
2016-01-08 10:39
232 查看
AVSpeechSynthesizer *avSpeech = [[AVSpeechSynthesizeralloc]init];
AVSpeechUtterance *avSpeechterance = [AVSpeechUtterancespeechUtteranceWithString:@"接下来是新闻联播"];
AVSpeechSynthesisVoice *voiceType = [AVSpeechSynthesisVoicevoiceWithLanguage:@"zh-CN"];
avSpeechterance.voice = voiceType;
avSpeechterance.rate *=0.4;
[avSpeech speakUtterance:avSpeechterance];
参考:
在iOS7上,系统为我们提供了语音播报文字的功能,我们不仅可以播报英语内容,也可以播报汉语文字
if(
[[[UIDevice currentDevice] systemVersion] integerValue]
>= 7.0)
{
AVSpeechUtterance *utterance
= [AVSpeechUtterance speechUtteranceWithString:warnmsg];
utterance.rate *= 0.8;
AVSpeechSynthesizer *synth
= [[AVSpeechSynthesizer alloc] init];
//获取当前系统语音
NSString *preferredLang
= @"";
if (m_strLang == "zh-Hans")
{
preferredLang = @"zh-CN";
}else{
preferredLang = @"en-US";
}
AVSpeechSynthesisVoice *voice
= [AVSpeechSynthesisVoice voiceWithLanguage:[NSStringstringWithFormat:@"%@",preferredLang]];
utterance.voice = voice;
[synth speakUtterance:utterance];
[synth release];
}
首先,判断设备的系统在iOS7或更高版本,接下来就是关键的AVSpeechUtterance和AVSpeechSynthesizer,从代价可以看出AVSpeechUtterance是设置需要播报的文字内容-warnmsg、语音速率-rate以及语言种类-preferredLang,而AVSpeechSynthesizer就是开始同步播放的类。
上面的代码节选自我项目中的部分代码,有一些变量也没有实际的意义,直接拷贝肯定不行,所以想要实现该功能,还是自己写一些变量内容,例如warnmsg = @"今天天气真好,工作加油";来测试一下。
上面的代码只能在前台运行,如果退到后台的话则不能运行,如果想要在推送的时候播报文字语音,那么这就有点蛋疼了。好在也有解决方法,在AppDelegate的applicationDidLaunch的代码中加上如下代码,则可支持后台语音播报文字内容,
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
}
AVSpeechUtterance *avSpeechterance = [AVSpeechUtterancespeechUtteranceWithString:@"接下来是新闻联播"];
AVSpeechSynthesisVoice *voiceType = [AVSpeechSynthesisVoicevoiceWithLanguage:@"zh-CN"];
avSpeechterance.voice = voiceType;
avSpeechterance.rate *=0.4;
[avSpeech speakUtterance:avSpeechterance];
参考:
在iOS7上,系统为我们提供了语音播报文字的功能,我们不仅可以播报英语内容,也可以播报汉语文字
if(
[[[UIDevice currentDevice] systemVersion] integerValue]
>= 7.0)
{
AVSpeechUtterance *utterance
= [AVSpeechUtterance speechUtteranceWithString:warnmsg];
utterance.rate *= 0.8;
AVSpeechSynthesizer *synth
= [[AVSpeechSynthesizer alloc] init];
//获取当前系统语音
NSString *preferredLang
= @"";
if (m_strLang == "zh-Hans")
{
preferredLang = @"zh-CN";
}else{
preferredLang = @"en-US";
}
AVSpeechSynthesisVoice *voice
= [AVSpeechSynthesisVoice voiceWithLanguage:[NSStringstringWithFormat:@"%@",preferredLang]];
utterance.voice = voice;
[synth speakUtterance:utterance];
[synth release];
}
首先,判断设备的系统在iOS7或更高版本,接下来就是关键的AVSpeechUtterance和AVSpeechSynthesizer,从代价可以看出AVSpeechUtterance是设置需要播报的文字内容-warnmsg、语音速率-rate以及语言种类-preferredLang,而AVSpeechSynthesizer就是开始同步播放的类。
上面的代码节选自我项目中的部分代码,有一些变量也没有实际的意义,直接拷贝肯定不行,所以想要实现该功能,还是自己写一些变量内容,例如warnmsg = @"今天天气真好,工作加油";来测试一下。
上面的代码只能在前台运行,如果退到后台的话则不能运行,如果想要在推送的时候播报文字语音,那么这就有点蛋疼了。好在也有解决方法,在AppDelegate的applicationDidLaunch的代码中加上如下代码,则可支持后台语音播报文字内容,
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
}
相关文章推荐
- iOS开发过程中一些数据的存储归档
- iOS-hitTest:withEvent与自定义hit-testing规则
- iOS 打印日志显示系统详细时间,类名,行号及打印值
- ARC 完全指南
- iOS NSNotificationCenter详解
- ios9 简单粗暴总结
- iOS图片拉伸技巧—— resizableImageWithCapInsets
- iOS监听键盘弹出
- iOS使用NSUserDefaults保存用户名和密码
- ios non-arc to arc 之三
- iOS cell排序
- 学习笔记5: 仿ios Dialog
- xcode7、iOS9 设置启动图片(Launch Image)
- IOS 去空格处理 特殊字符处理
- ios 非arc转arc 之二
- IOS笔记
- iOS开发调试技巧总结(持续更新中)
- iOS倒计时的实现
- ios 关于非arc 工程转arc
- iOS用CGContextRef画图(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)