---
# System prepended metadata

title: AVFoundation - AVSpeechSynthesizer

---

# AVFoundation - AVSpeechSynthesizer
先匯入AVFoundation套件(函式庫)，透過AVSpeechUtterance生成說話的內容，之後透過發聲器AVSpeechSynthesizer講話。

## AVSpeechUtterance
- 語言( .voice )：AVSpeechSynthesisVoice(language:)
- 音調(.pitchMultiplier)：型態：Float、範圍：0.5~2
- 速度( .rate)：型態：Float、範圍：0~1
- 音量( .volume)：型態：Float、範圍：0~1
- 延遲停頓時間
.postUtteranceDelay：說完後的停頓時間、型態：Double
.preUtteranceDelay：說之前的停頓時間、型態：Double

~~~
        self.synthesizer = [[AVSpeechSynthesizer alloc] init];
        NSString *speechString = item.TCTTSPlayerItem_Sentence;
        if (!speechString) {
            speechString = item.TCTTSPlayerItem_SentenceAnother;
        }
        // 在數字與數字之間加上[，就會一個一個數字唸
        speechString = [self addBetweenBrackets:speechString];
        AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:speechString];
        [utterance setVolume:1.0];
        [utterance setRate:0.5];
        AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-TW"];
        [utterance setVoice: voice];
        self.synthesizer.delegate = self;
        [self.synthesizer speakUtterance:utterance];
~~~


## 注意事項
- 文字與數字之間需要加符號，否則有可能順序會錯亂

- 數字間若不想被唸成"一千一百一十一"整串的數字，也須用符號隔開
