談?wù)刬OS自帶文字轉(zhuǎn)語音

前言

? ? 之前自己的項(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)入框架

隨后,導(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",英語(英國(guó))

"[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",英語(美國(guó))

"[AVSpeechSynthesisVoice 0x978b860] Language: en-IE",

"[AVSpeechSynthesisVoice 0x978b8b0] Language: hi-IN",

"[AVSpeechSynthesisVoice 0x978b900] Language: el-GR",

"[AVSpeechSynthesisVoice 0x978b950] Language: ja-JP"

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容