AndroidでTTS(TextToSpeech)を使用する方法

2019年6月24日

本稿ではAndroidでテキストを読み上げるTextTopSpeechの使用方法を説明します。

 

 

TextToSPeechの使用方法

TextToSpeechのインスタンスを生成する

public class TextToSpeechActivity extends Activity implements View.OnClickListener, TextToSpeech.OnInitListener {
  private TextToSpeech tts;
  private EditText inpText;
  private Button spkBtn, clsBtn;
  private SeekBar pitchBar, rateBar;
  private float pitch = 1.0f;
  private float rate = 1.0f;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // TTSのインスタンス生成
    tts = new TextToSpeech(this, this);

言語を設定する

  @Override
  public void onInit(int status) {
    // TODO Auto-generated method stub
    if(status == TextToSpeech.SUCCESS) {
      // 言語をUSに設定
      Locale locale = Locale.US;
      if(tts.isLanguageAvailable(locale) >= TextToSpeech.LANG_AVAILABLE) {
        tts.setLanguage(locale);
      }
      else {
        Log.e("TTS", "Not support locale.");
      }
    else {
      Log.e("TTS", "Init error.");
    }
  }

 

テキストを読み上げる

        if(sp.toString().length() > 0) {
          if(tts.isSpeaking()) {
            // 読上げ中ならストップ
            tts.stop();
          }
          // テキスト読上げ
          tts.speak(sp.toString(), TextToSpeech.QUEUE_FLUSH, null);
        }

 

TextToSpeechのサンプルコード

package com.sample.ithack
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.text.SpannableStringBuilder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
public class TextToSpeechActivity extends Activity implements View.OnClickListener, TextToSpeech.OnInitListener {
  private TextToSpeech tts;
  private EditText inpText;
  private Button spkBtn, clsBtn;
  private SeekBar pitchBar, rateBar;
  private float pitch = 1.0f;
  private float rate = 1.0f;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // TTSのインスタンス生成
    tts = new TextToSpeech(this, this);
    spkBtn = (Button)findViewById(R.id.speak_button_id);
    spkBtn.setOnClickListener(this);
    clsBtn = (Button)findViewById(R.id.clear_button_id);
    clsBtn.setOnClickListener(this);
    inpText = (EditText)findViewById(R.id.input_text_id);
    pitchBar = (SeekBar)findViewById(R.id.pitch_id);
    rateBar = (SeekBar)findViewById(R.id.rate_id);
    pitchBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
      }
      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
      }
      @Override
      public void onProgressChanged(SeekBar seekBar, int progress, 
        boolean fromUser) {
        // TODO Auto-generated method stub
        // pitchを計算
        pitch = progress / 50.0f;
        if(pitch < 0.1f)
          pitch = 0.1f;
      }
    });
    pitch = pitchBar.getProgress() / 50.0f;
    rateBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
      }
      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
      }
      @Override
      public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser) {
        // TODO Auto-generated method stub
        // speech rateを計算
        rate = progress / 50.0f;
        if(rate < 0.1f) 
          rate = 0.1f;
      }
    });
    rate = rateBar.getProgress() / 50.0f;
  }
@Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v == spkBtn) {
      if(tts != null) {
        if(tts.setPitch(pitch) == TextToSpeech.ERROR) {
          Log.e("TTS", "Ptich(" + pitch + ") set error.");
        }
        if(tts.setSpeechRate(rate) == TextToSpeech.ERROR) {
          Log.e("TTS", "Speech rate(" + rate + ") set error.");
        }
        SpannableStringBuilder sp = (SpannableStringBuilder)inpText.getText();
        if(sp.toString().length() > 0) {
          if(tts.isSpeaking()) {
            // 読上げ中ならストップ
            tts.stop();
          }
          // テキスト読上げ
          tts.speak(sp.toString(), TextToSpeech.QUEUE_FLUSH, null);
        }
      }
    }
    else if(v == clsBtn) {
      // テキストクリア
      inpText.setText("");
    }
  }
  @Override
  public void onInit(int status) {
    // TODO Auto-generated method stub
    if(status == TextToSpeech.SUCCESS) {
      // 言語をUSに設定
      Locale locale = Locale.US;
      if(tts.isLanguageAvailable(locale) >= TextToSpeech.LANG_AVAILABLE) {
        tts.setLanguage(locale);
      }
      else {
        Log.e("TTS", "Not support locale.");
      }
    else {
      Log.e("TTS", "Init error.");
    }
  }
  @Override
  protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    if(tts != null) {
      // リソースを解放
      tts.shutdown();
    }
  }
}