理解并在android应用程序上实现eclipse的语音识别



我目前正在开发一个应用程序,如果需要,我需要让用户只使用语音来控制应用程序。我得到了一份显然来自安卓系统的语音识别代码副本,并尝试在我的应用程序中使用它。代码本身看起来相当简单,但我需要帮助理解一些部分并在应用程序上实现它。我在安卓设备上运行了这个应用程序,当我使用语音时,我会得到一个我可能说过的短语列表。我希望能够设置和活动,例如,如果其中一个结果是"关闭应用程序",它将关闭应用程序。我想我可以通过对语音识别器的结果进行if语句来设置它。例如,如果结果==关闭应用程序,则运行活动关闭。我没有任何这样的设置,因为我不知道如何准确地编码它,但我认为你们中的一些人会有一些想法。

package com.example.com.proto1;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;

    /**
     * Sample code that invokes the speech recognition intent API.
     */
    public class VoiceRecognition extends Activity implements OnClickListener {
        private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
        private ListView mList;
        /**
         * Called with the activity is first created.
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Inflate our UI from its XML layout description.
            setContentView(R.layout.voice_recognition);
            // Get display items for later interaction
            Button speakButton = (Button) findViewById(R.id.btn_speak);
            mList = (ListView) findViewById(R.id.list);
            // Check to see if a recognition activity is present
            PackageManager pm = getPackageManager();
            List<ResolveInfo> activities = pm.queryIntentActivities(
                    new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
            if (activities.size() != 0) {
                speakButton.setOnClickListener(this);
            } else {
                speakButton.setEnabled(false);
                speakButton.setText("Recognizer not present");
            }
        }
        /**
         * Handle the click on the start recognition button.
         */
        public void onClick(View v) {
            if (v.getId() == R.id.btn_speak) {
                startVoiceRecognitionActivity();
            }
        }
        /**
         * Fire an intent to start the speech recognition activity.
         */
        private void startVoiceRecognitionActivity() {
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
            startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
        }
        /**
         * Handle the results from the recognition activity.
         */
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
                // Fill the list view with the strings the recognizer thought it could have heard
                ArrayList<String> matches = data.getStringArrayListExtra(
                        RecognizerIntent.EXTRA_RESULTS);
                mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                        matches));
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

这是您从语音识别活动中收到的结果:

ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

matches是您已发音的可能文本的列表。如果你想在有人说你需要这样的神奇短语时关闭你的应用程序:

if (matches.contains("close")) {
     finish();
}

如果您想超越字符串比较,可以尝试词干或语音匹配。

你需要这样才能使你的识别更加准确,或者如果你有一些难以识别的单词。

看看这里的代码,它实现词干和语音匹配。代码的其他部分使用匹配进行语音识别。如果你想知道所有的细节,还有这本书。

最新更新