如何将Android SpeechRecognizer用作服务? [英] How do I use Android SpeechRecognizer as a service?

查看:81
本文介绍了如何将Android SpeechRecognizer用作服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Android语音识别作为服务运行.我可以验证是否已调用该服务的onCreate()和onStart()方法,但是没有调用语音识别方法的回调,尽管事实上我已经正确设置了SpeechRecognizer对象.语音识别似乎是在一项活动而不是一项服务中完成的.如何使它作为服务工作?这是明显的问题吗?

I am trying to run Android voice recognition as a service. I can verify that the onCreate() and onStart() methods of the service are called, but no callbacks to the speech recognition methods are called, despite the fact that I have set up the SpeechRecognizer object correctly. The speech recognition seems to work when it is done in an activity instead of a service. How do I make it work as a service? Is this a manifest issue?

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");
     }
}

推荐答案

您可以执行以下操作:

public class OpenMicService extends Service implements RecognitionListener{

    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
    private SpeechRecognizer speechRecognizer;

    @Nullable
    @Override

    public IBinder onBind(Intent intent) {


        return null;
    }

    @Override
    public int onStartCommand(Intent intent,int flags,int startId) {

        Toast.makeText(this,"start Service.",Toast.LENGTH_SHORT).show();

        speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
        speechRecognizer.setRecognitionListener(this);

        Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        voice.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass()
                .getPackage().getName());
        voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);

        speechRecognizer.startListening(voice);

        return START_REDELIVER_INTENT;

    }

    @Override
    public void onDestroy() {

        super.onDestroy();
    }


    @Override
    public void onReadyForSpeech(Bundle bundle) {

    }

    @Override
    public void onBeginningOfSpeech() {

    }

    @Override
    public void onRmsChanged(float v) {

    }

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

    }

    @Override
    public void onEndOfSpeech() {

    }

    @Override
    public void onError(int i) {

    }

    @Override
    public void onResults(Bundle results) {

        String wordStr = null;
        String[] words = null;
        String firstWord = null;
        String secondWord = null;

        ArrayList<String> matches = results
                .getStringArrayList(speechRecognizer.RESULTS_RECOGNITION);
        wordStr = matches.get(0);
        words = wordStr.split(" ");
        firstWord = words[0];
        secondWord = words[1];

        if (firstWord.equals("open")) {
            PackageManager packageManager = getPackageManager();
            List<PackageInfo> packs = packageManager
                    .getInstalledPackages(0);
            int size = packs.size();
            boolean uninstallApp = false;
            boolean exceptFlg = false;
            for (int v = 0; v < size; v++) {
                PackageInfo p = packs.get(v);
                String tmpAppName = p.applicationInfo.loadLabel(
                        packageManager).toString();
                String pname = p.packageName;
                //URL urlAddress = urlAddress.toLowerCase();
                tmpAppName = tmpAppName.toLowerCase();
                if (tmpAppName.trim().toLowerCase().equals(secondWord.trim().toLowerCase())) {
                    PackageManager pm = this.getPackageManager();
                    Intent appStartIntent = pm.getLaunchIntentForPackage(pname);
                    if (null != appStartIntent) {
                        try {
                            this.startActivity(appStartIntent);
                        } catch (Exception e) {
                        }
                    }
                }
            }
        } // end of open app code
    }

    @Override
    public void onPartialResults(Bundle bundle) {

    }

    @Override
    public void onEvent(int i,Bundle bundle) {

    }
}

这篇关于如何将Android SpeechRecognizer用作服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