2013-11-22 25 views
0

我正在Texttospeech接口上工作。其中我經過一些處理後添加了一些文本。根據谷歌Android開發人員,你必須寫一個常量在講方法TextToSpeech.QUEUE_ADD,但它不適合我,可以不能幫助我。texttospeak.QUEUE_ADD常量不起作用

protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_audio_bus); 
     textView = (EditText) findViewById(R.id.ipEditText); 
     welcomeNote = (TextView) findViewById(R.id.welcomeTextView); 
     text = welcomeNote.getText().toString(); 
     speakText= text.concat(" , Please wait we are retriving information"); 
     System.out.println("1 "+ speakText); 
     IP = getIntent().getExtras().getString("IP"); 

     tts = new TextToSpeech(this, new OnInitListener() { 


      @Override 
      public void onInit(int status) { 
       // TODO Auto-generated method stub 
       if (status == TextToSpeech.SUCCESS) { 
        tts.setLanguage(Locale.ENGLISH); 
        //tts.setSpeechRate(1.1f); 
        speakOut(speakText); 
        //tts.speak(IP, TextToSpeech.QUEUE_ADD, null); 
       } 
      } 
     }); 

      //http post 
     try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://"+IP+"/checkingbusno.php"); 
      List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>(1); 
      nameValuePairs.add(new BasicNameValuePair("BusStopName", "thakur_complex")); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 
     }catch(Exception e){ 
      Log.e("log_tag", "Error in http connection"+e.toString()); 
     } 

     //convert response to string 
     try{ 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
      sb = new StringBuilder(); 
      sb.append(reader.readLine() + "\n"); 
      String line="0"; 

      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 

      is.close(); 
      result=sb.toString(); 
      textView.setText(result); 
      //initialising once again tts value 
      tts.speak(result, TextToSpeech.QUEUE_ADD,null); 
     }catch(Exception e){ 
      Log.e("log_tag", "Error converting result "+e.toString()); 
     } 





    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 



    private void speakOut(String text) { 

     //String text = txtText.getText().toString(); 

     tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); 
    } 

    @Override 
    public void onDestroy() { 
     // Don't forget to shutdown! 
     if (tts != null) { 
      tts.stop(); 
      tts.shutdown(); 
     } 
     super.onDestroy(); 
    } 


} 
+0

你可以在你想要做的事上添加一些代碼嗎? –

+0

我已更新那東西 – Ashishsingh

回答

0

您對tts.speak(result, TextToSpeech.QUEUE_ADD,null);呼叫正的TTS之前完成已被初始化。

QUEUE_ADD標誌在那裏處理併發請求,在它被初始化後發言。假設你的TTS已經在講話,並且你想讓它說另外的話。在這種情況下,您將它添加到隊列中以便接下來說出。無論哪種方式,使用刷新或添加隊列,您需要在onInit()已返回status == TextToSpeech.SUCCESS後致電tts.speak()

編輯:

如果您有:

speakText = result; 

希望它能幫助:

tts.speak(result, TextToSpeech.QUEUE_ADD,null); 

將其替換。

+0

但我打電話給initalising oninit方法 – Ashishsingh

+0

不,你不是。當你調用'new TextToSpeech()'時,你不能保證'onInit()'會被立即調用。你的'onCreate()'方法很可能會在回調到'onInit()'之前繼續執行其餘的代碼,直到'tts.speak()'調用。 –

+0

然後如何控制這個東西........ – Ashishsingh