如何使用AndroidSpeechRecognizer作为服务?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (197)

以下是我的代码:

package net.viralpatel.android.speechtotextdemo;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service implements RecognitionListener {
    private SpeechRecognizer speechRecognizer;
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
    @Override
    public void onCreate() {
        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
        Log.d("tag", "onCreate");
        speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
        speechRecognizer.setRecognitionListener(this);

        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);

        speechRecognizer.startListening(intent); 
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        Log.d("tag", "onDestroy");
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d("tag", "onStart");
    }

    @Override
     public void onBeginningOfSpeech() {
      Log.d("Speech", "onBeginningOfSpeech");
     }

     @Override
     public void onBufferReceived(byte[] buffer) {
      Log.d("Speech", "onBufferReceived");
     }

     @Override
     public void onEndOfSpeech() {
      Log.d("Speech", "onEndOfSpeech");
     }

     @Override
     public void onError(int error) {
      Log.d("Speech", "onError");
     }

     @Override
     public void onEvent(int eventType, Bundle params) {
      Log.d("Speech", "onEvent");
     }

     @Override
     public void onPartialResults(Bundle partialResults) {
      Log.d("Speech", "onPartialResults");
     }

     @Override
     public void onReadyForSpeech(Bundle params) {
      Log.d("Speech", "onReadyForSpeech");
     }

     @Override
     public void onResults(Bundle results) {
      Log.d("Speech", "onResults");
      ArrayList strlist = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
      for (int i = 0; i < strlist.size();i++ ) {
       Log.d("Speech", "result=" + strlist.get(i));
      }
      BufferedWriter out;
    try {
        out = new BufferedWriter(new FileWriter("mnt/sdcard/results.txt"));
//        out.write(processor.execute(strlist.get(0).toString()));
          out.write("hello world"); 
    } catch (IOException e) {
        Log.e("Speech",e.toString());
    }
     }

     @Override
     public void onRmsChanged(float rmsdB) {
      Log.d("Speech", "onRmsChanged");
     }
}
提问于
用户回答回答于

需要弄清楚以下两件事

  1. 是否正确地在列表中声明了服务?
  2. 语音识别可能无法启动服务的“onCreate”。

扫码关注云+社区

领取腾讯云代金券