2012-08-02 6 views
18

मेरे एंड्रॉइड एप्लिकेशन में फीचर बोलने के लिए टेक्स्ट है और फिर एक गतिविधि इस सुविधा का उपयोग करती है। इसलिए इसे आसान बनाने के लिए एक स्थिर सहायक वर्ग बनाया।एंड्रॉइड "बोलने में असफल रहा: टीटीएस इंजन से बंधे नहीं"

import java.util.Locale; 

import android.content.Context; 
import android.speech.tts.TextToSpeech; 
import android.speech.tts.TextToSpeech.OnInitListener; 
import android.util.Log; 

public class TextToSpeechController implements OnInitListener { 

    private static final String TAG = "TextToSpeechController"; 
    private TextToSpeech myTTS; 
    private String textToSpeak; 
    private Context context; 

    private static TextToSpeechController singleton; 

    public static TextToSpeechController getInstance(Context ctx) { 
     if (singleton == null) 
      singleton = new TextToSpeechController(ctx); 
     return singleton; 
    } 

    private TextToSpeechController(Context ctx) { 
     context = ctx; 
    } 

    public void speak(String text) { 
     textToSpeak = text; 

     if (myTTS == null) { 
      // currently can't change Locale until speech ends 
      try { 
       // Initialize text-to-speech. This is an asynchronous operation. 
       // The OnInitListener (second argument) is called after 
       // initialization completes. 
       myTTS = new TextToSpeech(context, this); 

      } catch (Exception e) {    
       e.printStackTrace(); 
      } 
     } 

     sayText(); 

    } 

    public void onInit(int initStatus) { 
     if (initStatus == TextToSpeech.SUCCESS) { 
      if (myTTS.isLanguageAvailable(Locale.UK) == TextToSpeech.LANG_AVAILABLE) 
       myTTS.setLanguage(Locale.UK); 
     } 

     // status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR. 
     if (initStatus == TextToSpeech.SUCCESS) { 
      int result = myTTS.setLanguage(Locale.UK); 
      if (result == TextToSpeech.LANG_MISSING_DATA 
        || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
       Log.e(TAG, "TTS missing or not supported (" + result + ")"); 
       // Language data is missing or the language is not supported. 
       // showError(R.string.tts_lang_not_available); 

      } else { 
       // Initialization failed. 
       Log.e(TAG, "Error occured"); 
      } 

     } 
    } 

    private void sayText() { 
     // ask TTs to say the text 
     myTTS.speak(this.textToSpeak, TextToSpeech.QUEUE_FLUSH,  null); 
    } 

    public void stopTTS() { 
     if (myTTS != null) { 
      myTTS.shutdown(); 
      myTTS.stop(); 
      myTTS = null; 
     } 
    } 

} 

मैं इस तरह के सहायक वर्ग का उपयोग कर रहा हूं।

TextToSpeechController.getInstance(this).speak(readableMessage); 

लेकिन कभी-कभी मुझे लॉगकैट पर लाइन बेलोव जैसी त्रुटि दिखाई देती है।

"speak:failed not bound to tts engine" 

कोई अपवाद फेंक दिया गया लेकिन टीटीएस द्वारा कुछ भी नहीं पढ़ा गया। मुझे एहसास हुआ कि अगर मैंने ब्रॉडकास्ट रिसीवर से गतिविधि शुरू की, तो मुझे यह त्रुटि मिली। अन्यथा अगर आवेदन कोड से मैन्युअल रूप से खुली गतिविधि कोई समस्या नहीं है।

यहाँ BroadcastReceiver कोड

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);    
     String readableMessage = intent.getExtras().getString(READABLE_MESSAGE);  

     Bundle b = new Bundle(); 
     b.putString(EXTRA_MESSAGE, newMessage); 
     b.putString(READABLE_MESSAGE, readableMessage); 
     Intent newIntent = new Intent("android.intent.action.MAIN"); 
     newIntent.setClass(context, Speak.class); 
     newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     newIntent.putExtra("MessageReceived", b); 
     newIntent.putExtra("CallType", CallType.NOTIFICATION); 
     context.startActivity(newIntent);   
    } 
}; 

