Android語音辨識功能使用

來源:互聯網
上載者:User
package com.king.android.speical;import java.util.ArrayList;import com.king.android.R;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.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;/** * 描述:語音辨識功能(只有在真機才能測試) * 作者:Andy.Liu * 時間: 2012-7-16  上午07:38:27 **/public class VoiceActivity extends Activity implements OnClickListener{private static final int VOICE_RECOGNITION_REQUEST_CODE = 4321;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Button btnVoice = (Button) findViewById(R.id.btn_voice);btnVoice.setVisibility(View.VISIBLE);btnVoice.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch(v.getId()){case R.id.btn_voice:doStartVoice();}}/** *  *TODO:開始語音 *Author:Andy.Liu *Create Time:2012-7-16 上午07:42:48 *TAG: *Return:void */private void doStartVoice(){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(ActivityNotFoundException e){//找到到語音裝置Toast.makeText(VoiceActivity.this, "沒有找到裝置", Toast.LENGTH_LONG).show();}}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if(VOICE_RECOGNITION_REQUEST_CODE==requestCode&&RESULT_OK ==resultCode){ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);StringBuilder sb = new StringBuilder();for(String voice:results){sb.append(voice);}Toast.makeText(VoiceActivity.this, sb.toString(), Toast.LENGTH_LONG).show();}super.onActivityResult(requestCode, resultCode, data);}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.