使用语音识别器和TTS管理器类

时间:2014-07-28 20:32:08

标签: android speech-recognition text-to-speech

我正在尝试使用这个中央管理器类来处理语音识别和输出语音数据。到目前为止,我失败了。这是该类的样子,但是当我尝试在其他类中实现它时,我的应用程序崩溃了。有人可以帮帮我吗?

干杯!

import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;

public class speechEngineR extends Activity {

SpeechRecognizer ears;
TextToSpeech tts; 
Intent i; 
Context mCon = null; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    ears = SpeechRecognizer.createSpeechRecognizer(mCon); 
    i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    i.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, mCon.getPackageName()); 

    tts = new TextToSpeech(mCon, new TextToSpeech.OnInitListener() {

        @Override
        public void onInit(int status) {
            // TODO Auto-generated method stub
            if(status != TextToSpeech.ERROR){
                tts.setLanguage(Locale.US); 
            }
        }
    });


    ears.setRecognitionListener(new RecognitionListener() {

        @Override
        public void onRmsChanged(float rmsdB) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onResults(Bundle results) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onReadyForSpeech(Bundle params) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPartialResults(Bundle partialResults) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onEvent(int eventType, Bundle params) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onError(int error) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onEndOfSpeech() {
            // TODO Auto-generated method stub

        }

        @Override
        public void onBufferReceived(byte[] buffer) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onBeginningOfSpeech() {
            // TODO Auto-generated method stub

        }
    });
}

public speechEngineR(Context c){
    mCon = c; 
}

public void outSpeech(String out){
    tts.speak(out, TextToSpeech.QUEUE_FLUSH, null); 
}


}

是的,我已经在Manifest.xml中添加了以下权限

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

1 个答案:

答案 0 :(得分:0)

您的问题和代码没有帮助。

在您的代码中,您似乎正在尝试使用语音到文本(录音机),并且您使用TTS(文本到语音)调用它,这与您尝试完成的操作完全相反,即语音到文本。

你能说清楚你想要做什么吗?