序:
iOS中语音朗读是AVFoundation框架下面的功能,实现起来非常简单
1.选一种朗读的嗓音
2.生成要朗读的表达
3.生成合成器开始朗读
可以使用合成器的代理来监控朗读过程
一、嗓音
1.所有的嗓音
用下面这段代码能够获取到iOS设备支持的所有的嗓音
NSArray *voices = [AVSpeechSynthesisVoice speechVoices];
结果如下:
Language: ar-SA, Name: Maged, Quality: Default [com.apple.ttsbundle.Maged-compact]
Language: cs-CZ, Name: Zuzana, Quality: Default [com.apple.ttsbundle.Zuzana-compact]
Language: da-DK, Name: Sara, Quality: Default [com.apple.ttsbundle.Sara-compact]
Language: de-DE, Name: Anna, Quality: Default [com.apple.ttsbundle.Anna-compact]
Language: de-DE, Name: Helena, Quality: Default [com.apple.ttsbundle.siri_female_de-DE_compact]
Language: de-DE, Name: Martin, Quality: Default [com.apple.ttsbundle.siri_male_de-DE_compact]
Language: el-GR, Name: Melina, Quality: Default [com.apple.ttsbundle.Melina-compact]
Language: en-AU, Name: Catherine, Quality: Default [com.apple.ttsbundle.siri_female_en-AU_compact]
Language: en-AU, Name: Gordon, Quality: Default [com.apple.ttsbundle.siri_male_en-AU_compact]
Language: en-AU, Name: Karen, Quality: Default [com.apple.ttsbundle.Karen-compact]
Language: en-GB, Name: Arthur, Quality: Default [com.apple.ttsbundle.siri_male_en-GB_compact]
Language: en-GB, Name: Daniel, Quality: Default [com.apple.ttsbundle.Daniel-compact]
Language: en-GB, Name: Martha, Quality: Default [com.apple.ttsbundle.siri_female_en-GB_compact]
Language: en-IE, Name: Moira, Quality: Default [com.apple.ttsbundle.Moira-compact]
Language: en-US, Name: Aaron, Quality: Default [com.apple.ttsbundle.siri_male_en-US_compact]
Language: en-US, Name: Fred, Quality: Default [com.apple.speech.synthesis.voice.Fred]
Language: en-US, Name: Nicky, Quality: Default [com.apple.ttsbundle.siri_female_en-US_compact]
Language: en-US, Name: Samantha, Quality: Default [com.apple.ttsbundle.Samantha-compact]
Language: en-ZA, Name: Tessa, Quality: Default [com.apple.ttsbundle.Tessa-compact]
Language: es-ES, Name: Monica, Quality: Default [com.apple.ttsbundle.Monica-compact]
Language: es-MX, Name: Paulina, Quality: Default [com.apple.ttsbundle.Paulina-compact]
Language: fi-FI, Name: Satu, Quality: Default [com.apple.ttsbundle.Satu-compact]
Language: fr-CA, Name: Amelie, Quality: Default [com.apple.ttsbundle.Amelie-compact]
Language: fr-FR, Name: Daniel, Quality: Default [com.apple.ttsbundle.siri_male_fr-FR_compact]
Language: fr-FR, Name: Marie, Quality: Default [com.apple.ttsbundle.siri_female_fr-FR_compact]
Language: fr-FR, Name: Thomas, Quality: Default [com.apple.ttsbundle.Thomas-compact]
Language: he-IL, Name: Carmit, Quality: Default [com.apple.ttsbundle.Carmit-compact]
Language: hi-IN, Name: Lekha, Quality: Default [com.apple.ttsbundle.Lekha-compact]
Language: hu-HU, Name: Mariska, Quality: Default [com.apple.ttsbundle.Mariska-compact]
Language: id-ID, Name: Damayanti, Quality: Default [com.apple.ttsbundle.Damayanti-compact]
Language: it-IT, Name: Alice, Quality: Default [com.apple.ttsbundle.Alice-compact]
Language: ja-JP, Name: Hattori, Quality: Default [com.apple.ttsbundle.siri_male_ja-JP_compact]
Language: ja-JP, Name: Kyoko, Quality: Default [com.apple.ttsbundle.Kyoko-compact]
Language: ja-JP, Name: O-ren, Quality: Default [com.apple.ttsbundle.siri_female_ja-JP_compact]
Language: ko-KR, Name: Yuna, Quality: Default [com.apple.ttsbundle.Yuna-compact]
Language: nl-BE, Name: Ellen, Quality: Default [com.apple.ttsbundle.Ellen-compact]
Language: nl-NL, Name: Xander, Quality: Default [com.apple.ttsbundle.Xander-compact]
Language: no-NO, Name: Nora, Quality: Default [com.apple.ttsbundle.Nora-compact]
Language: pl-PL, Name: Zosia, Quality: Default [com.apple.ttsbundle.Zosia-compact]
Language: pt-BR, Name: Luciana, Quality: Default [com.apple.ttsbundle.Luciana-compact]
Language: pt-PT, Name: Joana, Quality: Default [com.apple.ttsbundle.Joana-compact]
Language: ro-RO, Name: Ioana, Quality: Default [com.apple.ttsbundle.Ioana-compact]
Language: ru-RU, Name: Milena, Quality: Default [com.apple.ttsbundle.Milena-compact]
Language: sk-SK, Name: Laura, Quality: Default [com.apple.ttsbundle.Laura-compact]
Language: sv-SE, Name: Alva, Quality: Default [com.apple.ttsbundle.Alva-compact]
Language: th-TH, Name: Kanya, Quality: Default [com.apple.ttsbundle.Kanya-compact]
Language: tr-TR, Name: Yelda, Quality: Default [com.apple.ttsbundle.Yelda-compact]
Language: zh-CN, Name: Li-mu (Enhanced), Quality: Enhanced [com.apple.ttsbundle.siri_male_zh-CN_premium]
Language: zh-CN, Name: Ting-Ting (Enhanced), Quality: Enhanced [com.apple.ttsbundle.Ting-Ting-premium]
Language: zh-CN, Name: Ting-Ting (Enhanced), Quality: Enhanced [com.apple.ttsbundle.Ting-Ting-premium]
Language: zh-CN, Name: Yu-shu (Enhanced), Quality: Enhanced [com.apple.ttsbundle.siri_female_zh-CN_premium]
Language: zh-CN, Name: Li-mu, Quality: Default [com.apple.ttsbundle.siri_male_zh-CN_compact]
Language: zh-CN, Name: Ting-Ting, Quality: Default [com.apple.ttsbundle.Ting-Ting-compact]
Language: zh-CN, Name: Yu-shu, Quality: Default [com.apple.ttsbundle.siri_female_zh-CN_compact]
Language: zh-HK, Name: Sin-Ji, Quality: Default [com.apple.ttsbundle.Sin-Ji-compact]
Language: zh-TW, Name: Mei-Jia, Quality: Default [com.apple.ttsbundle.Mei-Jia-compact]
2. 生成嗓音实例
- 通过地区语言代码获取嗓音对象
// 获取当前地区语言代码
NSString *languageCode = [AVSpeechSynthesisVoice currentLanguageCode];
// 通过地区语言代码生成语言对象
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-TW"];
- 通过嗓音标识符获取嗓音对象
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithIdentifier:@"com.apple.ttsbundle.siri_male_zh-CN_compact"];
二、朗读方式(AVSpeechUtterance)
1.生成朗读方式对象
AVSpeechUtterance
提供了两种类方法和两种实例方法来生成朗读方式对象
+ (instancetype)speechUtteranceWithString:(NSString *)string;
+ (instancetype)speechUtteranceWithAttributedString:(NSAttributedString *)string NS_AVAILABLE_IOS(10_0);
- (instancetype)initWithString:(NSString *)string;
- (instancetype)initWithAttributedString:(NSAttributedString *)string NS_AVAILABLE_IOS(10_0);
- 生成一个实例
NSString *speechString = @"小伙子,你真帅"
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:speechString];
2.设置嗓音
utterance.voice = voice;
3.设置速度
utterance.rate = 0.5;
Values are pinned between AVSpeechUtteranceMinimumSpeechRate and AVSpeechUtteranceMaximumSpeechRate.
4.设置音高
utterance.pitchMultiplier = 1;
音高的取值范围是[0.5 - 2],默认值为1。
5.设置音量
utterance.volume = 1;
音量的取值范围[0-1],默认为1,一般不用设置
三、朗读合成器(AVSpeechSynthesizer)
1.生成合成器
AVSpeechSynthesizer *speechSynthesizer = [[AVSpeechSynthesizer alloc] init];
2.开始朗读
[speechSynthesizer speakUtterance:utterance];
代理方法
- (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;