0

问题:

我正在设计一个小型 Android 应用程序,当您按下看起来像特定乐器的图像按钮时播放声音(音符),并且您可以在 6 个图像按钮上播放多达 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(); 
                    }
                }
            }
    };
4

1 回答 1

4

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

于 2013-05-16T21:18:27.923 回答