前言
之前自己的项目中曾经使用过讯飞的文字转语音技术,但是通过实际测试,发现它的免费在线转语音不是很好,受网络的影响声音经常断断续续的;而它的离线转语音价格有太贵,最便宜的要8000RMB/2000装机量,令许多开发者望而止步。那么既然支付不了讯飞那昂贵的费用,iOS自带的文字转语音也是我们不错的选择,只是他的发音效果不够理想,不过还可以接受。今天我就带大家学习iOS自带的文字转语音!
第一步:导入系统框架
使用iOS系统的文字转语音需要用到AVFoundation框架,这个不需要详谈了吧:
随后,导入
#import<AVFoundation/AVSpeechSynthesis.h>
需要代理的话导入代理
@interfaceViewController()<AVSpeechSynthesizerDelegate>
第二步:创建对象
在这里对象最好创建为全局的,便于我们控制;当然如果你不需要暂停、继续播放等操作的话可以创建一个局部的。
{
AVSpeechSynthesizer*av;
}
随后我们创建一个简单的按钮来操控他:
UIButton*button=[UIButtonbuttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(100,100,100,50);
[buttonsetTitle:@"讲"forState:UIControlStateNormal];
[buttonsetTitle:@"停"forState:UIControlStateSelected];
[buttonsetTitleColor:[UIColorblueColor]forState:UIControlStateNormal];
button.backgroundColor=[UIColorgrayColor];
button.showsTouchWhenHighlighted=YES;
[buttonaddTarget:selfaction:@selector(start:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
第三步:具体代码部分
-(void)start:(UIButton*)sender{
if(sender.selected==NO) {
if([avisPaused]) {
//如果暂停则恢复,会从暂停的地方继续
[avcontinueSpeaking];
sender.selected=!sender.selected;
}else{
//初始化对象
av= [[AVSpeechSynthesizeralloc]init];
av.delegate=self;//挂上代理
AVSpeechUtterance*utterance = [[AVSpeechUtterancealloc]initWithString:@"锦瑟无端五十弦,一弦一柱思华年。庄生晓梦迷蝴蝶,望帝春心托杜鹃。沧海月明珠有泪,蓝田日暖玉生烟。此情可待成追忆,只是当时已惘然。"];//需要转换的文字
utterance.rate=0.5;// 设置语速,范围0-1,注意0最慢,1最快;AVSpeechUtteranceMinimumSpeechRate最慢,AVSpeechUtteranceMaximumSpeechRate最快
AVSpeechSynthesisVoice*voice = [AVSpeechSynthesisVoicevoiceWithLanguage:@"zh-CN"];//设置发音,这是中文普通话
utterance.voice= voice;
[avspeakUtterance:utterance];//开始
sender.selected=!sender.selected;
}
}else{
//[av stopSpeakingAtBoundary:AVSpeechBoundaryWord];//感觉效果一样,对应代理>>>取消
[avpauseSpeakingAtBoundary:AVSpeechBoundaryWord];//暂停
sender.selected=!sender.selected;
}
//[utterance release];//需要关闭ARC
//[av release];
}
下面是代理:
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didStartSpeechUtterance:(AVSpeechUtterance*)utterance{
NSLog(@"---开始播放");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance*)utterance{
NSLog(@"---完成播放");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance*)utterance{
NSLog(@"---播放中止");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance*)utterance{
NSLog(@"---恢复播放");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance*)utterance{
NSLog(@"---播放取消");
}
详解:
@property(nonatomic,readonly,getter=isSpeaking)BOOL speaking;//是否正在播放
@property(nonatomic,readonly,getter=isPaused)BOOL paused;//是否暂停
- (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;//取消播放,将会完全停止
- (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;//暂停播放,将会保存进度
- (BOOL)continueSpeaking;//恢复播放
取消与暂停的两个效果我感觉都一样:
typedefNS_ENUM(NSInteger, AVSpeechBoundary) {
AVSpeechBoundaryImmediate,
AVSpeechBoundaryWord
}NS_ENUM_AVAILABLE_IOS(7_0);
@property(nonatomic)floatrate;// 设置语速,范围0-1,注意0最慢,1最快;AVSpeechUtteranceMinimumSpeechRate最慢,AVSpeechUtteranceMaximumSpeechRate最快
@property(nonatomic)floatpitchMultiplier;// [0.5 - 2] Default = 1,声调,不怕逗死你就设成2
@property(nonatomic)floatvolume;// [0-1] Default = 1,音量
代理:
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didStartSpeechUtterance:(AVSpeechUtterance*)utterance;
//开始
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance*)utterance;
//完成
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance*)utterance;
//暂停
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance*)utterance;
//恢复
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance*)utterance;
//取消
设置声音效果的方法,注意选英语则读不了汉语,但是汉语可以和英语混用,这个我已经试过了:
+ (nullableAVSpeechSynthesisVoice*)voiceWithLanguage:(nullableNSString*)languageCode;
下面给出详细的发音列表,中英文的已标出,其他语言请大家自己试吧:
avspeech支持的语言种类包括:
"[AVSpeechSynthesisVoice 0x978a0b0] Language: th-TH",
"[AVSpeechSynthesisVoice 0x977a450] Language: pt-BR",
"[AVSpeechSynthesisVoice 0x977a480] Language: sk-SK",
"[AVSpeechSynthesisVoice 0x978ad50] Language: fr-CA",
"[AVSpeechSynthesisVoice 0x978ada0] Language: ro-RO",
"[AVSpeechSynthesisVoice 0x97823f0] Language: no-NO",
"[AVSpeechSynthesisVoice 0x978e7b0] Language: fi-FI",
"[AVSpeechSynthesisVoice 0x978af50] Language: pl-PL",
"[AVSpeechSynthesisVoice 0x978afa0] Language: de-DE",
"[AVSpeechSynthesisVoice 0x978e390] Language: nl-NL",
"[AVSpeechSynthesisVoice 0x978b030] Language: id-ID",
"[AVSpeechSynthesisVoice 0x978b080] Language: tr-TR",
"[AVSpeechSynthesisVoice 0x978b0d0] Language: it-IT",
"[AVSpeechSynthesisVoice 0x978b120] Language: pt-PT",
"[AVSpeechSynthesisVoice 0x978b170] Language: fr-FR",
"[AVSpeechSynthesisVoice 0x978b1c0] Language: ru-RU",
"[AVSpeechSynthesisVoice 0x978b210] Language: es-MX",
"[AVSpeechSynthesisVoice 0x978b2d0] Language: zh-HK",中文(香港)粤语
"[AVSpeechSynthesisVoice 0x978b320] Language: sv-SE",
"[AVSpeechSynthesisVoice 0x978b010] Language: hu-HU",
"[AVSpeechSynthesisVoice 0x978b440] Language: zh-TW",中文(台湾)
"[AVSpeechSynthesisVoice 0x978b490] Language: es-ES",
"[AVSpeechSynthesisVoice 0x978b4e0] Language: zh-CN",中文(普通话)
"[AVSpeechSynthesisVoice 0x978b530] Language: nl-BE",
"[AVSpeechSynthesisVoice 0x978b580] Language: en-GB",英语(英国)
"[AVSpeechSynthesisVoice 0x978b5d0] Language: ar-SA",
"[AVSpeechSynthesisVoice 0x978b620] Language: ko-KR",
"[AVSpeechSynthesisVoice 0x978b670] Language: cs-CZ",
"[AVSpeechSynthesisVoice 0x978b6c0] Language: en-ZA",
"[AVSpeechSynthesisVoice 0x978aed0] Language: en-AU",
"[AVSpeechSynthesisVoice 0x978af20] Language: da-DK",
"[AVSpeechSynthesisVoice 0x978b810] Language: en-US",英语(美国)
"[AVSpeechSynthesisVoice 0x978b860] Language: en-IE",
"[AVSpeechSynthesisVoice 0x978b8b0] Language: hi-IN",
"[AVSpeechSynthesisVoice 0x978b900] Language: el-GR",
"[AVSpeechSynthesisVoice 0x978b950] Language: ja-JP"