AVSpeechSynthesis使用详解

前言

从IOS5开始,IOS系统已经在siri上集成了语音合成的功能,但是是私有API。但是在IOS7,新增了一个简单的API----AVSpeechSynthesizer来做这件事情。

使用步骤

1.使用iOS系统的文字转语音需要用到AVFoundation框架

image.png

2.引入头文件#import

实现语音播报功能我们只要用到两个类:

AVSpeechSynthesizer
AVSpeechUtterance
- (AVSpeechSynthesisVoice *)synthesisVoice {
    if (!_synthesisVoice) {
        _synthesisVoice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
    }
    return _synthesisVoice;
}

- (AVSpeechSynthesizer *)synthesizer {
    if (!_synthesizer) {
        _synthesizer = [[AVSpeechSynthesizer alloc] init];
    }
    return _synthesizer;
}

3.读取文本内容

- (IBAction)readContentAction:(UIButton *)sender {
    NSString *content = self.textField.text;
    if (content.length <= 0) {
        content = @"什么都没有读个鬼啊";
    }
    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:content];
    utterance.rate = 0.5;
    utterance.voice = self.synthesisVoice;
    [self.synthesizer speakUtterance:utterance];
}

哈哈哈 就是这么简单!!!你学会了吗?

相关解释:

1.噪音

iOS7包含了一组可以用来合成的声音的嗓音,你可以自定义多种嗓音来合成。 AVSpeechSynthesisVoice:speechVoices返回当前可用的嗓音数组:

 PushNotification[11742:1211461] (
    "[AVSpeechSynthesisVoice 0x282bb4cd0] Language: ar-SA, Name: Maged, Quality: Default [com.apple.ttsbundle.Maged-compact]",
    "[AVSpeechSynthesisVoice 0x282bb5390] Language: cs-CZ, Name: Zuzana, Quality: Default [com.apple.ttsbundle.Zuzana-compact]",
    "[AVSpeechSynthesisVoice 0x282bb5090] Language: da-DK, Name: Sara, Quality: Default [com.apple.ttsbundle.Sara-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4770] Language: de-DE, Name: Anna, Quality: Default [com.apple.ttsbundle.Anna-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4a50] Language: de-DE, Name: Helena, Quality: Default [com.apple.ttsbundle.siri_female_de-DE_compact]",
    "[AVSpeechSynthesisVoice 0x282bb4dd0] Language: de-DE, Name: Martin, Quality: Default [com.apple.ttsbundle.siri_male_de-DE_compact]",
    "[AVSpeechSynthesisVoice 0x282bb4e50] Language: el-GR, Name: Melina, Quality: Default [com.apple.ttsbundle.Melina-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4850] Language: en-AU, Name: Catherine, Quality: Default [com.apple.ttsbundle.siri_female_en-AU_compact]",
    "[AVSpeechSynthesisVoice 0x282bb49d0] Language: en-AU, Name: Gordon, Quality: Default [com.apple.ttsbundle.siri_male_en-AU_compact]",
    "[AVSpeechSynthesisVoice 0x282bb4b50] Language: en-AU, Name: Karen, Quality: Default [com.apple.ttsbundle.Karen-compact]",
    "[AVSpeechSynthesisVoice 0x282bb47e0] Language: en-GB, Name: Arthur, Quality: Default [com.apple.ttsbundle.siri_male_en-GB_compact]",

    "[AVSpeechSynthesisVoice 0x282bb48d0] Language: en-GB, Name: Daniel, Quality: Default [com.apple.ttsbundle.Daniel-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4d90] Language: en-GB, Name: Martha, Quality: Default [com.apple.ttsbundle.siri_female_en-GB_compact]",
    "[AVSpeechSynthesisVoice 0x282bb4ed0] Language: en-IE, Name: Moira, Quality: Default [com.apple.ttsbundle.Moira-compact]",
    "[AVSpeechSynthesisVoice 0x282bb45b0] Language: en-US, Name: Aaron, Quality: Default [com.apple.ttsbundle.siri_male_en-US_compact]",
    "[AVSpeechSynthesisVoice 0x282bb4990] Language: en-US, Name: Fred, Quality: Default [com.apple.speech.synthesis.voice.Fred]",
    "[AVSpeechSynthesisVoice 0x282bb4f50] Language: en-US, Name: Nicky, Quality: Default [com.apple.ttsbundle.siri_female_en-US_compact]",
    "[AVSpeechSynthesisVoice 0x282bb5050] Language: en-US, Name: Samantha, Quality: Default [com.apple.ttsbundle.Samantha-compact]",
    "[AVSpeechSynthesisVoice 0x282bb5150] Language: en-ZA, Name: Tessa, Quality: Default [com.apple.ttsbundle.Tessa-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4f10] Language: es-ES, Name: Monica, Quality: Default [com.apple.ttsbundle.Monica-compact]",
    "[AVSpeechSynthesisVoice 0x282bb5010] Language: es-MX, Name: Paulina, Quality: Default [com.apple.ttsbundle.Paulina-compact]",
    "[AVSpeechSynthesisVoice 0x282bb50d0] Language: fi-FI, Name: Satu, Quality: Default [com.apple.ttsbundle.Satu-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4550] Language: fr-CA, Name: Amelie, Quality: Default [com.apple.ttsbundle.Amelie-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4910] Language: fr-FR, Name: Daniel, Quality: Default [com.apple.ttsbundle.siri_male_fr-FR_compact]",
    "[AVSpeechSynthesisVoice 0x282bb4d10] Language: fr-FR, Name: Marie, Quality: Default [com.apple.ttsbundle.siri_female_fr-FR_compact]",
    "[AVSpeechSynthesisVoice 0x282bb5190] Language: fr-FR, Name: Thomas, Quality: Default [com.apple.ttsbundle.Thomas-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4810] Language: he-IL, Name: Carmit, Quality: Default [com.apple.ttsbundle.Carmit-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4c10] Language: hi-IN, Name: Lekha, Quality: Default [com.apple.ttsbundle.Lekha-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4d50] Language: hu-HU, Name: Mariska, Quality: Default [com.apple.ttsbundle.Mariska-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4890] Language: id-ID, Name: Damayanti, Quality: Default [com.apple.ttsbundle.Damayanti-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4670] Language: it-IT, Name: Alice, Quality: Default [com.apple.ttsbundle.Alice-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4a10] Language: ja-JP, Name: Hattori, Quality: Default [com.apple.ttsbundle.siri_male_ja-JP_compact]",
    "[AVSpeechSynthesisVoice 0x282bb4b90] Language: ja-JP, Name: Kyoko, Quality: Default [com.apple.ttsbundle.Kyoko-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4fd0] Language: ja-JP, Name: O-ren, Quality: Default [com.apple.ttsbundle.siri_female_ja-JP_compact]",
    "[AVSpeechSynthesisVoice 0x282bb52d0] Language: ko-KR, Name: Yuna, Quality: Default [com.apple.ttsbundle.Yuna-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4950] Language: nl-BE, Name: Ellen, Quality: Default [com.apple.ttsbundle.Ellen-compact]",
    "[AVSpeechSynthesisVoice 0x282bb5210] Language: nl-NL, Name: Xander, Quality: Default [com.apple.ttsbundle.Xander-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4f90] Language: no-NO, Name: Nora, Quality: Default [com.apple.ttsbundle.Nora-compact]",
    "[AVSpeechSynthesisVoice 0x282bb5350] Language: pl-PL, Name: Zosia, Quality: Default [com.apple.ttsbundle.Zosia-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4c90] Language: pt-BR, Name: Luciana, Quality: Default [com.apple.ttsbundle.Luciana-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4ad0] Language: pt-PT, Name: Joana, Quality: Default [com.apple.ttsbundle.Joana-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4a90] Language: ro-RO, Name: Ioana, Quality: Default [com.apple.ttsbundle.Ioana-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4e90] Language: ru-RU, Name: Milena, Quality: Default [com.apple.ttsbundle.Milena-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4bd0] Language: sk-SK, Name: Laura, Quality: Default [com.apple.ttsbundle.Laura-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4720] Language: sv-SE, Name: Alva, Quality: Default [com.apple.ttsbundle.Alva-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4b10] Language: th-TH, Name: Kanya, Quality: Default [com.apple.ttsbundle.Kanya-compact]",
    "[AVSpeechSynthesisVoice 0x282bb5250] Language: tr-TR, Name: Yelda, Quality: Default [com.apple.ttsbundle.Yelda-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4c50] Language: zh-CN, Name: Li-mu, Quality: Default [com.apple.ttsbundle.siri_male_zh-CN_compact]",
    "[AVSpeechSynthesisVoice 0x282bb51d0] Language: zh-CN, Name: Ting-Ting, Quality: Default [com.apple.ttsbundle.Ting-Ting-compact]",
    "[AVSpeechSynthesisVoice 0x282bb5290] Language: zh-CN, Name: Yu-shu, Quality: Default [com.apple.ttsbundle.siri_female_zh-CN_compact]",
    "[AVSpeechSynthesisVoice 0x282bb5110] Language: zh-HK, Name: Sin-Ji, Quality: Default [com.apple.ttsbundle.Sin-Ji-compact]",
    "[AVSpeechSynthesisVoice 0x282bb4e10] Language: zh-TW, Name: Mei-Jia, Quality: Default [com.apple.ttsbundle.Mei-Jia-compact]"
)

