2013-08-07 32 views
5

Tôi đang cố gắng chạy nhận dạng giọng nói Android dưới dạng dịch vụ. Tôi có thể xác minh rằng các phương thức onCreate() và onStart() của dịch vụ được gọi, nhưng không có callback nào cho các phương thức nhận dạng giọng nói được gọi, mặc dù thực tế là tôi đã thiết lập đúng đối tượng SpeechRecognizer. Nhận dạng giọng nói dường như hoạt động khi nó được thực hiện trong một hoạt động thay vì một dịch vụ. Làm thế nào để tôi làm cho nó hoạt động như một dịch vụ? Đây có phải là vấn đề hiển thị không?Tôi làm cách nào để sử dụng Android SpeechRecognizer làm dịch vụ?

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

Bạn đã tìm kiếm trên StackOverflow trước khi đặt câu hỏi? :) - 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 - [Ví dụ: SpeechActivationService.java] (https://github.com/gast-lib/gast-lib/blob/master/library/src/root /gast/speech/activation/SpeechActivationService.java "Ví dụ mã nguồn") – Organ

Trả lời

0

Có 2 điều tôi nghĩ bạn cần làm rõ và có thể cung cấp cho bạn giải pháp.

  1. Đã khai báo dịch vụ trong tệp kê khai đúng cách? Tôi tin rằng đây là điều đã được giải quyết.

  2. Nhận dạng giọng nói có thể không bắt đầu "onCreate" của dịch vụ. Tôi đã thực hiện tương tự nhưng nó không hoạt động. Bạn có thể thử đặt startListening (intent) trong một số phương thức khác và gọi nó một cách rõ ràng. Điều này làm việc cho tôi.

Hãy cho tôi biết nếu điều đó có ích.