语音识别器使用指南

时间:2019-01-10 22:49:43

标签: android accessibility speech-recognition

我正在使用以下指南“ https://www.youtube.com/watch?v=AnNJPf-4T70&index=13&t=0s&list=LLWs2Xlax6q42i66xW1W7F3Q”来尝试开发演示应用程序,以便我可以熟悉在实际应用程序中使用语音识别器并学习如何开发可通过语音控制的应用程序允许行动不便的人将来使用我创建的所有应用程序。

我对使用意图来使用测试应用程序感兴趣,例如仅作为示例打开浏览器,但是我已经匹配了代码,却遇到了找不到解决方案的错误。

这是我的活动xml。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Blind Accessibility Demo"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        app:srcCompat="@drawable/ic_mic_black_24dp"
        android:layout_alignParentBottom="true"
        android:layout_margin="16dp" />


</RelativeLayout>

Below is my Main activity class

package com.example.ricardo.voicecontrol;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    //    this initalizes the text to speech variable
    private TextToSpeech myTTS;
    //this initalizes the speech reconizer variable
    private SpeechRecognizer speechtest;
private  FloatingActionButton fab;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
               intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                       RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
               intent.putExtra((RecognizerIntent.EXTRA_MAX_RESULTS),1);
               speechtest.startListening(intent);


            }
        });
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


//this initalizes text to speech
        initializeTextToSpeech();

//        this code initalizses or "starts" the speech recognizer
        initializeSpeechRecognizer();

    }


    private void initializeSpeechRecognizer() {
        if (SpeechRecognizer.isRecognitionAvailable((this))
                ) {
            speechtest = SpeechRecognizer.createSpeechRecognizer(this);
            speechtest.setRecognitionListener(new RecognitionListener() {
                @Override
                public void onReadyForSpeech(Bundle params) {

                }

                @Override
                public void onBeginningOfSpeech() {

                }

                @Override
                public void onRmsChanged(float rmsdB) {

                }

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

                }

                @Override
                public void onEndOfSpeech() {

                }

                @Override
                public void onError(int error) {

                }

                @Override
                public void onResults(Bundle bundle) {
                    List<String> results = bundle.getStringArrayList(
                            (
                                    SpeechRecognizer.RESULTS_RECOGNITION
                            ));
                    processResults(results.get(0));

                }

                @Override
                public void onPartialResults(Bundle partialResults) {

                }

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

                }
            });
        }
    }

    private void processResults(String command) {
        command = command.toLowerCase();
        if (command.indexOf("open") != -1) {
            if (command.indexOf("browser") != -1) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.yahoo.com/"));
                startActivity(intent);
            }
        }

}
//        what is your name
//    what is the time





    private void initializeTextToSpeech() {
        myTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (myTTS.getEngines().size() == 0) {
                    Toast.makeText(MainActivity.this, "There is no text to speech engine available on your phone"
                            , Toast.LENGTH_LONG).show();
                    finish();
                } else {
                    myTTS.setLanguage(Locale.US);
//                    this line of code below will be spoken once the app loads up
                    speak("This app can be controlled using your voice tap the microphone icon to begin");
                }
            }
        });
    }

    private void speak(String message) {
        if (Build.VERSION.SDK_INT >= 21) {
            myTTS.speak(message, TextToSpeech.QUEUE_FLUSH, null, null);
        } else {
            myTTS.speak(message, TextToSpeech.QUEUE_FLUSH, null);
        }

        }
    @Override
    protected void onPause () {
        super.onPause();
        myTTS.shutdown();
    }

    private class FloatingActionButton {
        public void setOnClickListener(View.OnClickListener onClickListener) {
        }
    }
}

我遇到的错误是“不可转换的类型;无法转换为'android.viewView到'com.example.ricardo.voicecontrol.MainActivity.FloatingActionButton” 我尝试重命名文件,使现金无效并重新启动,但没有发现变化。

1 个答案:

答案 0 :(得分:0)

看起来您的FloatingActionButton不是您创建内部类的View

private class FloatingActionButton {
    public void setOnClickListener(View.OnClickListener onClickListener) {
    }
}

那根本不应该存在。您创建了一个不扩展View的类,并尝试将其用作View。删除此类,并使用库中的FloatingActionButton。这是错误的根本原因。

尝试更改

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
           intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                   RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
           intent.putExtra((RecognizerIntent.EXTRA_MAX_RESULTS),1);
           speechtest.startListening(intent);


        }
    });
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

收件人

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
           intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                   RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
           intent.putExtra((RecognizerIntent.EXTRA_MAX_RESULTS),1);
           speechtest.startListening(intent);


        }
    });

我希望您会收到NullPointerException。

这行代码

 setContentView(R.layout.activity_main);

是说;加载我的xml文件activity_main作为该活动的当前布局。

这行代码

 FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

是说;在我用setContentView加载的xml文件中,找到一个ID fab的视图并将其转换为FloatingActionButton

因此,它们以这种方式布置了您的代码,您尝试在加载视图之前找到视图。