前言
? ? 之前自己的項(xiàng)目中曾經(jīng)使用過訊飛的文字轉(zhuǎn)語音技術(shù)甸鸟,但是通過實(shí)際測(cè)試,發(fā)現(xiàn)它的免費(fèi)在線轉(zhuǎn)語音不是很好兵迅,受網(wǎng)絡(luò)的影響聲音經(jīng)常斷斷續(xù)續(xù)的抢韭;而它的離線轉(zhuǎn)語音價(jià)格有太貴,最便宜的要8000RMB/2000裝機(jī)量喷兼,令許多開發(fā)者望而止步篮绰。那么既然支付不了訊飛那昂貴的費(fèi)用,iOS自帶的文字轉(zhuǎn)語音也是我們不錯(cuò)的選擇季惯,只是他的發(fā)音效果不夠理想吠各,不過還可以接受臀突。今天我就帶大家學(xué)習(xí)iOS自帶的文字轉(zhuǎn)語音!
第一步:導(dǎo)入系統(tǒng)框架
使用iOS系統(tǒng)的文字轉(zhuǎn)語音需要用到AVFoundation框架贾漏,這個(gè)不需要詳談了吧:
隨后候学,導(dǎo)入
#import<AVFoundation/AVSpeechSynthesis.h>
需要代理的話導(dǎo)入代理
@interfaceViewController()<AVSpeechSynthesizerDelegate>
第二步:創(chuàng)建對(duì)象
在這里對(duì)象最好創(chuàng)建為全局的,便于我們控制纵散;當(dāng)然如果你不需要暫停梳码、繼續(xù)播放等操作的話可以創(chuàng)建一個(gè)局部的。
{
AVSpeechSynthesizer*av;
}
隨后我們創(chuàng)建一個(gè)簡(jiǎn)單的按鈕來操控他:
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]) {
//如果暫停則恢復(fù)伍掀,會(huì)從暫停的地方繼續(xù)
[avcontinueSpeaking];
sender.selected=!sender.selected;
}else{
//初始化對(duì)象
av= [[AVSpeechSynthesizeralloc]init];
av.delegate=self;//掛上代理
AVSpeechUtterance*utterance = [[AVSpeechUtterancealloc]initWithString:@"錦瑟無端五十弦掰茶,一弦一柱思華年。莊生曉夢(mèng)迷蝴蝶蜜笤,望帝春心托杜鵑濒蒋。滄海月明珠有淚,藍(lán)田日暖玉生煙把兔。此情可待成追憶沪伙,只是當(dāng)時(shí)已惘然。"];//需要轉(zhuǎn)換的文字
utterance.rate=0.5;// 設(shè)置語速县好,范圍0-1围橡,注意0最慢,1最快缕贡;AVSpeechUtteranceMinimumSpeechRate最慢翁授,AVSpeechUtteranceMaximumSpeechRate最快
AVSpeechSynthesisVoice*voice = [AVSpeechSynthesisVoicevoiceWithLanguage:@"zh-CN"];//設(shè)置發(fā)音,這是中文普通話
utterance.voice= voice;
[avspeakUtterance:utterance];//開始
sender.selected=!sender.selected;
}
}else{
//[av stopSpeakingAtBoundary:AVSpeechBoundaryWord];//感覺效果一樣善绎,對(duì)應(yīng)代理>>>取消
[avpauseSpeakingAtBoundary:AVSpeechBoundaryWord];//暫停
sender.selected=!sender.selected;
}
//[utterance release];//需要關(guān)閉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(@"---恢復(fù)播放");
}
- (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;//取消播放黔漂,將會(huì)完全停止
- (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;//暫停播放诫尽,將會(huì)保存進(jìn)度
- (BOOL)continueSpeaking;//恢復(fù)播放
取消與暫停的兩個(gè)效果我感覺都一樣:
typedefNS_ENUM(NSInteger, AVSpeechBoundary) {
AVSpeechBoundaryImmediate,
AVSpeechBoundaryWord
}NS_ENUM_AVAILABLE_IOS(7_0);
@property(nonatomic)floatrate;// 設(shè)置語速禀酱,范圍0-1,注意0最慢牧嫉,1最快剂跟;AVSpeechUtteranceMinimumSpeechRate最慢,AVSpeechUtteranceMaximumSpeechRate最快
@property(nonatomic)floatpitchMultiplier;// [0.5 - 2] Default = 1酣藻,聲調(diào)曹洽,不怕逗死你就設(shè)成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;
//恢復(fù)
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance*)utterance;
//取消
設(shè)置聲音效果的方法辽剧,注意選英語則讀不了漢語送淆,但是漢語可以和英語混用,這個(gè)我已經(jīng)試過了:
+ (nullableAVSpeechSynthesisVoice*)voiceWithLanguage:(nullableNSString*)languageCode;
下面給出詳細(xì)的發(fā)音列表怕轿,中英文的已標(biāo)出偷崩,其他語言請(qǐng)大家自己試吧:
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",中文(臺(tái)灣)
"[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"