您的位置:首页 > 移动开发 > IOS开发

iOS语音播报文字

2016-02-19 14:39 609 查看
记得大学的时候学微软Window Phone时,有语音识别类似苹果的嘿,Siri.今天无聊百度搜了一下,搜到苹果语音播报文字。自己试了下还挺好玩。

1.引入框架#import <AVFoundation/AVFoundation.h>

2.获取系统当前语言

//获取当前系统语音
NSString *m_strLang=[[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0];


3.播放文字内容

AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"我是谁? who is it?"];
utterance.rate *= 0.8;
AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
//获取当前系统语音 NSString *m_strLang=[[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0];
NSLog(@"%@",m_strLang);
NSString *preferredLang = @"";
if ([m_strLang isEqual: @"zh-Hans-CN"])
{
preferredLang = @"zh-CN";
}else{
preferredLang = @"en-US";
}
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:[NSString stringWithFormat:@"%@",preferredLang]];
utterance.voice = voice;
[synth speakUtterance:utterance];


4.注意: 在模拟器好像试实现不了,在真机测试很有意思,很好玩。

收集的一些AVFoundation资料:
http://www.cocoachina.com/ios/20150528/11966.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: