Android - 将语音传递给文本输出到另一个活动/进程

时间:2022-11-13 16:07:01

I am developing an android app that has two parts: 1. converts speech to text and 2. manipulates the text to query a database. I have both parts working separately - what is the best way to join the activities together?

我正在开发一个Android应用程序,它有两个部分:1。将语音转换为文本,2。操作文本以查询数据库。我有两个部分分开工作 - 一起加入活动的最佳方式是什么?

To be clear, I want to take the text output from the Speech to text and use that as an input for another Activity. Any help is greatly appreciated!

为了清楚起见,我想将Speech的文本输出转换为文本,并将其用作另一个Activity的输入。任何帮助是极大的赞赏!

2 个解决方案

#1


2  

First activity:

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("speechtext", yourstring);
startActivity(intent);
finish();

Second activity:

String speechtext = getIntent().getStringExtra("speechtext");

#2


0  

Kindly take a look at below codes!..

请看下面的代码!..

   **Using Intent:::**

            Intent intent = new Intent(
                    RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

            try {
                startActivityForResult(intent, RESULT_SPEECH);
                txtText.setText("");
            } catch (ActivityNotFoundException a) {
                Toast t = Toast.makeText(getApplicationContext(),
                        "Opps! Your device doesn't support Speech to Text",
                        Toast.LENGTH_SHORT);
                t.show();
            }

Without Using Intent::

不使用Intent ::

Step 1: Implement RecognitionListener in your class.

第1步:在您的类中实现RecognitionListener。

Step 2. Add the Below codes:

步骤2.添加以下代码:

    private SpeechRecognizer speech = null;
    private Intent speechIntent=null;

    /**
 * Speech Result is used to Store the Voice Commands
 */
    private ArrayList<String> speechResult;


    inside onCreate()  --- > 

     speech = SpeechRecognizer.createSpeechRecognizer(this);
     speech.setRecognitionListener(this);


   Trigger this after your button Click: 

           if (SpeechRecognizer.isRecognitionAvailable(this)) {
                if(speechIntent==null ){
                    speechIntent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                    speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
                    speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
                    speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
                    speechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,12);
                    speech.startListening(speechIntent);
                }else{
                    if(speech!=null){
                        speech.startListening(speechIntent);
                    }
                }
            }

replace the onResults with below code:::

用以下代码替换onResults :::

public void onResults(Bundle results) {
    speechResult = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    if(speechResult!=null){
        if(speechResult.size()>0 ){
            String command=speechResult.get(0).toString();
                     }
             }

}

#1


2  

First activity:

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("speechtext", yourstring);
startActivity(intent);
finish();

Second activity:

String speechtext = getIntent().getStringExtra("speechtext");

#2


0  

Kindly take a look at below codes!..

请看下面的代码!..

   **Using Intent:::**

            Intent intent = new Intent(
                    RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");

            try {
                startActivityForResult(intent, RESULT_SPEECH);
                txtText.setText("");
            } catch (ActivityNotFoundException a) {
                Toast t = Toast.makeText(getApplicationContext(),
                        "Opps! Your device doesn't support Speech to Text",
                        Toast.LENGTH_SHORT);
                t.show();
            }

Without Using Intent::

不使用Intent ::

Step 1: Implement RecognitionListener in your class.

第1步:在您的类中实现RecognitionListener。

Step 2. Add the Below codes:

步骤2.添加以下代码:

    private SpeechRecognizer speech = null;
    private Intent speechIntent=null;

    /**
 * Speech Result is used to Store the Voice Commands
 */
    private ArrayList<String> speechResult;


    inside onCreate()  --- > 

     speech = SpeechRecognizer.createSpeechRecognizer(this);
     speech.setRecognitionListener(this);


   Trigger this after your button Click: 

           if (SpeechRecognizer.isRecognitionAvailable(this)) {
                if(speechIntent==null ){
                    speechIntent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                    speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
                    speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
                    speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
                    speechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,12);
                    speech.startListening(speechIntent);
                }else{
                    if(speech!=null){
                        speech.startListening(speechIntent);
                    }
                }
            }

replace the onResults with below code:::

用以下代码替换onResults :::

public void onResults(Bundle results) {
    speechResult = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    if(speechResult!=null){
        if(speechResult.size()>0 ){
            String command=speechResult.get(0).toString();
                     }
             }

}