[英]Adding a delay after sound output in Android Studio
我目前正在为我的第一个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以毫秒为单位,这将增加300ms的延迟
您只需要在代码中添加delay方法即可。 有三种最常见的方法可以实现目标。
new Thread() { @Overrride public void run() { super.run(); Thread.sleep(time); // the time is how long you need to delay // TODO: your code } }
new Handler().postDelay( new Runnable() { public void run() { // TODO: your code } }, time); // time is how long your need delay
TimerTask task = new TimerTask(){ public void run(){ //execute the task } }; Timer timer = new Timer(); timer.schedule(task, delay);
我已经将其与以下代码一起使用似乎运行良好,但是这种方式将来会导致错误吗?
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.