texttospeak.QUEUE_ADD常量不起作用

时间:2013-11-22 09:39:51

标签: android

我正在研究Texttospeech界面。我在一些处理后添加一些文本。根据谷歌Android开发人员你必须写一个常量说话方法作为TextToSpeech.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();
    }


}

1 个答案:

答案 0 :(得分:0)

在初始化tts之前,您对tts.speak(result, TextToSpeech.QUEUE_ADD,null);的呼叫已经完成。

QUEUE_ADD标志用于处理同步请求,以便在初始化后发言。假设你的tts已经说话了,你想让它说些别的话。在这种情况下,您将其添加到接下来要说的队列中。无论哪种方式,使用刷新或添加到队列,您需要在tts.speak()返回onInit()后致电status == TextToSpeech.SUCCESS

编辑:

你在哪里:

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

将其替换为:

speakText = result;

希望它有所帮助。