1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AVSpeechSynthesizerでの初回音声読み上げが遅いのを直す

Posted at

iOSで音声読み上げをする時のコード。

import AVFoundation

class SpeechViewController: UIViewController {
    private let synthesizer = AVSpeechSynthesizer()

    private func speech(_ text: String) {
        // 初回のみ0.1~2秒遅れて読み上げられる
        let utterance = AVSpeechUtterance(string: text)
        synthesizer.speak(utterance)
    }

ただこれだと初回の読み上げのみ0.1~2秒遅れて読み上げられる。
2回目以降は遅延なく読み上げられる。
なのでviewDidLoadで空文字を1回読んでおく。

import AVFoundation

class SpeechViewController: UIViewController {
    private let synthesizer = AVSpeechSynthesizer()

    override func viewDidLoad() {
        super.viewDidLoad()

        // 空文字を読んでおく
        let utterance = AVSpeechUtterance(string: "")
        utterance.volume = 0
        synthesizer.speak(utterance)
    }

    private func speech(_ text: String) {
        // 遅延なく読み上げられる
        let utterance = AVSpeechUtterance(string: text)
        synthesizer.speak(utterance)
    }
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?