简体   繁体   中英

Adding a delay after sound output in Android Studio

I'm currently coding my first App for Android. I am parsing a string and passing the value into a switch.

The value passed into the switch outputs the corresponding sound. The problem is it outputs all the sounds at the same time overlapping eachother.

Is there anyway to add a slight delay after the sound is played to stop this happening?

 for(int j =0; j<resultString.length(); j++)
    {
     int i = resultString.charAt(j);
    switch(i) {
        case '1':
            one.start(); 
            break;
        case '2':
            two.start();
            break;
        case '3':
            three.start();
            break;
        case '4':
            four.start();
            break;
        case '5':
            five.start();
            break;
        case '6':
            six.start();
            break;
        case '7':
            seven.start();
            break;
        case '8':
            eight.start();
            break;
        case '9':
            nine.start();
            break;
        case '0':
            zero.start();
            break;
        case '.':
            j=resultString.length();
            break;
    }
}

Above is my code that relates to this question. If anymore code is needed etc. let me know.

Edit:

I have now got it working with the following code:

Is this a bad way of doing it?

private void readAnswer() {
    long startTime;
    for(int j =0; j<resultString.length(); j++)
    {
     int i = resultString.charAt(j);
    switch(i) {
        case '1':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < one.getDuration()) {
                one.start();
            }
            break;
        case '2':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < two.getDuration()) {
                two.start();
            }
            break;
        case '3':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < three.getDuration()) {
                three.start();
            }
            break;
        case '4':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < four.getDuration()) {
                four.start();
            }
            break;
        case '5':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < five.getDuration()) {
                five.start();
            }
            break;
        case '6':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < six.getDuration()) {
                six.start();
            }
            break;
        case '7':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < seven.getDuration()) {
                seven.start();
            }
            break;
        case '8':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < eight.getDuration()) {
                eight.start();
            }
            break;
        case '9':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < nine.getDuration()) {
                nine.start();
            }
            break;
        case '0':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < zero.getDuration()) {
                zero.start();
            }
            break;
        case '.':
            j=resultString.length();
            break;
    }


    }
}

try this code

for(int j =0; j<resultString.length(); j++)
{
    int i = resultString.charAt(j);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            switch(i) {
                case '1':
                    one.start();
                    break;
                case '2':
                    two.start();
                    break;
                case '3':
                    three.start();
                    break;
                case '4':
                    four.start();
                    break;
                case '5':
                    five.start();
                    break;
                case '6':
                    six.start();
                    break;
                case '7':
                    seven.start();
                    break;
                case '8':
                    eight.start();
                    break;
                case '9':
                    nine.start();
                    break;
                case '0':
                    zero.start();
                    break;
                case '.':
                    j=resultString.length();
                    break;
            }

        }
    },300*j);

}

where 300 is in milli seconds , this will add delay everytime of 300ms

You just need to add the delay method in your code. There are three most common ways to get the goal.

  1. Put your code in one thread and use the Thread.sleep().
 new Thread() { @Overrride public void run() { super.run(); Thread.sleep(time); // the time is how long you need to delay // TODO: your code } } 
  1. Handler.postDelay just like the first answer.
 new Handler().postDelay( new Runnable() { public void run() { // TODO: your code } }, time); // time is how long your need delay 
  1. Use the Timer to set a delay code
 TimerTask task = new TimerTask(){ public void run(){ //execute the task } }; Timer timer = new Timer(); timer.schedule(task, delay); 

I have got it working with the following code seems to run well but will this way of doing it cause errors in the future?

private void readAnswer() {
    long startTime;
    for(int j =0; j<resultString.length(); j++)
    {
     int i = resultString.charAt(j);
    switch(i) {
        case '1':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < one.getDuration()) {
                one.start();
            }
            break;
        case '2':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < two.getDuration()) {
                two.start();
            }
            break;
        case '3':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < three.getDuration()) {
                three.start();
            }
            break;
        case '4':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < four.getDuration()) {
                four.start();
            }
            break;
        case '5':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < five.getDuration()) {
                five.start();
            }
            break;
        case '6':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < six.getDuration()) {
                six.start();
            }
            break;
        case '7':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < seven.getDuration()) {
                seven.start();
            }
            break;
        case '8':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < eight.getDuration()) {
                eight.start();
            }
            break;
        case '9':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < nine.getDuration()) {
                nine.start();
            }
            break;
        case '0':
            startTime =  System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < zero.getDuration()) {
                zero.start();
            }
            break;
        case '.':
            j=resultString.length();
            break;
    }


    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM