我目前正在编写我的第一个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;
}
}
}
答案 0 :(得分:1)
试试这段代码
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毫秒的延迟
答案 1 :(得分:1)
您只需在代码中添加延迟方法即可。有三种最常见的方法来实现目标。
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);
答案 2 :(得分:0)
我已经使用以下代码似乎运行良好,但是这样做会不会导致错误?
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;
}
}
}