记得大学的时候学微软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
时间: 2024-09-27 19:40:42