0

In this code snippet, I want the user to experience the following in the respective order: 1. See the letter he/she entered. 2. Hear the 'wrongAns' sound. 3. After the sound has finished, replace his text by the default "0".

Here is what I tried:

    tv_1.setText(input1);
    tv_1.setTextColor(Color.BLACK);
    wrongAns.start();
    try{
        Thread.sleep(2300);
    }catch(InterruptedException ex) {
        Thread.currentThread().interrupt();
    }
//  fullSentence.start();
    tv_1.setText("0");
    tv_1.setTextColor(Color.RED);

I tried including another sound clip 'fullSentence' to see in exactly what order everything is executing.

What actually happens: 1st: I see "0" in red, and at the same time, I hear the 'wrongAns' sound. 2nd: I hear 'fullSentence' 2.3s after 'wrongAns' began.

How do I get this to work properly?

EDIT: (This is the Class)

public class TestCorrectSentence extends Activity implements OnClickListener {

    MediaPlayer fullSentence, correctAns, wrongAns;
    TextView    tv_1, tv_2, tv_3, tv_4, tv_5, tv_6, tv_7, tv_8, tv_9, tv_10;
    String      A_1, A_2, A_3, A_4, A_5, A_6, A_7, A_8, A_9, A_10;
    int         counter;
    EditText    user_input;
    Button      b_Chk;
    String      input1, input2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sentence_activity);
        SoundsMethod();
        TextViewsMethod();
        InitializeMethod();
        ETandButtonMethod();
    }

    private void SoundsMethod() {
        fullSentence = MediaPlayer.create(TestCorrectSentence.this, R.raw.voice_sent);
        wrongAns = MediaPlayer.create(TestCorrectSentence.this, R.raw.voice_wrong);
    }

    private void TextViewsMethod() {
        tv_1  = (TextView) findViewById(R.id.tvAns1);
    }

    private void InitializeMethod() {
        A_1  = tv_1.getText().toString();

        counter = 1;
    }

    private void ETandButtonMethod() {
        user_input = (EditText) findViewById(R.id.etInput);

        b_Chk = (Button) findViewById(R.id.bCheck);
        b_Chk.setOnClickListener(this);     
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()) {
            case R.id.bCheck:
                try{
                    CheckAnsMethod();
                }catch(Exception e){
                    e.printStackTrace();
                }
            break;
        }
    }

    private void CheckAnsMethod() {
        // TODO Auto-generated method stub
        if (counter == 1){
            if ("0".equals(A_1)){
                input1 = user_input.getText().toString();
                if ("T".equals(input1)){
                    tv_1.setText(input1);
                    tv_1.setTextColor(Color.GREEN);
                    correctAns.start();
                    counter++;
                }else{
                    wrongAns.start();
                    tv_1.setText(input1);
                    tv_1.setTextColor(Color.BLACK);
                    try{
                        Thread.sleep(2300);
                    }catch(InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                    tv_1.setText("0");
                    tv_1.setTextColor(Color.RED);
                    fullSentence.start();
                }
            }else{
//              wrongAns.start();
//              fullSentence.start();
            }
        }else if (counter == 2){
            if ("0".equals(A_2)){
                input2 = user_input.getText().toString();
                if ("h".equals(input2)){
//                  tv_2.setText(input2);
//                  tv_2.setTextColor(Color.GREEN);
//                  tv_2.setTypeface(null, Typeface.BOLD);;
//                  correctAns.start();
//                  correctAns.start();
//                  counter++;
                }else{
//                  tv_2.setText(input2);
//                  tv_2.setTextColor(Color.BLACK);
//                  tv_2.setTypeface(null, Typeface.BOLD);;
//                  wrongAns.start();
//                  wrongAns.start();
//                  tv_2.setText("0");
//                  tv_2.setTypeface(null, Typeface.BOLD);;
                }
            }
        }
    }
}
Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
Keegs
  • 95
  • 3
  • 11
  • What frame work are you using? Swing/AWT/SWT/GWT/JavaFX? – MadProgrammer Feb 10 '15 at 05:37
  • @MadProgrammer I don't even know...is there a way I can find out so I can tell you? – Keegs Feb 10 '15 at 05:45
  • What is `tv_1`? Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This will result in less confusion and better responses – MadProgrammer Feb 10 '15 at 05:50
  • tv_1 is a textview. sorry about that. – Keegs Feb 10 '15 at 05:54
  • Sounds like JavaFX. Start with [Concurrency in JavaFX](http://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm), maybe take a look at [Using the JavaFX AnimationTimer](http://blog.netopyr.com/2012/06/14/using-the-javafx-animationtimer/) or [Timers in JavaFX](http://tomasmikula.github.io/blog/2014/06/04/timers-in-javafx-and-reactfx.html) – MadProgrammer Feb 10 '15 at 06:01
  • Use `Handler` for your requirement. – Piyush Feb 10 '15 at 06:08
  • only classes with captial letters. – Martin Pfeffer Feb 10 '15 at 06:23

1 Answers1

1

You have to use MediaPlayer lifecycle listeners here, something like that:

final TextView textView1 = tv_1;
wrongAns.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    public void onCompletion(MediaPlayer mp) {
        // reset your values here
        textView1.setText("0");
    }
});
wrongAns.start();

More information: Android dev. guide about media playback and MediaPlayer.OnCompletionListener javadoc