iOS编程Text to Speech 及支持语言
2017-04-17 00:22
417 查看
iOS编程中Text to Speech的基本框架,需要包含头文件:
#import <AVFoundation/AVFoundation.h>
基本编程框架:
头文件定义
@interface Text2Speech :NSObject<AVSpeechSynthesizerDelegate>
{
NSString * text;//需要发音的文本
float rate;//速率
float pitch;//音高
int timeInterval;//每句话的间隔
enum
EnglishType voice;//英语的种类
AVSpeechSynthesizer* voiceSynthesizer;//合成器
BOOL speakFinished;//当前是否完成speech
NSString *speakingString;//正在合成器中的文本
}
-(void)speak:(NSString *)uterString;
-(BOOL)speakFinish:(NSString *)uterString;
-(void)stop;
-(void)pause;
-(void)continueSpeak;
//源文件代码
-(id)init{
if(self=[superinit]){
NSLog(@"\n----------Text2Speech init OK----------\n");
self.rate=1.0;
self.pitch =1.0;
self.timeInterval=300;
self.text=[NSStringalloc];
self.text=@"";
self.speakFinished=false;
self.speakingString=@"";
self.voiceSynthesizer=[[AVSpeechSynthesizeralloc]init];
self.voiceSynthesizer.delegate=self;
logFlag=NO;
}
returnself;
}
-(void)speak:(NSString *)uterString{
@autoreleasepool
{
AVSpeechUtterance* utterance=[[AVSpeechUtterancealloc]initWithString:uterString];
utterance.rate=self.rate;
utterance.pitchMultiplier=self.pitch;
utterance.postUtteranceDelay=0;
speakFinished=false;
switch(self.voice){
caseEN_UK:
utterance.voice=[AVSpeechSynthesisVoicevoiceWithLanguage:@"en-gb"];
break;
caseEN_USA:
utterance.voice=[AVSpeechSynthesisVoicevoiceWithLanguage:@"en-us"];
break;
caseEN_CA:
utterance.voice=[AVSpeechSynthesisVoicevoiceWithLanguage:@"en-ca"];
break;
caseEN_AU:
utterance.voice=[AVSpeechSynthesisVoicevoiceWithLanguage:@"en-au"];
break;
}
speakingString=@"";
[self.voiceSynthesizerspeakUtterance:utterance];
}
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer
didFinishSpeechUtterance:(AVSpeechUtterance *)utterance{
printTestMsg(@"speechSynthesizer",@"--finish speaking--",logFlag);
speakFinished=true;
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer
willSpeakRangeOfSpeechString:(NSRange)characterRange
utterance:(AVSpeechUtterance *)utterance{
NSString *speechSting=utterance.speechString;
NSString *subString=[speechStingsubstringWithRange:characterRange];
self.speakingString=subString;
printTestMsg(@"speechSynthesizer",@"-------the current speaking string---",NO);
}
-(BOOL)speakFinish:(NSString *)uterString{
//need add code
returnspeakFinished;
}
-(void)stop{
[self.voiceSynthesizerstopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
-(void)pause{
[self.voiceSynthesizerpauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
-(void)continueSpeak{
[self.voiceSynthesizercontinueSpeaking];
}
@end
/*
BOOL speechPaused = 0;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.synthesizer = [[AVSpeechSynthesizer alloc] init];
speechPaused = NO;
self.synthesizer.delegate = self;
}
- (IBAction)playPauseButtonPressed:(UIButton *)sender {
[self.textToSpeak resignFirstResponder];
if (speechPaused == NO) {
[self.playPauseButton setTitle:@"Pause" forState:UIControlStateNormal];
[self.synthesizer continueSpeaking];
speechPaused = YES;
} else {
[self.playPauseButton setTitle:@"Play" forState:UIControlStateNormal];
speechPaused = NO;
[self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
if (self.synthesizer.speaking == NO) {
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:self.textToSpeak.text];
//utterance.rate = AVSpeechUtteranceMinimumSpeechRate;
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-au"];
[self.synthesizer speakUtterance:utterance];
}
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
[self.playPauseButton setTitle:@"Play" forState:UIControlStateNormal];
speechPaused = NO;
NSLog(@"Playback finished");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
*/
Text To Speech所支持的语言类型,请参考
#import <AVFoundation/AVFoundation.h>
基本编程框架:
头文件定义
@interface Text2Speech :NSObject<AVSpeechSynthesizerDelegate>
{
NSString * text;//需要发音的文本
float rate;//速率
float pitch;//音高
int timeInterval;//每句话的间隔
enum
EnglishType voice;//英语的种类
AVSpeechSynthesizer* voiceSynthesizer;//合成器
BOOL speakFinished;//当前是否完成speech
NSString *speakingString;//正在合成器中的文本
}
-(void)speak:(NSString *)uterString;
-(BOOL)speakFinish:(NSString *)uterString;
-(void)stop;
-(void)pause;
-(void)continueSpeak;
//源文件代码
-(id)init{
if(self=[superinit]){
NSLog(@"\n----------Text2Speech init OK----------\n");
self.rate=1.0;
self.pitch =1.0;
self.timeInterval=300;
self.text=[NSStringalloc];
self.text=@"";
self.speakFinished=false;
self.speakingString=@"";
self.voiceSynthesizer=[[AVSpeechSynthesizeralloc]init];
self.voiceSynthesizer.delegate=self;
logFlag=NO;
}
returnself;
}
-(void)speak:(NSString *)uterString{
@autoreleasepool
{
AVSpeechUtterance* utterance=[[AVSpeechUtterancealloc]initWithString:uterString];
utterance.rate=self.rate;
utterance.pitchMultiplier=self.pitch;
utterance.postUtteranceDelay=0;
speakFinished=false;
switch(self.voice){
caseEN_UK:
utterance.voice=[AVSpeechSynthesisVoicevoiceWithLanguage:@"en-gb"];
break;
caseEN_USA:
utterance.voice=[AVSpeechSynthesisVoicevoiceWithLanguage:@"en-us"];
break;
caseEN_CA:
utterance.voice=[AVSpeechSynthesisVoicevoiceWithLanguage:@"en-ca"];
break;
caseEN_AU:
utterance.voice=[AVSpeechSynthesisVoicevoiceWithLanguage:@"en-au"];
break;
}
speakingString=@"";
[self.voiceSynthesizerspeakUtterance:utterance];
}
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer
didFinishSpeechUtterance:(AVSpeechUtterance *)utterance{
printTestMsg(@"speechSynthesizer",@"--finish speaking--",logFlag);
speakFinished=true;
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer
willSpeakRangeOfSpeechString:(NSRange)characterRange
utterance:(AVSpeechUtterance *)utterance{
NSString *speechSting=utterance.speechString;
NSString *subString=[speechStingsubstringWithRange:characterRange];
self.speakingString=subString;
printTestMsg(@"speechSynthesizer",@"-------the current speaking string---",NO);
}
-(BOOL)speakFinish:(NSString *)uterString{
//need add code
returnspeakFinished;
}
-(void)stop{
[self.voiceSynthesizerstopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
-(void)pause{
[self.voiceSynthesizerpauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
-(void)continueSpeak{
[self.voiceSynthesizercontinueSpeaking];
}
@end
/*
BOOL speechPaused = 0;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.synthesizer = [[AVSpeechSynthesizer alloc] init];
speechPaused = NO;
self.synthesizer.delegate = self;
}
- (IBAction)playPauseButtonPressed:(UIButton *)sender {
[self.textToSpeak resignFirstResponder];
if (speechPaused == NO) {
[self.playPauseButton setTitle:@"Pause" forState:UIControlStateNormal];
[self.synthesizer continueSpeaking];
speechPaused = YES;
} else {
[self.playPauseButton setTitle:@"Play" forState:UIControlStateNormal];
speechPaused = NO;
[self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}
if (self.synthesizer.speaking == NO) {
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:self.textToSpeak.text];
//utterance.rate = AVSpeechUtteranceMinimumSpeechRate;
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-au"];
[self.synthesizer speakUtterance:utterance];
}
}
-(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
[self.playPauseButton setTitle:@"Play" forState:UIControlStateNormal];
speechPaused = NO;
NSLog(@"Playback finished");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
*/
Text To Speech所支持的语言类型,请参考
Arabic (Saudi Arabia) - ar-SA Chinese (China) - zh-CN Chinese (Hong Kong SAR China) - zh-HK Chinese (Taiwan) - zh-TW Czech (Czech Republic) - cs-CZ Danish (Denmark) - da-DK Dutch (Belgium) - nl-BE Dutch (Netherlands) - nl-NL English (Australia) - en-AU English (Ireland) - en-IE English (South Africa) - en-ZA English (United Kingdom) - en-GB English (United States) - en-US Finnish (Finland) - fi-FI French (Canada) - fr-CA French (France) - fr-FR German (Germany) - de-DE Greek (Greece) - el-GR Hindi (India) - hi-IN Hungarian (Hungary) - hu-HU Indonesian (Indonesia) - id-ID Italian (Italy) - it-IT Japanese (Japan) - ja-JP Korean (South Korea) - ko-KR Norwegian (Norway) - no-NO Polish (Poland) - pl-PL Portuguese (Brazil) - pt-BR Portuguese (Portugal) - pt-PT Romanian (Romania) - ro-RO Russian (Russia) - ru-RU Slovak (Slovakia) - sk-SK Spanish (Mexico) - es-MX Spanish (Spain) - es-ES Swedish (Sweden) - sv-SE Thai (Thailand) - th-TH Turkish (Turkey) - tr-TR
相关文章推荐
- Android TextToSpeech多国语言自动朗读
- Android TTS 英文或中文(要下载中文支持包)转语音 使用TextToSpeech
- 介绍Text-To-Speech在Android中的用法
- Text to speech hello world sapi
- text-to-speech
- 详解Android SDK1.6中Text-To-Speech(TTS)语音朗读
- 使用C#进行语音识别(Speech-to-Text)
- Android TTS(Text-to-speech)技术
- Google Text to Speech API and AIR
- Text-to-speech 入门与进阶学习笔记(android)
- 初试Text-To-Speech
- How To: 为组件属性添加Design-time多语言支持
- Windows phone 应用开发[1]-Text To Speech
- Windows phone 应用开发[1]-Text To Speech
- 学习Microsoft Speech SDK TTS(text to speech)简单运用
- 在C++Builder使用TTS(Text To Speech)
- Microsoft TTS(Text To Speech)语音包的简单应用
- C# 实现Text to Speech
- Microsoft Speech API 5.3 Text-to-Speech Tutorial