ANDROID语音识别示例
我们现在通过一个小的示例程序展示如何在应用中使用语音搜索。
我们的应用需要完成以下任务:
首先,我们需要创建一个类,为语音识别实现逻辑。 调用类?SpeechRecognitionHelper,此时我们需要声明一个静态的公共函数?run(),该函数将收到一个启动识别的请求:
01/**02?* A helper class for speech recognition03?*/04public class SpeechRecognitionHelper {05?06/**07?????* Running the recognition process. Checks availability of recognition Activity,08?????* If Activity is absent, send user to Google Play to install Google Voice Search.09????* If Activity is available, send Intent for running.10?????*11?????* @param callingActivity = Activity, that initializing recognition process12?????*/13????public static void run(Activity callingActivity) {14????????// check if there is recognition Activity15????????if (isSpeechRecognitionActivityPresented(callingActivity) == true) {16????????????// if yes – running recognition17????????????startRecognition(callingActivity);18????????} else {19????????????// if no, then showing notification to install Voice Search20????????????Toast.makeText(callingActivity, "In order to activate speech recognition you must install "Google Voice Search"", Toast.LENGTH_LONG).show();21????????????// start installing process22????????????installGoogleVoiceSearch(callingActivity);23????????}24????}25}如您所见,除 run() 函数之外,我们还需要执行三个函数:
为了检查设备是否有语音识别应用,我们可以使用类?PackageManager?中的?queryIntentActivities?方法。 该方法列出了可以处理指定意图的各种活动。 为了接收 PackageManager 的一个实例,我们可以使用?getPackageManager。
我们的代码如下所示:
isSpeechRecognitionActivityPresented
01/**02?????* Checks availability of speech recognizing Activity03?????*04?????* @param callerActivity – Activity that called the checking05?????* @return true – if Activity there available, false – if Activity is absent06?????*/07????private static boolean isSpeechRecognitionActivityPresented(Activity callerActivity) {08????????try {09????????????// getting an instance of package manager10????????????PackageManager pm = callerActivity.getPackageManager();11????????????// a list of activities, which can process speech recognition Intent12????????????List activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);13?14????????????if (activities.size() != 0) {??? // if list not empty15????????????????return true;??????????????? // then we can recognize the speech16????????????}17????????} catch (Exception e) {18?19????????}20?21????????return false; // we have no activities to recognize the speech22????}现在执行?startRecognition?函数。 该函数为启动语音识别活动提供适合的意图。 如欲了解有关该过程的详细信息,请查看?文档页。
源代码:
01???/**02?????* Send an Intent with request on speech 03?????* @param callerActivity? - Activity, that initiated a request04?????*/05????private static void startRecognitionActivity(Activity callerActivity) {06?07????????// creating an Intent with “RecognizerIntent.ACTION_RECOGNIZE_SPEECH” action08????????Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);09?10????????// giving additional parameters:11????????intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Select an application");??? // user hint12????????intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);??? // setting recognition model, optimized for short phrases – search queries13????????intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);??? // quantity of results we want to receive14//choosing only 1st -? the most relevant 15?16????????// start Activity ant waiting the result17????????ownerActivity.startActivityForResult(intent, SystemData.VOICE_RECOGNITION_REQUEST_CODE);18????}最后,我们需要执行?installGoogleVoiceSearch。 该函数将会显示出一个会话,询问用户是否需要安装 Google 语音搜索,如果用户同意,则将其转至 Google Play。
01/**02?????* Asking the permission for installing Google Voice Search. 03?????* If permission granted – sent user to Google Play04?????* @param callerActivity – Activity, that initialized installing05?????*/06????private static void installGoogleVoiceSearch(final Activity ownerActivity) {07?08????????// creating a dialog asking user if he want09????????// to install the Voice Search10????????Dialog dialog = new AlertDialog.Builder(ownerActivity)11????????????.setMessage("For recognition it’s necessary to install "Google Voice Search"")??? // dialog message12????????????.setTitle("Install Voice Search from Google Play?")??? // dialog header13????????????.setPositiveButton("Install", new DialogInterface.OnClickListener() {??? // confirm button14?15????????????????// Install Button click handler16????????????????@Override17????????????????public void onClick(DialogInterface dialog, int which) {18????????????????????try {19????????????????????????// creating an Intent for opening applications page in Google Play20????????????????????????// Voice Search package name: com.google.android.voicesearch21????????????????????????Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.google.android.voicesearch"));22????????????????????????// setting flags to avoid going in application history (Activity call stack)23????????????????????????intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);24????????????????????????// sending an Intent25????????????????????????ownerActivity.startActivity(intent);26?????????????????????} catch (Exception ex) {27?????????????????????????// if something going wrong28?????????????????????????// doing nothing29?????????????????????}30????????????????}})31?32????????????.setNegativeButton("Cancel", null)??? // cancel button33????????????.create();34?35????????dialog.show();??? // showing dialog36????}大致就是这样。 我们运行语音识别活动。 然后请求用户的许可,安装语音搜索并在取得用户同意之后将其转至 Google Play。 我们还需要做的一件事就是收集语音识别的结果。
我们使用?startActivityForResult?函数发送一个请求,收集已启动活动的结果。 我们还需要在我们的意图调用程序活动中重新定义一个OnActivityResult?方法。 按照以下方式进行定义:
01// Activity Results handler02????@Override03????public void onActivityResult(int requestCode, int resultCode, Intent data) {04?05????????// if it’s speech recognition results06????????// and process finished ok07????????if (requestCode == SystemData.VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {08?09????????????// receiving a result in string array10????????????// there can be some strings because sometimes speech recognizing inaccurate11????????????// more relevant results in the beginning of the list12????????????ArrayList matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);13?14????????????// in “matches” array we holding a results... let’s show the most relevant15????????????if (matches.size() > 0) Toast.makeText(this, matches.get(0), Toast.LENGTH_LONG).show();16????????}17?18????????super.onActivityResult(requestCode, resultCode, data);19????}借助已创建的类?SpeechRecognitionHelper,我们只需调用一个函数?run()?就能执行一个语音识别请求。
此时只需在我们的项目中添加该类并在需要的位置中调用运行函数就能添加一个识别功能。 然后为发起识别请求的活动重新定义 onActivityResult 方法并执行处理文本结果。
如欲了解更多信息,请查看?Android 开发人员?网站。 这里的一些正确?示例?展示了如何实现语音识别,更重要的是,如何获取可用的语言列表。 如果您想要识别除用户默认位置之外的一种语言,您就需要使用该列表。
如果您想快速地将语音输入集成到您的应用中,您可以下载该代码并将其用于?SpeechRecognitionHelper?类