前言
? ? 之前自己的項目中曾經使用過訊飛的文字轉語音技術,但是通過實際測試,發現它的免費在線轉語音不是很好,受網絡的影響聲音經常斷斷續續的;而它的離線轉語音價格有太貴,最便宜的要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"