【Android】テキスト読み上げを行うには ~ TTS / Text To Speech ~

■ はじめに

Android の テキスト読み上げ を行うための方法をメモ。

 ■ 読み上げ機能

 Java

初期化

... implements TextToSpeech.OnInitListener {
  private TextToSpeech tts;

  protected void onCreate(... 
    ... 

    // TextToSpeechオブジェクトの生成 ★初期化★
    this.tts = new TextToSpeech(this, this);
  }

機能実行

if (this.tts.isSpeaking()) {
  this.tts.stop();
}

this.tts.speak(value, TextToSpeech.QUEUE_FLUSH, null);

サポートしている言語かどうか / 言語を設定

if (this.tts.isLanguageAvailable(this.locale) >= TextToSpeech.LANG_AVAILABLE) {
  this.tts.setLanguage(this.locale);
}

 ■ サンプル

Java

 デザイン

 * EditText x 1
 * RadioGroup x 1, RadioButton x 2
 * Button x 1

 MainActivity.java

import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.Locale;

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
    private TextToSpeech tts;
    private Locale locale = Locale.ENGLISH;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // TextToSpeechオブジェクトの生成
        this.tts = new TextToSpeech(this, this);

        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.langRadioGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            RadioButton jpRadioButton = (RadioButton) findViewById(R.id.jpRadioButton);

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton selectedRadioButton = (RadioButton) findViewById(checkedId);
                Log.v("checked", selectedRadioButton.getText().toString());

                if (jpRadioButton.equals(selectedRadioButton)) {
                    locale = Locale.JAPAN;
                } else {
                    locale = Locale.ENGLISH;
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != this.tts) {
            // TextToSpeechのリソースを解放する
            this.tts.shutdown();
        }
    }

    @Override
    public void onInit(int status) {
        if (TextToSpeech.SUCCESS == status) {
            if (this.tts.isLanguageAvailable(this.locale) >= TextToSpeech.LANG_AVAILABLE) {
                this.tts.setLanguage(this.locale);
            } else {
                Log.d("", "Error SetLocale");
            }
        } else {
            Log.d("", "Error Init");
        }
    }

    public void onClickButton(View view) {
        try {
            String value = ((EditText)findViewById(R.id.inputEditText)).getText().toString();
            this.speechText(value);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        Toast.makeText(this, "Done!!", Toast.LENGTH_LONG).show();
    }

    private void speechText(String value) {
        if (0 < value.length()) {
            if (this.tts.isSpeaking()) {
                // 読み上げ中なら止める
                this.tts.stop();
            }

            // 読み上げ開始
            this.tts.speak(value, TextToSpeech.QUEUE_FLUSH, null);
        }
    }
}

 参考文献

https://techbooster.org/android/application/550/