▼Androidメモ▼
音声認識

音声認識を行うプログラムを作成する。
エミュレータでは音声認識のインテントがないため動作しない。



ソースコード
RecognizeSpeechEx.java
package net.npaka.recognizespeechex;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

//音声認識
public class RecognizeSpeechEx extends Activity 
    implements View.OnClickListener {
    private static final int REQUEST_CODE=0;
    private EditText editText;//エディットテキスト
    private Button   button;  //ボタン
    
    //アプリの初期化
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //レイアウトの生成
        LinearLayout layout=new LinearLayout(this);
        layout.setBackgroundColor(Color.rgb(255,255,255));
        layout.setOrientation(LinearLayout.VERTICAL);
        setContentView(layout);       
        
        //エディットテキストの生成
        editText=new EditText(this);
        editText.setText("",
            EditText.BufferType.NORMAL);
        setLLParams(editText,240,50);
        layout.addView(editText);
        
        //ボタンの生成
        button=new Button(this);
        button.setText("Speech");
        button.setOnClickListener(this);
        setLLParams(button);  
        layout.addView(button);
    }

    //ボタンクリックイベントの処理
    public void onClick(View v) {
        try {
            //音声認識インテントの実行
            Intent intent=new Intent(
                RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(
                RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(
                RecognizerIntent.EXTRA_PROMPT,
                "RecognizeSpeechEx");
            startActivityForResult(intent,REQUEST_CODE);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(RecognizeSpeechEx.this,
                "ActivityNotFoundException",Toast.LENGTH_LONG).show();
        }        
    }    
    
    //アクティビティ終了時に呼ばれる
    protected void onActivityResult(int requestCode,
        int resultCode,Intent data) {
        if (requestCode==REQUEST_CODE && resultCode==RESULT_OK) {
            String str="";
            
            //結果文字列リスト
            ArrayList results=
                data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);
            for (int i=0;i<results.size();i++) {
                str+=results.get(i);
            }
            editText.setText(str);
        }
        super.onActivityResult(requestCode,resultCode,data);
    }
    
    //ライナーレイアウトのパラメータ指定
    private static void setLLParams(View view) {
        view.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    }  

    //ライナーレイアウトのパラメータ指定
    private static void setLLParams(View view,int w,int h) {
        view.setLayoutParams(new LinearLayout.LayoutParams(w,h));
    }
}



−戻る−