Android语音


在ACTION_RECOGNIZE_SPEECH状态,可以取得被识别的语音的文字串。
在ACTION_WEB_SEARCH模式下,通过网络检索被识别的语音,并表示结果到画面上。

运行结果
运行后,按下按钮,语音识别画面启动,面向麦克风讲几句话,
被识别出来后,结果会表示出来。
但是,能够响应这个Intent的Activity没有被安装的时候,例如模拟器状态下,
会发生ActivityNotFoundException例外,应该用try-catch来捕捉。
※开发用手机ADP1上可以动作。


 
import java.util.ArrayList;
 
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
import com.adakoda.voicerecognitiontest.R.id;
 
public class VoiceRecognitionTestActivity extends Activity {
    // = 0的部分,请用适当的值替换
    private static final int REQUEST_CODE = 0;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        Button button = (Button) findViewById(id.button);
        
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    // Intent作成
                    Intent intent = new Intent(
                            RecognizerIntent.ACTION_RECOGNIZE_SPEECH); // ACTION_WEB_SEARCH
                    intent.putExtra(
                            RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                    intent.putExtra(
                            RecognizerIntent.EXTRA_PROMPT,
                            "VoiceRecognitionTest"); // 可以替换成您喜欢的文字
                    
                    // Intent发行
                    startActivityForResult(intent, REQUEST_CODE);
                } catch (ActivityNotFoundException e) {
                    // 如果没有安装可以应答这个Intent的Activity的时候,显示一条消息
                    Toast.makeText(VoiceRecognitionTestActivity.this,
                        "ActivityNotFoundException", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
    
    // Activity终了的时候,会调用onActivityResult
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // 应答自己抛出的intent,
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            String resultsString = "";
            
            // 结果文字列数组
            ArrayList<String> results = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);
            
            for (int i = 0; i< results.size(); i++) {
                // 结合复数个文字列
                resultsString += results.get(i);
            }
            
            // 表示结果
            Toast.makeText(this, resultsString, Toast.LENGTH_LONG).show();
        }
        
        super.onActivityResult(requestCode, resultCode, data);
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    
<Button
    android:id="@+id/button"
    android:text="Click to start"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
    
</LinearLayout>

猜你喜欢

转载自maatlawson.iteye.com/blog/1716772