2013-08-07 1 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

난 당신이 명확해야하고 해결 방법으로 당신을 제공 할 수 있습니다 생각 2 가지가 있습니다.

  1. 서비스를 매니페스트에 올바르게 선언 했습니까? 나는 이것이 이미 다루어 진 것이라고 생각한다.

  2. 음성 인식이 서비스의 "onCreate"를 시작하지 않을 수 있습니다. 비슷한 구현을했지만 작동하지 않았습니다. 다른 메서드에서 startListening (intent)을 배치하여 명시 적으로 호출 할 수 있습니다. 이것은 나를 위해 일했다.

도움이되는지 알려주세요.

관련 문제