繁体   English   中英

识别侦听器Android Studio,它不起作用

[英]Recognition listener android studio, it doesn't work

我从互联网上复制了此文件,其他人则说是可行的,但是当我尝试讲话时却无济于事。

我试图在onBeginingOfSpeech()方法之外编写有关意图的代码,并且它可以工作。

public class Schermata extends ActionBarActivity implements RecognitionListener{

    SpeechRecognizer speechRecognizer;
    TextView test;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_schermata);
        speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
        test = (TextView) findViewById(R.id.test);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_schermata, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onReadyForSpeech(Bundle params) {
        test.setText("INIZIO");

    }

    @Override
    public void onBeginningOfSpeech() {
        test.setText("INIZIO");
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,getClass().getPackage().getName());
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        speechRecognizer.startListening(intent);

    }

    @Override
    public void onRmsChanged(float rmsdB) {

    }

    @Override
    public void onBufferReceived(byte[] buffer) {

    }

    @Override
    public void onEndOfSpeech() {
        test.setText("FINE");
    }

    @Override
    public void onError(int error) {

    }

    @Override
    public void onResults(Bundle results) {
        ArrayList<String> res = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        String s = "";
        for (String result : res)
            s += result + "\n";
        test.setText(s);
    }

    @Override
    public void onPartialResults(Bundle partialResults) {

    }

    @Override
    public void onEvent(int eventType, Bundle params) {

    }
}

如果我理解正确,并且在inBeginningOfSpeech()之外定义了Intent时这对您有用,那么只需将其放在它的外面,将它放在那里是错误的,我将进行解释。

函数onBeginningOfSpeech()(也:onResults,onReadyForSpeech等)在识别器开始工作后即被调用,并且仅是识别器听到某人讲话的事件的回调,因此您需要准备好该意图并在调用此功能之前与识别器配对。

尝试创建一个“ initialize()”函数,该函数会在调用之前设置所有这些函数

speechRecognizer.startListening(intent);

换句话说,使用this,而不是您在'onBeginningOfSpeech()'上的内容

 public void initialize(){
    Intent intent = new   Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,getClass().getPackage().getName());
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    speechRecognizer.startListening(intent);
}

否则,您将以错误的方式使用onResults(),则应仅提取提供给您的ArrayList的第一个String,而其他字符串只是同一语音的最差结果。

<uses-permission android:name="android.permission.RECORD_AUDIO" />

请确保您在清单中具有RECORD_AUDIO权限,否则,语音识别将无法正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM