0

我正在尝试编写一个应用程序,允许我说出命令并让应用程序执行我所说的。但问题是,我不想使用弹出 GoogleVoice 的 RecognizerIntent。我想拥有一个自己定制的。谁能给我一些帮助或提示,让我这样做?在我说了些什么并执行任务之后,也许对如何使用结果有一些帮助?

更新代码:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class VoicingMain extends Activity implements OnClickListener {

ListView lv;    
private SpeechRecognizer sr;
private Intent srIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.voicing);
    lv = (ListView) findViewById(R.id.lvList);
    Button b = (Button) findViewById(R.id.bStartVoicing);
    b.setOnClickListener(this);
    boolean available = SpeechRecognizer.isRecognitionAvailable(this);
    Log.d("Speech", "available = " + available);
    sr = SpeechRecognizer.createSpeechRecognizer(this);
    sr.setRecognitionListener(new SpeechListener());

}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    srIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    srIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    srIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
            this.getPackageName());
    srIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
    Log.d("speech", "button active");
    sr.startListening(srIntent);
    new CountDownTimer(3000, 1000) {

        @Override
        public void onTick(long arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            sr.stopListening();
        }

    }.start();
}

private class SpeechListener implements RecognitionListener {

    @Override
    public void onBeginningOfSpeech() {
        // TODO Auto-generated method stub
        Log.d("Speech", "onBeginningOfSpeech");
    }

    @Override
    public void onBufferReceived(byte[] arg0) {
        // TODO Auto-generated method stub
        Log.d("Speech", "onBufferReceived");
    }

    @Override
    public void onEndOfSpeech() {
        // TODO Auto-generated method stub
        Log.d("Speech", "onEndOfSpeech");
    }

    @Override
    public void onError(int arg0) {
        // TODO Auto-generated method stub
        Log.d("Speech", "onError");
    }

    @Override
    public void onEvent(int arg0, Bundle arg1) {
        // TODO Auto-generated method stub
        Log.d("Speech", "onEvent");
    }

    @Override
    public void onPartialResults(Bundle arg0) {
        // TODO Auto-generated method stub
        Log.d("Speech", "onPartialResults");
    }

    @Override
    public void onReadyForSpeech(Bundle arg0) {
        // TODO Auto-generated method stub
        Log.d("Speech", "onReadyForSpeech");
    }

    @Override
    public void onResults(Bundle arg0) {
        // TODO Auto-generated method stub
        Log.d("Speech", "results");
    }

    @Override
    public void onRmsChanged(float arg0) {
        // TODO Auto-generated method stub
        // Log.d("Speech", "onRmsChanged");
    }

}

}

这就是我到目前为止所拥有的

4

2 回答 2

1

这个SO 问题显示了如何访问您的SpeechListener. 这些结果在Results()方法中可用。

至于执行任务的能力,您应该有某种处理程序,它将根据文本启动必要的服务。该处理程序可以使用意图与服务进行通信。例如,您可以对用户说“Call Bob”做出反应(基于上面的链接):

        @Override
        public void onResults(Bundle results) {
            ArrayList<String> voiceResults = results
                    .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            if (voiceResults == null) {
                Log.e(TAG, "No voice results");
            } else {
                for (int i = 0; i < voiceResults.size(); ++i) {
                    if( voiceResults[i].contains("Call") )
                      for( int j = i+1; j < matches.size(); ++j )
                        if( voiceResults[j].contains("Bob") )
                        {
                          //Get Bob's phone number, using [this][2]
                          String bobNumber = "123 123 1234";
                          Intent callIntent = new Intent(Intent.ACTION_CALL);          
                          callIntent.setData(Uri.parse("tel:"+bobNumber));          
                          startActivity(callIntent);
                          break;
                        }
                }
            }
        }
于 2013-07-19T04:16:13.303 回答
0

您可以简单地将 SetOnClickListener 放在任何按钮上。内部活动或片段比开始 onACtivityResult 与结果代码与其他值进行比较。

看看代码。

OnClickListener

                R.id.searchVoice -> {
                val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
                intent.putExtra(
                    RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
                )
                intent.putExtra(
                    RecognizerIntent.EXTRA_LANGUAGE,
                    Locale.getDefault());
                intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak to text")

                try {
                    startActivityForResult(intent, REQUEST_CODE_SPEECH_INPUT)
                } catch (e: Exception) {
                    Toast.makeText(
                        mContext, " " + e.message,
                        Toast.LENGTH_SHORT
                    )
                        .show()
                }
            }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)


        if (requestCode == REQUEST_CODE_SPEECH_INPUT) {
            if (resultCode == RESULT_OK && data != null) {
                val result = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS
                )
                if (result != null) {
                    if(result.contains("search products")){
                        val intent = Intent(this, ActivitySpecificProduct::class.java)
                        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
                        startActivity(intent)
                    }
                    else  if(result.contains("update products")){
                        val intent = Intent(this, ActivitySpecificProduct::class.java)
                        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
                        startActivity(intent)
                    }
                    else  if(result.contains("add products")){
                        val intent = Intent(this, ActivityStoreQR::class.java)
                        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
                        startActivity(intent)
                    }
                    else Toast.makeText(this, "Try something else", Toast.LENGTH_LONG).show()
                }
            }
        }
    }
于 2021-10-28T13:18:50.830 回答