2011-06-16 26 views
15

Bir aktiviteyi servis olarak uygulamak mümkün mü? Etkinliğim bir ses tanıma etkinliği. Uygulamanın arka planında çalışan sesin sürekli olarak kontrol edilmesini ve kullanıcının bunu tanıyacağı bir komut yazıp eylemi gerçekleştirmesini istiyorum. Sorum şu ki ... bunu yapmak mümkün mü ve eğer öyleyse arka plan servisi mevcut aktiviteyi veya uygulamayı nasıl bilgilendirebilir? Net bir cevabı olmayan bir önceki yazı vardı ... Herhangi bir giriş veya yardım için teşekkürler.Ses Tanıma Bir arka plan hizmeti olarak

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.speech.RecognitionListener; 
import android.speech.RecognizerIntent; 
import android.speech.SpeechRecognizer; 
import android.widget.Button; 
import android.widget.TextView; 
import java.util.ArrayList; 
import android.util.Log; 



public class voiceRecognitionTest extends Activity implements OnClickListener 
{ 

    private TextView mText; 
    private SpeechRecognizer sr; 
    private static final String TAG = "MyStt3Activity"; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      Button speakButton = (Button) findViewById(R.id.btn_speak);  
      mText = (TextView) findViewById(R.id.textView1);  
      speakButton.setOnClickListener(this); 
      sr = SpeechRecognizer.createSpeechRecognizer(this);  
      sr.setRecognitionListener(new listener());   
    } 

    class listener implements RecognitionListener   
    { 
      public void onReadyForSpeech(Bundle params) 
      { 
        Log.d(TAG, "onReadyForSpeech"); 
      } 
      public void onBeginningOfSpeech() 
      { 
        Log.d(TAG, "onBeginningOfSpeech"); 
      } 
      public void onRmsChanged(float rmsdB) 
      { 
        Log.d(TAG, "onRmsChanged"); 
      } 
      public void onBufferReceived(byte[] buffer) 
      { 
        Log.d(TAG, "onBufferReceived"); 
      } 
      public void onEndOfSpeech() 
      { 
        Log.d(TAG, "onEndofSpeech"); 
      } 
      public void onError(int error) 
      { 
        Log.d(TAG, "error " + error); 
        mText.setText("error " + error); 
      } 
      public void onResults(Bundle results)     
      { 
        String str = new String(); 
        Log.d(TAG, "onResults " + results); 
        ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); 
        for (int i = 0; i < data.size(); i++) 
        { 
           Log.d(TAG, "result " + data.get(i)); 
           str += data.get(i); 
        } 
        mText.setText("results: "+String.valueOf(data.size()));   
      } 
      public void onPartialResults(Bundle partialResults) 
      { 
        Log.d(TAG, "onPartialResults"); 
      } 
      public void onEvent(int eventType, Bundle params) 
      { 
        Log.d(TAG, "onEvent " + eventType); 
      } 
    } 
    public void onClick(View v) { 
      if (v.getId() == R.id.btn_speak) 
      { 
       Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);   
       intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
       intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test"); 

       intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
        sr.startListening(intent); 
        Log.i("111111","11111111"); 
      } 
    } 
} 
+1

Merhaba, yapacak arıyorum ... aktivite sürekli konuşma girişi için dinleme olacak ve daha da önemlisi BT ODAKLANILAN ve arka planda çalışmadığından OLMAK GEREKİR olmasıdır Buna benzer bir şey, bu soruyu sorduğunuzdan beri bir süre olduğunu biliyorum, bunu yapmanın bir yolunu buldunuz mu merak ediyordum? – Peter

+0

Şunlara dikkat edin: http://stackoverflow.com/questions/11406925/speech-recognition-as-a-service-android – SepehrM

cevap

0

Ne yapabilirsiniz komutlar için dinleyecek tanıyıcı etkinlik başlatmasına ve bu uygun bir komut bulduğunda bunu bunu istediğini yapar: Burada ses faaliyet ... başka bir StackOverflow görevinden alınmış o belirli komutta yap. Bu basit yaklaşım ile sorun

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.html

+1

Bu yüzden bir tür ses komut sistemini uygulamak gerçekten yok mu? –

+24

Bu cevap, kişinin bir hizmette değil, bir hizmette ses tanıyıcıya ihtiyaç duyması nedeniyle soruyu nasıl yanıtlayabilir? – ozmank