TTS and Touch Events Sysnchronization
<p>This is my code</p>

<pre><code>public boolean onTouch(View v, MotionEvent event)
{
    // TODO Auto-generated method stub

    String s = null;
    Button btn=null;
    Button b_prev=null;

    s = ((Button) v).getText().toString().trim();

    switch (event.getAction() &amp; MotionEvent.ACTION_MASK) {

    case MotionEvent.ACTION_DOWN:
        b_prev=(Button)v;
        //while(tts.isSpeaking());
        tts.speak(s, TextToSpeech.QUEUE_FLUSH, null);
        break;
    case MotionEvent.ACTION_UP:
        for(int i=0;i&lt;mybtn.length;i++)
        {
            if(isPointInsideView(event.getRawX(),event.getRawY(),mybtn[i]))
            {
                s = (mybtn[i]).getText().toString().trim();
                btn=mybtn[i];
                break;
              //et.setText("up android" + s, TextView.BufferType.EDITABLE);
            }
        }

        if(btn.equals(mybtn[27])) //clear button pressed
        {
             s = et.getText().toString().trim();
                int l = s.length();

                if (l &gt; 0) {
                    String t,p;
                    if (l &gt; 1)
                    {
                        t = s.substring(0, l-1);
                    }
                    else
                        t = "";
                    p=s.substring(l-1, l);
                    p.trim();
                    et.setText(t, TextView.BufferType.EDITABLE );

                    //while(tts.isSpeaking());
                    tts.stop();
                    tts.speak("ok"+p,TextToSpeech.QUEUE_FLUSH , null);
                    while(tts.isSpeaking());
                    tts.speak("deleted",TextToSpeech.QUEUE_FLUSH , null);
                    //Toast.makeText(TimerActivity.this,p+"deleted",Toast.LENGTH_LONG).show();

                }
        }

        else if(btn.equals(mybtn[26])) //space button
        {
             //s = et.getText().toString().trim();
            et.setText(et.getText() + " ",TextView.BufferType.EDITABLE );
            tts.stop();
            tts.speak("OK space entered",TextToSpeech.QUEUE_FLUSH , null);
             //get_Number(s);

        }

        else
        {
        //String s1= et.getText().toString().trim();
        et.setText(et.getText() + "" + s,TextView.BufferType.EDITABLE );
        //while(tts.isSpeaking());
        tts.stop();
        tts.speak("ok"+s,TextToSpeech.QUEUE_FLUSH , null);
        while(tts.isSpeaking());
        tts.speak("entered",TextToSpeech.QUEUE_FLUSH , null);
        }
        //et.setText("up android" + s, TextView.BufferType.EDITABLE);
        //return true;
        while(tts.isSpeaking());
        //fun(true);
        break;

    case MotionEvent.ACTION_MOVE:
        //if (current != Integer.valueOf(s)) {
            //current = Integer.valueOf(s);
        //}
        //System.out.println("move android"+s);
        for(int i=0;i&lt;mybtn.length;i++)
        {
            if(isPointInsideView(event.getRawX(),event.getRawY(),mybtn[i]))
            {
                s = (mybtn[i]).getText().toString().trim();
                btn=mybtn[i];
                break;
              //et.setText("down android" + s, TextView.BufferType.EDITABLE);

            }
        }

        //while(tts.isSpeaking());
        tts.stop();
        if(btn!=b_prev)
        {
            //if(b_prev!=null)
            System.out.println(b_prev.getText().toString()+" "+btn.getText().toString());
            tts.speak(s, TextToSpeech.QUEUE_FLUSH, null);
        }
        b_prev=btn;
        //finish();
        //return false;
        //fun(true);
        break; 
    }

    return true;
}
</code></pre>

<p>The problem is with the btn and b_prev buttons. When the user slides on the keypad, the button  currently being touched should be spoken. It is happening and working well, but if the user moves slowly within the same button as well, it is pronouncing the letter which should not happen, it is because <code>b_prev</code> is getting set to NULL every time the TOUCH event happens. </p>

<p>So to avoid that problem I have deleted those two variables and declared them as global in my class. Using the following line, I am getting the expected result in the logcat. This also shows that the TTS engine is receiving various letters correctly.</p>

<pre><code>System.out.println(b_prev.getText().toString()+" "+btn.getText().toString()); 
</code></pre>

<p>However, it is not speaking the letters consistently, it is speaking sometimes and not speaking some times. But in the local variables case, it used to work well. I am not able to figure out the problem and debug it because there are no changes in code. Everything is same, no errors.</p>

<p>I think the problem is related to synchronizing TTS and touch events because I have to use various TTS functions. Please shed some light on this. I want some good advice on using TTS to handle these sync problems. Please help me. Thanks in advance.</p>
