为什么我的if语句在这个for循环中不起作用?

时间:2013-05-16 21:03:28

标签: java android audio android-mediaplayer runnable

问题:

我正在设计一个小型Android应用程序,当您按下看起来像特定乐器的imageButton时播放声音(音符),并且您可以在6个imageButtons上播放多达10个音符。它将音符编号存储在一个数组中,并将该元素设置为特定的数字。 这部分似乎工作正常

该应用有一项功能,可让您播放最近播放的内容。由于某种原因,它只播放第一个音符10次 - 它似乎不理解新的数组值,所以它总是挂在for循环中的第一个if语句上。我需要它来读取新的数组值,以便它可以播放用户在给定乐器上播放的内容。

代码:

public void drum1button(View view) { //This code snippet works as expected
    if (studio.drumCount < 10){
        new Thread(drum.drum1).start();
        drum.drums[studio.drumCount] = 0;
        Toast.makeText(getApplicationContext(),studio.drumCount + " drum.drums[studio.drumCount] = " + drum.drums[studio.drumCount],2).show();

        studio.drumCount++;
    }
    else {
    Toast.makeText(getApplicationContext(),"Record limit reached. Head to the studio!",5).show();
    }
}


public void drum2button(View view) {
if (studio.drumCount < 10){
    new Thread(drum.drum2).start();
    drum.drums[studio.drumCount] = 1;
    Toast.makeText(getApplicationContext(),studio.drumCount + " drum.drums[studio.drumCount] = " + drum.drums[studio.drumCount],2).show();

    studio.drumCount++;

}
else {
Toast.makeText(getApplicationContext(),"Record limit reached. Head to the studio!",5).show();
}
}


public void drum3button(View view) {
    if (studio.drumCount <10 ) {
    new Thread(drum.drum3).start();
    drum.drums[studio.drumCount] = 2;
    Toast.makeText(getApplicationContext(),studio.drumCount + " drum.drums[studio.drumCount] = " + drum.drums[studio.drumCount],2).show();

    studio.drumCount++;
    }
    else {
    Toast.makeText(getApplicationContext(),"Record limit reached. Head to the studio!",5).show();
    }

}

public void drum4button(View view) {
    if (studio.drumCount < 10) {
    new Thread(drum.drum4).start();
    drum.drums[studio.drumCount] = 3;
    Toast.makeText(getApplicationContext(),studio.drumCount + " drum.drums[studio.drumCount] = " + drum.drums[studio.drumCount],2).show();

    studio.drumCount++;
    }
    else {
    Toast.makeText(getApplicationContext(),"Record limit reached. Head to the studio!",5).show();
    }
}

public void drum5button(View view) {
    if (studio.drumCount < 10) {
    new Thread(drum.drum5).start();
    drum.drums[studio.drumCount] = 4;
    Toast.makeText(getApplicationContext(),studio.drumCount + " drum.drums[studio.drumCount] = " + drum.drums[studio.drumCount],2).show();

    studio.drumCount++;
    }

    else {
    Toast.makeText(getApplicationContext(),"Record limit reached. Head to the studio!",5).show();
    }
}

public void drum6button(View view) {
    if (studio.drumCount < 10){
    new Thread(drum.drum6).start();
    drum.drums[studio.drumCount] = 5;
    Toast.makeText(getApplicationContext(),studio.drumCount + " drum.drums[studio.drumCount] = " + drum.drums[studio.drumCount],2).show();

    studio.drumCount++;
    }
    else {
        Toast.makeText(getApplicationContext(),"Record limit reached. Head to the studio!",5).show();

    }
}

Runnable drumPlayback = new Runnable() {

    public void run() {
    for (int i = 0; i < 10; i++) { //(was i = 10, typo in formatting when //pasting. Still does not work with i = 0
        //handler.postDelayed(drumPlayback, 5000);

        try {Thread.sleep(1000);} catch(Exception e) {}
         if (drum.drums[i] == 0) { 
          new Thread(drum.drum1).start(); //this is the only one that plays

          }
          if (drum.drums[i] == 1) { 
          new Thread(drum.drum2).start(); 
          } 
         if (drum.drums[i] == 2) { 
          new Thread(drum.drum3).start(); 
          } 
           if (drum.drums[i] == 3) { 
          new Thread(drum.drum4).start(); 
          } 
           if (drum.drums[i] == 4) { 
          new Thread(drum.drum5).start(); 
          } 
           if (drum.drums[i] == 5) {
          new Thread(drum.drum6).start(); 
                    }
                }
            }
    };

1 个答案:

答案 0 :(得分:4)

循环条件是i < 10但是你用10初始化。所以循环永远不会启动。