ご注意!
Google Translate API v1が2011年12月1日にサービスが終了いたします。添付のサンプルソースでは、Google Translate API v1を利用していますので、翻訳が正常に機能しない場合がございます。ご承知おきください。
Android Market公開を目指してAndroidアプリを開発する!(音声認識機能実装編)の続きです。
We’ve introduced a new feature in version 1.6 of the Android platform: Text-To-Speech (TTS). Also known as “speech synthesis”, TTS enables your Android device to “speak” text of different languages.
Android Developers Blog: An introduction to Text-To-Speech in Androidで、テキストデータの読み上げが可能になったのを知りました。
前回に引き続き、今回もTranslatAIRにはない機能、
翻訳結果をしゃべる機能
を実装していきたいと思います。
The TTS engine that ships with the Android platform supports a number of languages: English, French, German, Italian and Spanish. Also, depending on which side of the Atlantic you are on, American and British accents for English are both supported.
Android Developers Blog: An introduction to Text-To-Speech in Androidより引用。
対応している言語は、
- 英語
- フランス語
- ドイツ語
- イタリア語
- スペイン語
の五ヶ国語のようです。
日本語には対応していないようですね。残念です。。。
前回サンプルの画面に「play」ボタンを追加しました。「voice」ボタンと「tweet」ボタンの間です。
XMLには、以下の部分を追加しました。
<Button android:id="@+id/play" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="play" />
それでは、「play」ボタンが押された時の処理に入っていきたい思います。
TextToSpeechクラスを使用します。
音声認識機能(Voice Recognition)の時と同じように、TextToSpeechのエンジンが利用できるか判断し、初期化処理をしていきます。
try {
Intent intent = new Intent();
intent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(intent, TTS_DATA_CHECK_REQUEST_CODE);
}
catch (Exception ex) {
playable = false;
}
コールバックで、TextToSpeechクラスのインスタンスを生成します。
else if (requestCode == TTS_DATA_CHECK_REQUEST_CODE) {
// TextToSpeech初期化の結果
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// TextToSpeechが利用できる場合
if (tts == null) {
try {
tts = new TextToSpeech(getApplicationContext(), this);
playable = true;
}
catch (Exception ex) {
playable = false;
}
}
}
else {
// TextToSpeechのリソースがインストールされていない場合
try {
Intent intent = new Intent();
intent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(intent);
}
catch (Exception ex) {
playable = false;
}
}
}
但し、TextToSpeechエンジンは音声読み上げのためのリソースが、Android端末に入っていないと機能しません。
インストールは、「設定」→「テキスト読み上げ」→「音声データをインストール」からできます。
ですが、リソースがインストールされていない場合には、18~20行のようにすると、インストールを促すように出来るのです。
Android端末の初期状態ではインストールされていないようなので、これは実装することをオススメします。
前回同様、コールバックのリクエストコード(requestCode)は、メンバー変数で定数として定義しています。
private static final int TTS_DATA_CHECK_REQUEST_CODE = 9998;
最後に、「play」ボタンのクリックイベントリスナーを実装します。
Button btnPlay = (Button)findViewById(R.id.play);
btnPlay.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (gmodel != null) {
if (!gmodel.getTranslated().equals("")) {
if (tts.isSpeaking()) {
tts.stop();
}
tts.setLanguage(getLocale(gmodel.getSrcLanguage()));
tts.speak(gmodel.getTranslated(), TextToSpeech.QUEUE_FLUSH, null);
}
}
}
});
しゃべっている最中に、繰り返し「play」ボタンを押された場合には、一旦止めてからしゃべりなおさせるようにしておきました。
それと、今後対応される言語が増えていったときのことを想定して、Localeを設定するところは、五ヶ国語以外も事前に準備をしておきました。
private String getLocale(LanguageModel model) {
String locale;
if (model.getValue().equals("en")) {
locale = Locale.ENGLISH.toString();
}
else if (model.getValue().equals("ja")) {
locale = Locale.JAPAN.toString();
}
else if (model.getValue().equals("zh")) {
locale = Locale.CHINA.toString();
}
else if (model.getValue().equals("it")) {
locale = Locale.ITALY.toString();
}
else if (model.getValue().equals("es")) {
locale = new Locale("spa", "ESP").toString();
}
else if (model.getValue().equals("fr")) {
locale = Locale.FRANCE.toString();
}
else if (model.getValue().equals("de")) {
locale = Locale.GERMAN.toString();
}
else if (model.getValue().equals("ko")) {
locale = Locale.KOREA.toString();
}
else if (model.getValue().equals("ru")) {
locale = new Locale("ru", "RUS").toString();
}
else {
locale = new Locale("OTHER").toString();
}
return locale;
}
実行結果は、ここでは表現できなかったので、サンプルソースを試していただければと思います!
サンプルソース
- TranslatorSample8:ダウンロード





