android語音辨識

來源:互聯網
上載者:User

代碼如下:

package com.example.voice;
import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.speech.RecognizerIntent;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class voice extends Activity {  
      
    private Button btnReconizer;  
    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.reconizer);  
          
        btnReconizer=(Button) this.findViewById(R.id.btnRecognizer);  
        btnReconizer.setOnClickListener(new OnClickListener() {  
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                 // TODO Auto-generated method stub  
                try{  
                //通過Intent傳遞語音辨識的模式,開啟語音  
                Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);  
                //語言模式和自由模式的語音辨識  
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);  
                //提示音開始  
                intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "開始語音");  
                //開始語音辨識  
                startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);  
                }catch (Exception e) {  
                    // TODO: handle exception  
                    e.printStackTrace();  
                    Toast.makeText(getApplicationContext(), "找不到語音裝置", 1).show();  
                    }  
            }
            });
          

    }
      
    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        // TODO Auto-generated method stub  
        //回調擷取從Google得到的資料   
        if(requestCode==VOICE_RECOGNITION_REQUEST_CODE && resultCode==RESULT_OK){  
            //取得語音的字元  
            ArrayList<String> results=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);  
              
            String resultString="";  
            for(int i=0;i<results.size();i++){  
                resultString+=results.get(i);  
            }  
            Toast.makeText(this, resultString, 1).show();  
        }  
        super.onActivityResult(requestCode, resultCode, data);  
    }  

}

注意:

 在manifest檔案下加入<uses-permission android:name="android.permission.INTERNET" /> 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.