उत्तर

6

आपका कोड TextToSpeech वर्ग के निर्माता से asynch वापसी के लिए इंतजार नहीं है। आपके पास एक टिप्पणी भी है जो कहती है कि यह असीम है। यह हमेशा रिसीवर में क्यों विफल रहता है लेकिन गतिविधि नहीं, मुझे यकीन नहीं है - अन्य तो हो सकता है कि एक अग्रभूमि कार्य के रूप में गतिविधि की प्राथमिकता हो और यह कहने से पहले पूरा हो जाए।

आपको उस टेक्स्ट में इनिट कॉल रिटर्न तक कॉल करने के लिए प्रतीक्षा करने की आवश्यकता है, जहां उसे एक नई टीटीएस ऑब्जेक्ट को नया करना है।

+0

तो मैं यह कैसे कर सकता हूं? Thread.Sleep? मैं नहीं कह सकता कि बोलने तक प्रतीक्षा करें। –

+0

हाय। क्या हम इस टीटीएस धागे को समानांतर में शुरू कर सकते हैं? –

3

यहां कोड है जिसे मैं भाषण के लिए पाठ के लिए उपयोग करता हूं। मेरे कोड के लिए बस स्पीकर टाइप करें ("कुछ कहें"); किसी भी गतिविधि में।

public class VoiceRecognition extends Activity implements OnClickListener, 
     OnInitListener { 

    public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; 

    public Button speakButton; 
    // TTS object 
    public TextToSpeech myTTS; 
    // status check code 
    public int MY_DATA_CHECK_CODE = 0; 

    // setup TTS 
    public void onInit(int initStatus) { 

     // check for successful instantiation 
     if (initStatus == TextToSpeech.SUCCESS) { 
      if (myTTS.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE) 
       myTTS.setLanguage(Locale.US); 
     } else if (initStatus == TextToSpeech.ERROR) { 
      Toast.makeText(this, "Sorry! Text To Speech failed...", 
        Toast.LENGTH_LONG).show(); 
     } 
    } 

    /** 
    * Called with the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle voiceinput) { 
     super.onCreate(voiceinput); 

     // Inflate our UI from its XML layout description. 
     setContentView(R.layout.voice_recognition); 

     // check for TTS data 
     Intent checkTTSIntent = new Intent(); 
     checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); 
     startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE); 

    } 

    // speak the user text 
    public void speakWords(String speech) { 

     // speak straight away 
     myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null); 
    } 


    public void onClick(View v) { 
     speakWords("hello"; 
    } 


    /** 
    * Handle the results from the recognition activity. 
    */ 
    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 

     if (requestCode == MY_DATA_CHECK_CODE) { 
      if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) { 
       // the user has the necessary data - create the TTS 
       myTTS = new TextToSpeech(this, this); 
      } else { 
       // no data - install it now 
       Intent installTTSIntent = new Intent(); 
       installTTSIntent 
         .setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); 
       startActivity(installTTSIntent); 
      } 
     } 

     super.onActivityResult(requestCode, resultCode, data); 

    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     myTTS.shutdown(); 
    } 
+0

मुझे पता है कि आप शायद अपने कोड को ठीक करेंगे लेकिन मुझे लगता है कि यह उपयोग करने में आसान हो सकता है। साथ ही, ऑनक्रेट बंडल के ऊपर दिए गए कोड के ऑनइन अनुभाग का उपयोग करने का प्रयास करें। यह आपको भविष्य की त्रुटियों से बचाएगा। \ –

+1

गतिविधि पर लागू होने पर कोई समस्या नहीं है। मैं वैश्विक टीटीएस प्रणाली बनाना चाहता हूं –

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

  • कोई संबंधित समस्या नहीं^_^