音声認識
音声認識で注意しなくてはいけないのは、何語で認識させるかです。
つまり英語として認識させるか、日本語として認識させるかです。
同じキャットと発音しても「cat」と「キャット」というように結果が変わってきます。
英語の場合はインテントに
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,Locale.ENGLISH.toString() );
日本語の場合には、
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,Locale.JAPAN.toString() );
として設定します。
startActivityForResultで呼び出し、
onActivityResultでintentから認識した結果を配列として受け取ります。
List<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
音声合成(Text-To-Speech)
アクティビティにimplements TextToSpeech.OnInitListenerを実装します。
onCreateでttsのインスタンスを作成します。
音声合成は読み上げるスピードを設定して、
tts.setSpeechRate(SPEECH_RATE_NORMAL);
読み上げるだけです。
tts.speak(word, TextToSpeech.QUEUE_FLUSH, null);
注意しなくてはいけないのは、事前に音声がインストールされているかどうかです。
設定 > 音声入出力 > 音声合成設定 > 音声データをインストール
を選択し、アンドロイドマーケットからSpeechSynthesis Dataをインストールしておきましょう。
音声がでないとだいぶ悩んでしまいました :x001:
サンプル
SpeechActivity.java
package com.nekotype.speech;
import java.util.List;
import java.util.Locale;
import com.nekotype.speach.R;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class SpeechActivity extends Activity implements TextToSpeech.OnInitListener {
/** Called when the activity is first created. */
// TextToSpeechインスタンス
private TextToSpeech tts = null;
// 読み上げスピード用(標準)
private static final float SPEECH_RATE_NORMAL = 1.0f;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.button1);
btn.setTag("音声認識");
btn.setOnClickListener(new BtnListener());
Button btn2 = (Button)findViewById(R.id.btnread);
btn2.setTag("読む");
btn2.setOnClickListener(new BtnListener());
// ttsのインスタンス作成
tts = new TextToSpeech(this, this);
}
class BtnListener implements OnClickListener{
public void onClick(View arg0) {
// TODO 自動生成されたメソッド・スタブ
Button button = (Button) arg0;
if (button.getTag().equals("音声認識")) {
try{
// 音声認識用インテント生成
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//LANGUAGE_MODEL_FREE_FORM 入力音声そのまま
//LANGUAGE_MODEL_WEB_SEARCH Web検索をかけて結果を取得
//intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "popupの文字");
//入力言語設定
//Locale.ENGLISH.toString() 英語
//Locale.ENGLISH.toString() 日本語
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,Locale.ENGLISH.toString() );
//intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,Locale.JAPAN.toString() );
// インテント実行
startActivityForResult(intent, 0);
} catch (ActivityNotFoundException e) {
// 音声認識用インテントに対応するアクティビティが存在しない場合、
Toast.makeText(SpeechActivity.this,"音声認識できない!", Toast.LENGTH_LONG).show();
}
}else if(button.getTag().equals("読む")){
String word = "";
//Toast.makeText(SpeachActivity.this,"yomu!", Toast.LENGTH_LONG).show();
TextView tv = (TextView)findViewById(R.id.txtview1);
word = (String) tv.getText();
if (word.length() > 0) {
if (tts.isSpeaking()) {
tts.stop();
}
// 読み上げ開始
//読み上げスピード設定
tts.setSpeechRate(SPEECH_RATE_NORMAL);
//読み上げ
tts.speak(word, TextToSpeech.QUEUE_FLUSH, null);
}
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// リクエストコードと結果コードチェック
if (requestCode == 0 && resultCode == RESULT_OK) {
String resultsString = "";
// 音声認識結果の文字列取得
List<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
for (int i = 0; i < results.size(); i++) {
resultsString += results.get(i);
}
// 取得した文字列をTextViewに設定
TextView speakword = (TextView) findViewById(R.id.txtview1);
speakword.setText(resultsString);
}
super.onActivityResult(requestCode, resultCode, data);
}
public void onInit(int status) {
// TODO 自動生成されたメソッド・スタブ
// 音声合成初期化処理が成功した場合
if (TextToSpeech.SUCCESS == status) {
// ロケールをENGLISHに設定
Locale locale = Locale.ENGLISH;
if (tts.isLanguageAvailable(locale) >= TextToSpeech.LANG_AVAILABLE) {
tts.setLanguage(locale);
} else {
Log.e(getClass().getSimpleName(), "Locale Setting Error");
}
} else {
Log.e(getClass().getSimpleName(), "TextToSpeech Init Error");
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="音声入力" />
<TextView
android:id="@+id/txtview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btnread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="読む" />
</LinearLayout>
