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

时间:2012-07-20 13:45:36

标签: android speech-recognition

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

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);

        }
    }

2 个答案:

答案 0 :(得分:3)

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

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

matches是您发音的可能文本列表。如果你想关闭你的应用程序,当一些神奇的短语说你需要这样的东西:

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

答案 1 :(得分:3)

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

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

查看实现词干和语音匹配的代码here。代码的其他部分使用匹配进行语音识别。如果您想了解所有细节,还有book