1
4

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 1 year has passed since last update.

【Swift】スマホを喋らせる(合成音声)

Posted at

実装

import SwiftUI
import AVFoundation

struct ContentView: View {
    let synthesizer = AVSpeechSynthesizer()
    var body: some View {
        Button("スピーチ") {
            speech()
        }
    }
    func speech() {
        let text = AVSpeechUtterance(string: "こんにちは、ここの文字を読み上げます")
        let language = AVSpeechSynthesisVoice(language: "ja-JP")
        text.voice = language
        synthesizer.speak(text)
    }
}

解説

AVFoundationのインポート

import AVFoundation

インスタンスの生成

let synthesizer = AVSpeechSynthesizer()

読み上げ文を設定

let text = AVSpeechUtterance(string: "こんにちは、ここの文字を読み上げます")

言語の設定

let language = AVSpeechSynthesisVoice(language: "ja-JP")
text.voice = language

読み上げ開始

synthesizer.speak(text)
1
4
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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?