2011-06-11 17 views
113

में कष्टप्रद संवाद के बिना मैं भाषण मान्यता का उपयोग कैसे कर सकता हूं क्या यह एंड्रॉइड एपीआई को संशोधित किए बिना संभव है? मुझे इसके बारे में एक लेख मिला है। एक टिप्पणी है कि मुझे एंड्रॉइड एपीआई में संशोधन करना चाहिए। लेकिन यह नहीं कहा कि संशोधन कैसे करें। क्या कोई मुझे ऐसा करने के बारे में कुछ सुझाव दे सकता है? धन्यवाद!एंड्रॉइड फोन


मुझे यह आलेख मिला है; SpeechRecognizer उनकी ज़रूरतें मेरे जैसी ही हैं। यह मेरे लिए एक अच्छा संदर्भ है!


मुझे पूरी तरह से यह समस्या हल हो गई है।
मैं एक प्रयोग करने योग्य नमूना कोड googled from this China website यहाँ मेरी स्रोत कोड है

package voice.recognition.test; 

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

यह निश्चित रूप से करना संभव है, जैसा कि मैंने देखा है कि अन्य ऐप्स इसे (वॉयस अनंतता) करते हैं, लेकिन इसके लिए, मेरे पास कोई सुराग नहीं है।मुझे लगता है कि आप एंड्रॉइड स्रोत डाउनलोड करके और एपीआई में जांच कर शुरू कर सकते हैं जहां आवाज़ है, और उसके बाद विस्तार का प्रयोग ... – Eric

+1

जैसा कि फेमी द्वारा उल्लेख किया गया है, सुनिश्चित करें कि '<अनुमति-अनुमति एंड्रॉइड: name = "एंड्रॉइड है .permission.RECORD_AUDIO "/>' आपके AndroidManifest.xml फ़ाइल में अन्यथा SpeechRecognizer आपकी सलाह के लिए कोई भी ऑडियो – nommer

उत्तर

63

SpeechRecognizer इंटरफ़ेस का उपयोग करें। आपके ऐप को RECORD_AUDIO अनुमति की आवश्यकता है, और फिर आप एक स्पीच रिकॉग्नाइज़र बना सकते हैं, इसे RecognitionListener दें और फिर startListening विधि को कॉल करें। श्रोताओं को कॉलबैक प्राप्त होगा जब भाषण पहचानकर्ता भाषण सुनने के लिए तैयार हो जाएगा और क्योंकि यह भाषण प्राप्त करता है और इसे पाठ में परिवर्तित करता है।

+0

नहीं उठाएगा। मैं अब कोशिश करूँगा – Jim31837

+9

ऑनडस्ट्राय() विधि में स्पीच रिकॉन्गियर को भी नष्ट करने के लिए मत भूलना जैसा कि यहां बताया गया है: http://stackoverflow.com/a/19931355/2048266 'प्राप्त करने के लिए' ServiceConnection android.speech.SpeechRecognizer $ कनेक्शन लीक नहीं है @ 414f0e40 जो मूल रूप से यहां बाध्य था 'त्रुटि – nommer

+0

क्या आप मुझे एक उदाहरण दिखा सकते हैं? साथ ही, क्या स्क्रीन का बंद होने पर मैं इसका उपयोग कर सकता हूं? –

6

GAST एक आसान सार श्रेणी है जिसका उपयोग आप SpeechRecognizer कक्षा का उपयोग बहुत कम नए कोड के साथ करने के लिए कर सकते हैं। और this

+0

क्या आप मुझे मार्गदर्शन करेंगे कि उन्हें मुख्य क्रियान्वयन में कैसे कार्यान्वित किया जाए? इसका क्या अर्थ है "* इसे शुरू करने और रोकने के लिए {@link intent} का उपयोग करें?" बहुत बहुत धन्यवाद – Dante

+0

क्या आप मुझे एक उदाहरण दिखा सकते हैं? साथ ही, क्या स्क्रीन का बंद होने पर मैं इसका उपयोग कर सकता हूं? –

4

का उपयोग करके पृष्ठभूमि सेवा के रूप में SpeechRecognizer को पृष्ठभूमि सेवा के रूप में चलाने का एक उदाहरण भी है!

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mText = (TextView) findViewById(R.id.textView1);  
    MyRecognitionListener listener = new MyRecognitionListener(); 
    sr = SpeechRecognizer.createSpeechRecognizer(this);  
    sr.setRecognitionListener(listener); 

    findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) 
     { 
       Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);  
       intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US"); 
       intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1); 
       intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test"); 
       sr.startListening(intent); 
     } 
    });  
} 
3

मैं this Github Repo में टीटीएस और एसटीटी से मेरे सारे सीखने की दुकान करने की कोशिश की ahve: मैं इसे उपयोगी OnCreate में onclick श्रोता परिभाषित करने के लिए मिल गया। यदि आपको एक लाइनर पसंद है तो आप मेरी परियोजना का उपयोग कर सकते हैं।

यह फैक्टरी पैटर्न का इस्तेमाल बिना कष्टप्रद संवाद

SpeechToText (एसटीटी) लाइन पर में पाठ भाषण कन्वर्ट करने के लिए।

TranslatorFactory.getInstance().getTranslator(TranslatorFactory.TRANSLATOR_TYPE.SPEECH_TO_TEXT, HomeActivity.this) 
            .initialize("Hello There", HomeActivity.this); 

आउटपुट: -

enter image description here

TextToSpeech (टीटीएस)

TranslatorFactory.getInstance().getTranslator(TranslatorFactory.TRANSLATOR_TYPE.TEXT_TO_SPEECH, HomeActivity.this) 
               .initialize((null != message && !message ? message : "Invalid Input"), HomeActivity.this); 

आउटपुट: -

enter image description here

संबंधित मुद्दे