您的位置:首页 > 其它

使用AVSpeechSynthesizer添加"文本转语音"的功能

2016-11-11 22:03 465 查看
AVSpeechSynthesizer播放一个或者多个语音内容:

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
@interface TSSpeech: NSObject

@property(nonatomic, strong, readonly)AVSpeechSynthesizer *speechSynthesizer;

+ (instancetype)speechController;

- (void)beginConverstation;

@end
#import "TSSpeech.h"
@interface TSSpeech ()

@property(nonatomic, strong)AVSpeechSynthesizer *speechSynthesizer;
@property(nonatomic, strong)NSArray *speechVoices;
@property(nonatomic, strong)NSArray *speechStrings;

@end
@implementation TSSpeech
+ (instancetype)speechController{

return  [[self alloc]init];

}

- (instancetype)init{

self = [super init];
if (self) {

_speechSynthesizer = [[AVSpeechSynthesizer alloc]init];
_speechVoices      = @[
[AVSpeechSynthesisVoice voiceWithLanguage:@"zh_TW"],
[AVSpeechSynthesisVoice voiceWithLanguage:@"zh_HK"],
];
_speechStrings = [self buildingSpeechStrings];

}
return self;

}

- (NSArray *)buildingSpeechStrings{

return @[
@"你好世界",
@"hello world",
@"thank you very much",
@"show me your code",
];

}
- (void)beginConverstation{

for (NSUInteger i = 0; i < _speechStrings.count; i++) {

AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:_speechStrings[i]];//创建新的 AVSpeechUtterance 实例
utterance.voice = _speechVoices[i % 2];//交替使用不同的音色播放
utterance.rate =0.4f;//设置播放语音的速率0.4f
utterance.pitchMultiplier = 0.8f;//可在播放特定语句时改变声音的音调,pitchMultiplier介于0.5(低音)和2.0(高音)之间
utterance.postUtteranceDelay  = 0.1f;//让语音播放合成器在播放下一句之前有短暂的暂停
[_speechSynthesizer speakUtterance:utterance];//播放语音

}
}
@end


以下内容为搜集的各国语言缩写列表

 th-TH  泰语 (泰国)

  pt-BR  葡萄牙语 (巴西)

  sk-SK  斯洛伐克语 (斯洛伐克)

  fr-CA  法语 (加拿大)

  ro-RO  罗马尼亚语 (罗马尼亚)

  no-NO  挪威语 (挪威)

  fi-FI  芬兰语 (芬兰)

  pl-PL  波兰语 (波兰)

  de-DE  德语 (德国)

  nl-NL  荷兰语 (荷兰)

  tr-TR  土耳其语 (土耳其)

  it-IT  意大利语 (意大利)

  pt-PT  葡萄牙语 (葡萄牙)

  fr-FR  法语 (法国)

  ru-RU  俄语 (俄罗斯)

  es-MX  西班牙语 (墨西哥) 

  zh-HK  汉语-香港

  sv-SE  瑞典语 (瑞典)

  hu-HU  匈牙利语 (匈牙利)

  zh-TW  汉语-台湾

  es-ES 西班牙语 (西班牙)

  zh-CN 汉语-普通话

  nl-BE 荷兰语 (比利时)

  en-GB 英语 (英国)

  ar-SA 阿拉伯语 (沙特阿拉伯)

  ko-KR 汉语

  cs-CZ 捷克语 (捷克共和国)

  en-ZA 英语 (南非)

  en-AU 英语 (澳大利亚)

  da-DK 丹麦语 (丹麦)

  en-US 英语 (美国)

  en-IE 英语 (爱尔兰)

  el-GR 希腊语 (希腊)

  ja-JP 日语
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  AVFoundation