生活随笔
收集整理的這篇文章主要介紹了
Android初学者之轻松实现语音识别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Android初學者之輕松實現語音識別
蘋果的iphone有語音識別用的是Google的技術,做為Google力推的Android 自然會將其核心技術往Android 系統里面植入,并結合google 的云端技術將其發揚光大。?
所以Google Voice Recognition在Android 的實現就變得極其輕松。?
語音識別,借助于云端技術可以識別用戶的語音輸入,包括語音控制等技術,下面我們將利用Google 提供的Api 實現這一功能。?
功能點為:通過用戶語音將用戶輸入的語音識別出來,并打印在列表上。?
| 001 | * Copyright (C) 2008 The Android Open Source Project |
| 005 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 007 | * you may not use this file except in compliance with the License. |
| 009 | * You may obtain a copy of the License at |
| 013 | * <a href="http://www.apache.org/licenses/LICENSE-2.0" rel="nofollow" target="_blank">http://www.apache.org/licenses/LICENSE-2.0</a> |
| 017 | * Unless required by applicable law or agreed to in writing, software |
| 019 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 021 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 023 | * See the License for the specific language governing permissions and |
| 025 | * limitations under the License. |
| 031 | package com.example.android.apis.app; |
| 035 | import com.example.android.apis.R; |
| 039 | import android.app.Activity; |
| 041 | import android.content.Intent; |
| 043 | import android.content.pm.PackageManager; |
| 045 | import android.content.pm.ResolveInfo; |
| 047 | import android.os.Bundle; |
| 049 | import android.speech.RecognizerIntent; |
| 051 | import android.view.View; |
| 053 | import android.view.View.OnClickListener; |
| 055 | import android.widget.ArrayAdapter; |
| 057 | import android.widget.Button; |
| 059 | import android.widget.ListView; |
| 063 | import java.util.ArrayList; |
| 065 | import java.util.List; |
| 071 | * Sample code that invokes the speech recognition intent API. |
| 075 | public class VoiceRecognition extends Activity implements OnClickListener { |
| 079 | private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; |
| 083 | private ListView mList; |
| 089 | * Called with the activity is first created. |
| 095 | public void onCreate(Bundle savedInstanceState) { |
| 097 | super.onCreate(savedInstanceState); |
| 101 | // Inflate our UI from its XML layout description. |
| 103 | setContentView(R.layout.voice_recognition); |
| 107 | // Get display items for later interaction |
| 109 | Button speakButton = (Button) findViewById(R.id.btn_speak); |
| 113 | mList = (ListView) findViewById(R.id.list); |
| 117 | // Check to see if a recognition activity is present |
| 119 | PackageManager pm = getPackageManager(); |
| 121 | List activities = pm.queryIntentActivities( |
| 123 | new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); |
| 125 | if (activities.size() != 0) { |
| 127 | speakButton.setOnClickListener(this); |
| 131 | speakButton.setEnabled(false); |
| 133 | speakButton.setText("Recognizer not present"); |
| 143 | * Handle the click on the start recognition button. |
| 147 | public void onClick(View v) { |
| 149 | if (v.getId() == R.id.btn_speak) { |
| 151 | startVoiceRecognitionActivity(); |
| 161 | * Fire an intent to start the speech recognition activity. |
| 165 | private void startVoiceRecognitionActivity() { |
| 167 | Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); |
| 169 | intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, |
| 171 | RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); |
| 173 | intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo"); |
| 175 | startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); |
| 183 | * Handle the results from the recognition activity. |
| 189 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 191 | if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) { |
| 193 | // Fill the list view with the strings the recognizer thought it could have heard |
| 195 | ArrayList matches = data.getStringArrayListExtra( |
| 197 | RecognizerIntent.EXTRA_RESULTS); |
| 199 | mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, |
| 207 | super.onActivityResult(requestCode, resultCode, data); |
評價:
分享 45 分鐘前
總結
以上是生活随笔為你收集整理的Android初学者之轻松实现语音识别的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。