Android 轻松实现语音识别

网友投稿 1006 2022-12-01

Android 轻松实现语音识别

Android 轻松实现语音识别

苹果的iphone 有语音识别用的是Google 的技术,做为Google 力推的Android 自然会将其核心技术往Android 系统里面植入,并结合google 的云端技术将其发扬光大。

所以Google Voice Recognition在Android 的实现就变得极其轻松。

语音识别,借助于云端技术可以识别用户的语音输入,包括语音控制等技术,下面我们将利用Google 提供的Api 实现这一功能。

功能点为:通过用户语音将用户输入的语音识别出来,并打印在列表上。

功能界面如下:

用户说完话后,将提交到云端搜索:

在云端搜索完成后,返回打印数据

Android 轻松实现语音识别的完整代码

Java代码 ​​​​​  ​​​​

1. * Copyright (C) 20082. *3. * Licensed under the Apache License, Version 2.0 (the "License");4. * you may not use this5. * You may obtain a copy of the License at6. *7. * *9. * Unless required by applicable law or agreed to in writing, software10. * distributed under the License is distributed on an "AS IS"11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12. * See the License for13. * limitations under the License.14. */15. 16. package17. 18. import19. 20. import21. import22. import23. import24. import25. import26. import27. import28. import29. import30. import31. 32. import33. import34. 35. /**36. * Sample code that invokes the speech recognition intent API.37. */38. public class VoiceRecognition extends Activity implements39. 40. private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;41. 42. private43. 44. /**45. * Called with the activity is first created.46. */47. @Override48. public void49. super.onCreate(savedInstanceState);50. 51. // Inflate our UI from its XML layout description.52. setContentView(R.layout.voice_recognition);53. 54. // Get display items for later interaction55. Button speakButton = (Button) findViewById(R.id.btn_speak);56. 57. mList = (ListView) findViewById(R.id.list);58. 59. // Check to see if a recognition activity is present60. PackageManager pm = getPackageManager();61. List activities = pm.queryIntentActivities(62. new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);63. if (activities.size() != 0) {64. speakButton.setOnClickListener(this);65. } else66. speakButton.setEnabled(false);67. speakButton.setText("Recognizer not present");68. }69. }70. 71. /**72. * Handle the click on the start recognition button.73. */74. public void75. if76. startVoiceRecognitionActivity();77. }78. }79. 80. /**81. * Fire an intent to start the speech recognition activity.82. */83. private void84. Intent intent = new85. intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,86. RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);87. intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");88. startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);89. }90. 91. /**92. * Handle the results from the recognition activity.93. */94. @Override95. protected void onActivityResult(int requestCode, int96. if97. // Fill the list view with the strings the recognizer thought it could have heard98. ArrayList matches = data.getStringArrayListExtra(99. RecognizerIntent.EXTRA_RESULTS);100. mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,101. matches));102. }103. 104. super.onActivityResult(requestCode, resultCode, data);105. }106. }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Springboot错误页面和错误信息定制操作
下一篇:android 中给图片加圆角效果
相关文章

 发表评论

暂时没有评论,来抢沙发吧~