简体   繁体   English

在Android Studio中添加声音输出后添加延迟

[英]Adding a delay after sound output in Android Studio

I'm currently coding my first App for Android. 我目前正在为我的第一个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 其中300以毫秒为单位,这将增加300ms的延迟

You just need to add the delay method in your code. 您只需要在代码中添加delay方法即可。 There are three most common ways to get the goal. 有三种最常见的方法可以实现目标。

  1. Put your code in one thread and use the Thread.sleep(). 将您的代码放在一个线程中,然后使用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. Handler.postDelay就像第一个答案一样。
 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;
    }


    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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