在iOS中我們想把文本轉換成語音使用的AVFoundation框架中的AVSpeechSynthesizer。
比如我們要把“你好”讀出來代碼如下:
AVSpeechSynthesizer * synthesizer = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance * utterance = [[AVSpeechUtterance alloc] initWithString:@"你好"];
[synthesizer speakUtterance:utterance];
運行程序會自動讀出“你好”。
這里的AVSpeechSynthesizer這個是合成器
AVSpeechSynthesizer
AVSpeechSynthesizer有以下的屬性:
speaking
:是否在說話,只讀屬性
paused
:是否已經停止說話,只讀屬性;
- (BOOL)stopSpeakingAtBoundary:(AVSpeechBoundary)boundary;
:停止說話并且清除隊列;
- (BOOL)pauseSpeakingAtBoundary:(AVSpeechBoundary)boundary;
:暫停說話;
- (BOOL)continueSpeaking;
:繼續說話;
NSArray<AVAudioSessionChannelDescription *> *outputChannels
:輸出頻道,iOS10加入,默認是沒有的也就是不設置就在系統的聲道;
delegate
- AVSpeechSynthesizerDelegate
:重要的屬性:
其中包括以下代理:
// 開始
- (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;
// 將要讀到的位置
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance;
AVSpeechUtterance
AVSpeechUtterance這里相當于一個人的聲帶。
我們看下AVSpeechUtterance里面的屬性。
voice
:表示語言的種類,我們可以用AVSpeechSynthesisVoice的speechVoices來回去“目前”支持的的語言列表。
rate
:播放語音的速率,相當于講話的速度哈。其中有
AVF_EXPORT const float AVSpeechUtteranceMinimumSpeechRate NS_AVAILABLE_IOS(7_0); // 最小的說話速率(0.0)
AVF_EXPORT const float AVSpeechUtteranceMaximumSpeechRate NS_AVAILABLE_IOS(7_0); // 最大的說話速率(1.0)
AVF_EXPORT const float AVSpeechUtteranceDefaultSpeechRate NS_AVAILABLE_IOS(7_0); // 默認的說話速率(0.5)
pitchMultiplier
:這個屬性是表示聲音的音調,他一般在[0.5 - 2]
,默認值是1
。
volume
:是聲音的大小,也就是音量默認是1
。
postUtteranceDelay
和preUtteranceDelay
:這個屬性表示播放多個語音是,之間的間歇。post
表示播放下一句的間歇時間,pre
播放當前語句前的間歇時間。
下面是我打印的支持的聲音列表:
for (AVSpeechSynthesisVoice *voice in [AVSpeechSynthesisVoice speechVoices]) {
NSLog(@"%@", voice.language);
}
2017-01-15 16:28:59.969489 NSSpeechSythesizer[1204:766847] ar-SA // 沙特阿拉伯 ????
2017-01-15 16:28:59.969527 NSSpeechSythesizer[1204:766847] cs-CZ // 捷克共和國 ????
2017-01-15 16:28:59.969558 NSSpeechSythesizer[1204:766847] da-DK // 丹麥 ????
2017-01-15 16:28:59.969586 NSSpeechSythesizer[1204:766847] de-DE // 德國 ????
2017-01-15 16:28:59.969613 NSSpeechSythesizer[1204:766847] el-GR // 希臘 ????
2017-01-15 16:28:59.969786 NSSpeechSythesizer[1204:766847] en-AU // 澳大利亞 ????
2017-01-15 16:28:59.969971 NSSpeechSythesizer[1204:766847] en-GB // 聯合王國
2017-01-15 16:28:59.970051 NSSpeechSythesizer[1204:766847] en-IE // 愛爾蘭 ????
2017-01-15 16:28:59.970077 NSSpeechSythesizer[1204:766847] en-US // 美國 ????
2017-01-15 16:28:59.970351 NSSpeechSythesizer[1204:766847] en-ZA // 南非 ????
2017-01-15 16:28:59.970446 NSSpeechSythesizer[1204:766847] es-ES // 西班牙 ????
2017-01-15 16:28:59.970517 NSSpeechSythesizer[1204:766847] es-MX // 墨西哥 ????
2017-01-15 16:28:59.970547 NSSpeechSythesizer[1204:766847] fi-FI // 芬蘭 ????
2017-01-15 16:28:59.970575 NSSpeechSythesizer[1204:766847] fr-CA // 加拿大 ????
2017-01-15 16:28:59.970630 NSSpeechSythesizer[1204:766847] fr-FR // 法國 ????
2017-01-15 16:28:59.970660 NSSpeechSythesizer[1204:766847] he-IL // 以色列 ????
2017-01-15 16:28:59.970687 NSSpeechSythesizer[1204:766847] hi-IN // 印度 ????
2017-01-15 16:28:59.970714 NSSpeechSythesizer[1204:766847] hu-HU // 匈牙利 ????
2017-01-15 16:28:59.970741 NSSpeechSythesizer[1204:766847] id-ID // 印尼 ????
2017-01-15 16:28:59.970767 NSSpeechSythesizer[1204:766847] it-IT // 意大利????
2017-01-15 16:28:59.970793 NSSpeechSythesizer[1204:766847] ja-JP // 日本 ????
2017-01-15 16:28:59.970819 NSSpeechSythesizer[1204:766847] ko-KR // 韓國 ????
2017-01-15 16:28:59.970945 NSSpeechSythesizer[1204:766847] nl-BE // 比利時 ????
2017-01-15 16:28:59.971428 NSSpeechSythesizer[1204:766847] nl-NL // 荷蘭 ????
2017-01-15 16:28:59.971531 NSSpeechSythesizer[1204:766847] no-NO // 挪威 ????
2017-01-15 16:28:59.971559 NSSpeechSythesizer[1204:766847] pl-PL // 波蘭 ????
2017-01-15 16:28:59.971613 NSSpeechSythesizer[1204:766847] pt-BR // 巴西 ????
2017-01-15 16:28:59.971643 NSSpeechSythesizer[1204:766847] pt-PT // 葡萄牙 ????
2017-01-15 16:28:59.971669 NSSpeechSythesizer[1204:766847] ro-RO // 羅馬尼亞 ????
2017-01-15 16:28:59.971696 NSSpeechSythesizer[1204:766847] ru-RU // 俄羅斯聯邦 ????
2017-01-15 16:28:59.971722 NSSpeechSythesizer[1204:766847] sk-SK // 斯洛伐克 ????
2017-01-15 16:28:59.971749 NSSpeechSythesizer[1204:766847] sv-SE // 瑞典 ????
2017-01-15 16:28:59.971795 NSSpeechSythesizer[1204:766847] th-TH // 泰國 ????
2017-01-15 16:28:59.971891 NSSpeechSythesizer[1204:766847] tr-TR // 土耳其 ????
2017-01-15 16:28:59.971959 NSSpeechSythesizer[1204:766847] zh-CN // 中國 ????
2017-01-15 16:28:59.972096 NSSpeechSythesizer[1204:766847] zh-HK // 中國香港 ????????
2017-01-15 16:28:59.973301 NSSpeechSythesizer[1204:766847] zh-TW // 中國臺灣 ????
iOS 8添加了希伯來語-以色列
,在iOS 9或10中沒有添加新語言。