[英]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.