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

标签 java android android-studio audio

我目前正在编写我的第一个 Android 应用程序。我正在解析一个字符串并将值传递到一个开关中。

传入开关的值会输出相应的声音。问题是它同时输出所有声音,并且彼此重叠。

有没有办法在声音播放后添加轻微的延迟来阻止这种情况发生?

 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;
    }
}

上面是我与这个问题相关的代码。如果需要更多代码等,请告诉我。

编辑:

我现在已经使用以下代码使其工作:

这是一个不好的做法吗?

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;
    }


    }
}

最佳答案

试试这个代码

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);

}

其中 300 的单位是毫秒,这将每次增加 300 毫秒的延迟

关于java - 在Android Studio中添加声音输出后的延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42342443/

相关文章:

java - 从 unity Activity 返回到 android studio 中的 MainActivity

Android 应用程序在启动时崩溃

java - 通过java App查看PDF

android - 在 Android 中使用 3G 和 Wifi 检查互联网连接

java - 如何自定义 Spinner 使其向顶部打开?

java - Twitter4J,直接消息

java - For 循环和 while 循环跳过 Scanner

java - 使用 AffineTransform 旋转动画 GIF (ImageIcon)

java - Weka:Src 和 Dest 的属性数量不同:使用 java

android - Android Studio Layout Inspector 中的 mID 是什么?