我们可以通过下面的语句创建一个特殊的噪音

AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];

2.话语

话语代表一段讲话,可以传递到语音合成器来生成一段语音流。话语可以用待发音的字符串来创建:

AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@"Hello world!"];

我们可以设置它的嗓音属性voice:

utterence.voice = voice;

还有其他各种属性,包括:rate,volume和pitchMultiplier。例如,稍微放慢语速:

utterance.rate = 0.7;

当话语创建完成,可以把它放到语音合成器中。语音合成器会生成音频:

AVSpeechSynthesizer *speechSynthesizer = [[AVSpeechSynthesizer alloc] init];
[speechSynthesizer speakUtterance:utterance];

话语会被合成器放到队列中,所以不用等待上一个结束,可以连续传送。但是,如果你尝试传送一个已经在队列中的话语,系统会抛出异常。

实现
本文的例子是一个关于多语言问候语的应用。这充分体现了语音合成的通用性。
注意:定义话语的字符串都是罗马字符,例如中文的'Ni hao'。示例项目定义了一个类,可以生成一组嗓音对应的话语。
项目有一个选择器供用户选择语言,然后按按钮可以听到对应的生成的语音。

总结
在IOS7,语音合成器非常简单,而且包含很多语言。合理的使用,可以提高应用的访问性,而不用时刻用你的手和眼来操作应用。

你可能感兴趣的:(AVSpeechSynthesis使用详解)