歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 輕松實現語音識別

Android 輕松實現語音識別

日期:2017/3/1 10:55:55   编辑:Linux編程

蘋果的iphone 有語音識別用的是Google 的技術,做為Google 力推的Android 自然會將其核心技術往Android 系統裡面植入,並結合google 的雲端技術將其發揚光大。

所以Google Voice Recognition在Android 的實現就變得極其輕松。

語音識別,借助於雲端技術可以識別用戶的語音輸入,包括語音控制等技術,下面我們將利用Google 提供的Api 實現這一功能。

功能點為:通過用戶語音將用戶輸入的語音識別出來,並打印在列表上。

功能界面如下:

用戶通過點擊speak按鈕顯示界面:

用戶說完話後,將提交到雲端搜索:

在雲端搜索完成後,返回打印數據:

Android 輕松實現語音識別的完整代碼

Java代碼

  1. * Copyright (C) 2008 The Android Open Source Project
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package com.example.android.apis.app;
  16. import com.example.android.apis.R;
  17. import android.app.Activity;
  18. import android.content.Intent;
  19. import android.content.pm.PackageManager;
  20. import android.content.pm.ResolveInfo;
  21. import android.os.Bundle;
  22. import android.speech.RecognizerIntent;
  23. import android.view.View;
  24. import android.view.View.OnClickListener;
  25. import android.widget.ArrayAdapter;
  26. import android.widget.Button;
  27. import android.widget.ListView;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. /**
  31. * Sample code that invokes the speech recognition intent API.
  32. */
  33. public class VoiceRecognition extends Activity implements OnClickListener {
  34. private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
  35. private ListView mList;
  36. /**
  37. * Called with the activity is first created.
  38. */
  39. @Override
  40. public void onCreate(Bundle savedInstanceState) {
  41. super.onCreate(savedInstanceState);
  42. // Inflate our UI from its XML layout description.
  43. setContentView(R.layout.voice_recognition);
  44. // Get display items for later interaction
  45. Button speakButton = (Button) findViewById(R.id.btn_speak);
  46. mList = (ListView) findViewById(R.id.list);
  47. // Check to see if a recognition activity is present
  48. PackageManager pm = getPackageManager();
  49. List activities = pm.queryIntentActivities(
  50. new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
  51. if (activities.size() != 0) {
  52. speakButton.setOnClickListener(this);
  53. } else {
  54. speakButton.setEnabled(false);
  55. speakButton.setText("Recognizer not present");
  56. }
  57. }
  58. /**
  59. * Handle the click on the start recognition button.
  60. */
  61. public void onClick(View v) {
  62. if (v.getId() == R.id.btn_speak) {
  63. startVoiceRecognitionActivity();
  64. }
  65. }
  66. /**
  67. * Fire an intent to start the speech recognition activity.
  68. */
  69. private void startVoiceRecognitionActivity() {
  70. Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  71. intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
  72. RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
  73. intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
  74. startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
  75. }
  76. /**
  77. * Handle the results from the recognition activity.
  78. */
  79. @Override
  80. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  81. if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
  82. // Fill the list view with the strings the recognizer thought it could have heard
  83. ArrayList matches = data.getStringArrayListExtra(
  84. RecognizerIntent.EXTRA_RESULTS);
  85. mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,
  86. matches));
  87. }
  88. super.onActivityResult(requestCode, resultCode, data);
  89. }
  90. }
Copyright © Linux教程網 All Rights Reserved