2013-08-07 50 views
5

我試圖將Android語音識別作爲服務運行。我可以驗證服務的onCreate()和onStart()方法被調用,但不調用語音識別方法的回調,儘管我已經正確設置了SpeechRecognizer對象。語音識別似乎是在一項活動而不是一項服務中完成的。我如何使它作爲服務工作?這是一個明顯的問題嗎?如何將Android SpeechRecognizer用作服務?

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

在問您的問題之前,您是否在StackOverflow上搜索? :) - http://stackoverflow.com/questions/9997720/how-to-register-a-custom-speech-recognition-service - http://stackoverflow.com/questions/14940657/android-speech-recognition-as -a-service-on-android-4-1-4-2 - [示例:SpeechActivationService.java](https://github.com/gast-lib/gast-lib/blob/master/library/src/root /gast/speech/activation/SpeechActivationService.java「示例源代碼」) – Organ

回答

0

有兩件事我認爲您需要澄清並可能爲您提供解決方法。

  1. 已經在清單中正確聲明瞭服務? 我相信這已經解決了。

  2. 語音識別可能無法啓動服務的「onCreate」。我做了類似的實施,但沒有奏效。你可以嘗試將startListening(intent)放在其他方法中,並明確地調用它。這對我有效。

讓我知道它是否有幫助。