【问题标题】:How can I use speech recognition without the annoying dialog in android phones如何在没有 Android 手机中烦人的对话框的情况下使用语音识别
【发布时间】:2011-09-13 02:26:27
【问题描述】:

在不修改 android API 的情况下这可能吗? 我找到了一篇关于这个的文章。 有一条评论说我应该对 android API 进行修改。 但它没有说明如何进行修改。 谁能给我一些关于如何做到这一点的建议? 谢谢!


我找到了这篇文章; SpeechRecognizer 他的需求和我的差不多。 对我来说是一个很好的参考!


我已经完全解决了这个问题。
我用谷歌搜索了一个可用的示例代码from this China website 这是我的源代码

package voice.recognition.test;

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



public class voiceRecognitionTest extends Activity implements OnClickListener 
{

   private TextView mText;
   private SpeechRecognizer sr;
   private static final String TAG = "MyStt3Activity";
   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Button speakButton = (Button) findViewById(R.id.btn_speak);     
            mText = (TextView) findViewById(R.id.textView1);     
            speakButton.setOnClickListener(this);
            sr = SpeechRecognizer.createSpeechRecognizer(this);       
            sr.setRecognitionListener(new listener());        
   }

   class listener implements RecognitionListener          
   {
            public void onReadyForSpeech(Bundle params)
            {
                     Log.d(TAG, "onReadyForSpeech");
            }
            public void onBeginningOfSpeech()
            {
                     Log.d(TAG, "onBeginningOfSpeech");
            }
            public void onRmsChanged(float rmsdB)
            {
                     Log.d(TAG, "onRmsChanged");
            }
            public void onBufferReceived(byte[] buffer)
            {
                     Log.d(TAG, "onBufferReceived");
            }
            public void onEndOfSpeech()
            {
                     Log.d(TAG, "onEndofSpeech");
            }
            public void onError(int error)
            {
                     Log.d(TAG,  "error " +  error);
                     mText.setText("error " + error);
            }
            public void onResults(Bundle results)                   
            {
                     String str = new String();
                     Log.d(TAG, "onResults " + results);
                     ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                     for (int i = 0; i < data.size(); i++)
                     {
                               Log.d(TAG, "result " + data.get(i));
                               str += data.get(i);
                     }
                     mText.setText("results: "+String.valueOf(data.size()));        
            }
            public void onPartialResults(Bundle partialResults)
            {
                     Log.d(TAG, "onPartialResults");
            }
            public void onEvent(int eventType, Bundle params)
            {
                     Log.d(TAG, "onEvent " + eventType);
            }
   }
   public void onClick(View v) {
            if (v.getId() == R.id.btn_speak) 
            {
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");

                intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
                     sr.startListening(intent);
                     Log.i("111111","11111111");
            }
   }
}

调试后一定要删除烦人的日志!

【问题讨论】:

  • 这绝对是可能的,因为我已经看到其他应用程序这样做了(Voice infinity),但至于如何,我不知道。我想你可以从下载 android 源代码并检查语音所在的 api 开始,然后尝试扩展......
  • 如 Femi 所述,请确保在您的 AndroidManifest.xml 文件中包含 &lt;uses-permission android:name="android.permission.RECORD_AUDIO" /&gt;,否则 SpeechRecognizer 将无法拾取任何音频

标签: android speech-recognition speech


【解决方案1】:

使用SpeechRecognizer 接口。你的应用需要有 RECORD_AUDIO 权限,然后你可以创建一个 SpeechRecognizer,给它一个RecognitionListener,然后调用它的startListening 方法。当语音识别器准备好开始收听语音以及接收语音并将其转换为文本时,您将收到对听众的回调。

【讨论】:

  • 也不要忘记在 OnDestroy() 方法中销毁 SpeechRecognier,如下所述:stackoverflow.com/a/19931355/2048266 不会得到 has leaked ServiceConnection android.speech.SpeechRecognizer$Connection@414f0e40 that was originally bound here 错误
  • 你能给我举个例子吗?另外,我可以在屏幕关闭时使用它吗?
【解决方案2】:

GAST 有一个方便的抽象类,你可以用很少的新代码来使用SpeechRecognizer 类。还有一个使用thisthisSpeechRecognizer 作为后台服务运行的示例

【讨论】:

  • 您介意指导我如何将它们实现到 MainActivity 中吗?这是什么意思“*使用{@link Intent}来启动和停止它?”非常感谢
  • 你能给我举个例子吗?另外,我可以在屏幕关闭时使用它吗?
【解决方案3】:

感谢您发布此信息!我发现在 oncreate 中定义 onclick 监听器很有帮助:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mText = (TextView) findViewById(R.id.textView1);     
    MyRecognitionListener listener = new MyRecognitionListener();
    sr = SpeechRecognizer.createSpeechRecognizer(this);       
    sr.setRecognitionListener(listener);

    findViewById(R.id.button1).setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) 
        {
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);    
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
                intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1); 
                intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
                sr.startListening(intent);
        }
    });     
}

【讨论】:

    【解决方案4】:

    我最终制作了 Github 项目,将文本转换为语音,将语音转换为文本,而无需烦人的对话,

    https://github.com/hiteshsahu/Android-TTS-STT/tree/master/app/src/main/java/com/hiteshsahu/stt_tts/translation_engine

     //SPEECH TO TEXT DEMO
        speechToText.setOnClickListener({ view ->
    
            Snackbar.make(view, "Speak now, App is listening", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show()
    
            TranslatorFactory
                    .instance
                    .with(TranslatorFactory.TRANSLATORS.SPEECH_TO_TEXT,
                            object : ConversionCallback {
                                override fun onSuccess(result: String) {
                                    sttOutput.text = result
                                }
    
                                override fun onCompletion() {
                                }
    
                                override fun onErrorOccurred(errorMessage: String) {
                                    erroConsole.text = "Speech2Text Error: $errorMessage"
                                }
    
                            }).initialize("Speak Now !!", this@HomeActivity)
    
        })
    
    
        //TEXT TO SPEECH DEMO
        textToSpeech.setOnClickListener({ view ->
    
            val stringToSpeak :String = ttsInput.text.toString()
    
            if (null!=stringToSpeak &&  stringToSpeak.isNotEmpty()) {
    
                TranslatorFactory
                        .instance
                        .with(TranslatorFactory.TRANSLATORS.TEXT_TO_SPEECH,
                                object : ConversionCallback {
                                    override fun onSuccess(result: String) {
                                    }
    
                                    override fun onCompletion() {
                                    }
    
                                    override fun onErrorOccurred(errorMessage: String) {
                                        erroConsole.text = "Text2Speech Error: $errorMessage"
                                    }
    
                                })
                        .initialize(stringToSpeak, this)
    
            } else {
                ttsInput.setText("Invalid input")
                Snackbar.make(view, "Please enter some text to speak", Snackbar.LENGTH_LONG).show()
            }
    
        })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      • 1970-01-01
      • 2015-10-16
      • 1970-01-01
      相关资源
      最近更新 更多