Android:当计数器达到整数时播放声音(仅限向上)
Android: Play sound when counter reaches integer (Going UP only)
我在我的应用程序中遇到了新问题,这不是错误而是我遇到的挑战。
所以在我的应用程序中,当检测到一定级别的声音时,计数器(显示在 TextView 中)开始计数。目前有一定的等级(基于龙珠 Z 的功率等级),例如 1 到 300'000,然后是 300'001 到 30'000'000,最后是 30'000'001 到 15'000'000。当不再检测到声音时,功率水平缓慢下降。
我有一个声音文件,我需要在达到每一层时播放它。因此,一旦计数器开始,声音文件就应该播放一次。然后当达到第二层(300'000)时,它再次播放。
我的条件是,文件应该只在功率级别上升时播放,所以从 299'999 到 300'000 它会播放。但是,如果功率级别从 300'001 下降到 300'000,则不应播放声音。此外,由于要达到的数字很大,计数器不会增加 1,而是根据层级增加一个更大的数字。
例如,从 300'000 到 30'000'000 一次数 1 将花费很长时间。除非我要非常快地计算这个,否则会很棒,但我不确定这是否可能。所以在那个例子中,我目前让它每 100 毫秒增加 312'345(用于测试的随机数)(所以每秒 3'123'450)。
所以我不能简单地说...
if (currentPowerlevel == 300000) {mSounds.play(etc)}
由于计数增加,这个确切的数字可能会被跳过。所以我尝试使用...
if (currentPowerlevel > 300000 && currentPowerlevel < 612344) {mSounds.play(etc)}
希望这意味着该文件将只播放一次,因为即使计数器正好达到 300'000,由于该层的增量,下一个计数将是 612'345。但这不仅很混乱,而且如果更改层级/计数速度需要大量麻烦的代码更改,而且它似乎仍然无法正常工作并重复播放文件。
所以我将 post 下面的所有相关代码,希望有人能克服这个挑战!理想情况下,我希望以下内容为真:
- 计数器每次计数增加 1,速度可调以适应大等级,例如 3'000'000 到 15'000'000。
- 声音文件只能在计数上升时播放,在计数下降时不能播放。
有某种冷却时间,或每 10 秒允许播放的最大声音,以阻止文件被重复。
public void countPowerLevel() {
// Goku Start
if (currentAmplitude > 9000 && currentPowerLevel <= 299999) {
currentPowerLevel = (currentPowerLevel) + 3123;
} else if (currentAmplitude <= 8999 && currentPowerLevel <= 299999) {
currentPowerLevel = (currentPowerLevel) - 312;
}
// Goku Kaioken (300'000)
if (currentAmplitude > 9000 && currentPowerLevel >= 300000) {
currentPowerLevel = (currentPowerLevel) + 312345;
} else if (currentAmplitude <= 8999 && currentPowerLevel >= 300000 && currentPowerLevel <= 29999999){
currentPowerLevel = (currentPowerLevel) - 31234;
}
// Goku Kaioken x 10 (30'000'000)
if (currentAmplitude > 9000 && currentPowerLevel >= 30000000) {
currentPowerLevel = (currentPowerLevel) + 1512345;
} else if (currentAmplitude <= 8999 && currentPowerLevel >= 30000000 && currentPowerLevel <= 149999999) {
currentPowerLevel = (currentPowerLevel) - 151234;
}
// Goku Super Saiyan 1 (150'000'000)
if (currentAmplitude > 9000 && currentPowerLevel >= 150000000) {
currentPowerLevel = (currentPowerLevel) + 8012345;
} else if (currentAmplitude <= 8999 && currentPowerLevel >= 150000000 && currentPowerLevel <= 799999999) {
currentPowerLevel = (currentPowerLevel) - 801234;
}
if (currentPowerLevel > 800000000) {
currentPowerLevel = (currentPowerLevel) - 801234;
}
// Stop power from going below 1
if (currentPowerLevel < 1) {
currentPowerLevel = 1;
}
// Alerts for reaching power level milestones
TextView textCurrentForm = (TextView) findViewById(R.id.textCurrentForm);
ImageView imgCurrentForm = (ImageView) findViewById(R.id.imgCurrentForm);
imgCurrentForm.setImageResource(R.drawable.goku);
// Goku Kaioken (300'000)
if (currentPowerLevel >= 300000) {
textCurrentForm.setText("Kaioken!");
imgCurrentForm.setImageResource(R.drawable.gokukk10);
}
// Goku Kaioken x 10 (30'000'000)
if (currentPowerLevel >= 30000000) {
textCurrentForm.setText("Kaioken x 10!");
imgCurrentForm.setImageResource(R.drawable.gokukk100);
}
// Goku Super Saiyan 1 (150'000'000)
if (currentPowerLevel >= 150000000) {
textCurrentForm.setText("Super Saiyan!");
imgCurrentForm.setImageResource(R.drawable.gokussj);
}
// Trigger sounds per power level
if (currentPowerLevel > 1 && currentPowerLevel < 4000) {
mSounds.play(sndAura01, 1, 1, 1, 0, 1);
}
if (currentPowerLevel > 4000 && currentPowerLevel < 10000) {
mSounds.stop(sndAura01);
}
if (currentPowerLevel == 300000) {
mSounds.play(sndAura01, 1, 1, 1, 0, 1);
}
}
更新:
好的,我想出了我认为应该可行的方法,但由于某种原因它不起作用...
// Trigger sounds per power level
// Count amount of times sound has been played
int soundPlayed = 0;
if (currentPowerLevel > 1 && currentPowerLevel <= 299999) {
if (soundPlayed == 0) {
mSounds.play(sndAura01, 1, 1, 1, 0, 1);
soundPlayed = 1;
}
}
if (currentPowerLevel > 300000 && currentPowerLevel <= 2999999) {
if (soundPlayed == 1) {
mSounds.play(sndAura01, 1, 1, 1, 0, 1);
soundPlayed = 2;
}
}
有什么想法吗?
更新 2:
我弄清楚了为什么上面的代码不起作用,整个块都在处理程序中循环。因此,在将 soundPlayed 整数设置为 1 后,它会立即再次设置回 0。现在正在研究解决方案...
好的,我已经解决了
我所做的是首先在 class:
的开头设置一个整型变量 "soundPlayed"
// Count amount of times sound has been played
int soundPlayed = 0;
然后我按如下方式更改了声音触发器:
if (currentPowerLevel > 1 && currentPowerLevel <= 299999) {
if (soundPlayed == 0) {
mSounds.play(sndAura01, 1, 1, 1, 0, 1);
soundPlayed = 1;
}
}
if (currentPowerLevel > 300000 && currentPowerLevel <= 2999999) {
if (soundPlayed == 1) {
mSounds.play(sndAura01, 1, 1, 1, 0, 1);
soundPlayed = 2;
}
}
所以soundPlayed从0开始。当功率级别在1到299'999之间,并且soundPlayed等于0时,将播放声音并且变量soundPlayed现在将设置为1。这段代码是每 100 毫秒(每秒 10 次)触发一次,因为它是我的可运行处理程序的一部分。在第二次和循环的每隔一次迭代中,它将检查 soundPlayed 是否为 0,它不是(因为它现在是 1)并且不会再次播放音频文件。
然后一旦达到 300'000,它现在将检查 soundPlayed 是否等于 1,它是。它再次播放声音文件,现在将 soundPlayed 设置为 2。
我可以将它设置回 0,但这意味着如果功率水平下降(下降)回到 299'999 或更低,声音将再次播放,这是我不想要的。
希望这对任何需要实现类似目标的人有所帮助!
我在我的应用程序中遇到了新问题,这不是错误而是我遇到的挑战。
所以在我的应用程序中,当检测到一定级别的声音时,计数器(显示在 TextView 中)开始计数。目前有一定的等级(基于龙珠 Z 的功率等级),例如 1 到 300'000,然后是 300'001 到 30'000'000,最后是 30'000'001 到 15'000'000。当不再检测到声音时,功率水平缓慢下降。
我有一个声音文件,我需要在达到每一层时播放它。因此,一旦计数器开始,声音文件就应该播放一次。然后当达到第二层(300'000)时,它再次播放。
我的条件是,文件应该只在功率级别上升时播放,所以从 299'999 到 300'000 它会播放。但是,如果功率级别从 300'001 下降到 300'000,则不应播放声音。此外,由于要达到的数字很大,计数器不会增加 1,而是根据层级增加一个更大的数字。
例如,从 300'000 到 30'000'000 一次数 1 将花费很长时间。除非我要非常快地计算这个,否则会很棒,但我不确定这是否可能。所以在那个例子中,我目前让它每 100 毫秒增加 312'345(用于测试的随机数)(所以每秒 3'123'450)。
所以我不能简单地说...
if (currentPowerlevel == 300000) {mSounds.play(etc)}
由于计数增加,这个确切的数字可能会被跳过。所以我尝试使用...
if (currentPowerlevel > 300000 && currentPowerlevel < 612344) {mSounds.play(etc)}
希望这意味着该文件将只播放一次,因为即使计数器正好达到 300'000,由于该层的增量,下一个计数将是 612'345。但这不仅很混乱,而且如果更改层级/计数速度需要大量麻烦的代码更改,而且它似乎仍然无法正常工作并重复播放文件。
所以我将 post 下面的所有相关代码,希望有人能克服这个挑战!理想情况下,我希望以下内容为真:
- 计数器每次计数增加 1,速度可调以适应大等级,例如 3'000'000 到 15'000'000。
- 声音文件只能在计数上升时播放,在计数下降时不能播放。
有某种冷却时间,或每 10 秒允许播放的最大声音,以阻止文件被重复。
public void countPowerLevel() {
// Goku Start if (currentAmplitude > 9000 && currentPowerLevel <= 299999) { currentPowerLevel = (currentPowerLevel) + 3123; } else if (currentAmplitude <= 8999 && currentPowerLevel <= 299999) { currentPowerLevel = (currentPowerLevel) - 312; } // Goku Kaioken (300'000) if (currentAmplitude > 9000 && currentPowerLevel >= 300000) { currentPowerLevel = (currentPowerLevel) + 312345; } else if (currentAmplitude <= 8999 && currentPowerLevel >= 300000 && currentPowerLevel <= 29999999){ currentPowerLevel = (currentPowerLevel) - 31234; } // Goku Kaioken x 10 (30'000'000) if (currentAmplitude > 9000 && currentPowerLevel >= 30000000) { currentPowerLevel = (currentPowerLevel) + 1512345; } else if (currentAmplitude <= 8999 && currentPowerLevel >= 30000000 && currentPowerLevel <= 149999999) { currentPowerLevel = (currentPowerLevel) - 151234; } // Goku Super Saiyan 1 (150'000'000) if (currentAmplitude > 9000 && currentPowerLevel >= 150000000) { currentPowerLevel = (currentPowerLevel) + 8012345; } else if (currentAmplitude <= 8999 && currentPowerLevel >= 150000000 && currentPowerLevel <= 799999999) { currentPowerLevel = (currentPowerLevel) - 801234; } if (currentPowerLevel > 800000000) { currentPowerLevel = (currentPowerLevel) - 801234; } // Stop power from going below 1 if (currentPowerLevel < 1) { currentPowerLevel = 1; } // Alerts for reaching power level milestones TextView textCurrentForm = (TextView) findViewById(R.id.textCurrentForm); ImageView imgCurrentForm = (ImageView) findViewById(R.id.imgCurrentForm); imgCurrentForm.setImageResource(R.drawable.goku); // Goku Kaioken (300'000) if (currentPowerLevel >= 300000) { textCurrentForm.setText("Kaioken!"); imgCurrentForm.setImageResource(R.drawable.gokukk10); } // Goku Kaioken x 10 (30'000'000) if (currentPowerLevel >= 30000000) { textCurrentForm.setText("Kaioken x 10!"); imgCurrentForm.setImageResource(R.drawable.gokukk100); } // Goku Super Saiyan 1 (150'000'000) if (currentPowerLevel >= 150000000) { textCurrentForm.setText("Super Saiyan!"); imgCurrentForm.setImageResource(R.drawable.gokussj); } // Trigger sounds per power level if (currentPowerLevel > 1 && currentPowerLevel < 4000) { mSounds.play(sndAura01, 1, 1, 1, 0, 1); } if (currentPowerLevel > 4000 && currentPowerLevel < 10000) { mSounds.stop(sndAura01); } if (currentPowerLevel == 300000) { mSounds.play(sndAura01, 1, 1, 1, 0, 1); }
}
更新:
好的,我想出了我认为应该可行的方法,但由于某种原因它不起作用...
// Trigger sounds per power level
// Count amount of times sound has been played
int soundPlayed = 0;
if (currentPowerLevel > 1 && currentPowerLevel <= 299999) {
if (soundPlayed == 0) {
mSounds.play(sndAura01, 1, 1, 1, 0, 1);
soundPlayed = 1;
}
}
if (currentPowerLevel > 300000 && currentPowerLevel <= 2999999) {
if (soundPlayed == 1) {
mSounds.play(sndAura01, 1, 1, 1, 0, 1);
soundPlayed = 2;
}
}
有什么想法吗?
更新 2:
我弄清楚了为什么上面的代码不起作用,整个块都在处理程序中循环。因此,在将 soundPlayed 整数设置为 1 后,它会立即再次设置回 0。现在正在研究解决方案...
好的,我已经解决了
我所做的是首先在 class:
的开头设置一个整型变量 "soundPlayed"// Count amount of times sound has been played
int soundPlayed = 0;
然后我按如下方式更改了声音触发器:
if (currentPowerLevel > 1 && currentPowerLevel <= 299999) {
if (soundPlayed == 0) {
mSounds.play(sndAura01, 1, 1, 1, 0, 1);
soundPlayed = 1;
}
}
if (currentPowerLevel > 300000 && currentPowerLevel <= 2999999) {
if (soundPlayed == 1) {
mSounds.play(sndAura01, 1, 1, 1, 0, 1);
soundPlayed = 2;
}
}
所以soundPlayed从0开始。当功率级别在1到299'999之间,并且soundPlayed等于0时,将播放声音并且变量soundPlayed现在将设置为1。这段代码是每 100 毫秒(每秒 10 次)触发一次,因为它是我的可运行处理程序的一部分。在第二次和循环的每隔一次迭代中,它将检查 soundPlayed 是否为 0,它不是(因为它现在是 1)并且不会再次播放音频文件。
然后一旦达到 300'000,它现在将检查 soundPlayed 是否等于 1,它是。它再次播放声音文件,现在将 soundPlayed 设置为 2。
我可以将它设置回 0,但这意味着如果功率水平下降(下降)回到 299'999 或更低,声音将再次播放,这是我不想要的。
希望这对任何需要实现类似目标的人有所帮助!