LoginSignup
0
1

More than 3 years have passed since last update.

MIT App Inventor で試しに作った「ボタンを押したら音声合成でしゃべる Androidアプリ」を Kotlin で作ってみた

Last updated at Posted at 2021-03-20

はじめに

以前、このような記事を書いたのですが、これの Kotlin版です。

●MIT App Inventor で試しに作った「ボタンを押したら音声合成でしゃべる Androidアプリ」を Java で作ってみた - Qiita
 https://qiita.com/youtoy/items/a0c26a3b8a671a2bb707#%E3%82%BD%E3%83%BC%E3%82%B9%E3%82%B3%E3%83%BC%E3%83%89%E5%85%A8%E4%BD%93

上記の「MIT App Inventor で試しに作ったアプリ」の話は以下で書いています。

●ビジュアルプログラミングで Androidアプリ開発ができる「MIT App Inventor」を試す(ボタンを押したら音声でしゃべるアプリを作る) - Qiita
 https://qiita.com/youtoy/items/93f7c786ff9d664d3032

参考情報

今回の内容は、以下を見つつ実装しました。

●Kotlinで合成音声を出力するアプリを作成[Android] | トモブログ
 https://lifetime-engineer.com/android-kotlin-speech/

実装内容

プロジェクト作成

プロジェクトの新規作成の設定は、冒頭で掲載した記事とほぼ同じです。

違いはプロジェクトの設定の以下の内容くらいです。
 ・プロジェクト名: TTS_test01_Kotlin
 ・言語: Kotlin

アプリ画面のデザイン(ボタンの追加)

アプリ上にボタンを配置する部分は、冒頭で掲載した記事と同じ手順です。

Kotlin での実装(追加・変更部分)

以下に、デフォルトのコードから変更を加えた部分や追加した部分を書いていきます。
そして、最後にコード全体の内容を示します。

冒頭のインポート

import android.speech.tts.TextToSpeech
import android.widget.Button
import android.util.Log
import android.view.View
import java.util.*

インポート直下

class MainActivity : AppCompatActivity(), View.OnClickListener, TextToSpeech.OnInitListener {
    private var tts : TextToSpeech? = null

onCreate内

        tts = TextToSpeech(this,this)
        val button = findViewById (R.id.button) as Button;
        button.setOnClickListener(this)

onCreate の下

    override fun onClick(view: View){
        SpeechText("この内容をしゃべるよ")
    }

    override fun onInit(status: Int) {
        if (status == TextToSpeech.SUCCESS) {
            Log.d("tts", "TextToSpeech初期化成功")
        }
    }

    private fun SpeechText(text:String){
        tts?.setLanguage(Locale.JAPANESE)
        tts?.speak(text,TextToSpeech.QUEUE_FLUSH,null,"ID")
    }
}

Kotlin での実装(全体)

以下が今回のコード全体の内容です。

package com.example.tts_test01_kotlin

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

import android.speech.tts.TextToSpeech
import android.widget.Button
import android.util.Log
import android.view.View
import java.util.*

class MainActivity : AppCompatActivity(), View.OnClickListener, TextToSpeech.OnInitListener {
    private var tts : TextToSpeech? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        tts = TextToSpeech(this,this)
        val button = findViewById (R.id.button) as Button;
        button.setOnClickListener(this)
    }

    override fun onClick(view: View){
        SpeechText("この内容をしゃべるよ")
    }

    override fun onInit(status: Int) {
        if (status == TextToSpeech.SUCCESS) {
            Log.d("tts", "TextToSpeech初期化成功")
        }
    }

    private fun SpeechText(text:String){
        tts?.setLanguage(Locale.JAPANESE)
        tts?.speak(text,TextToSpeech.QUEUE_FLUSH,null,"ID")
    }
}

おわりに

とりあえず動く、という状態のものを、Kotlin を使って短いコードで書いてみました(本当はもう少し実装すべき部分がありそう)。
ちょっと前に書いた Java とずいぶん書き方が違った感じになりました。

0
1
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
0
1