The float targetVolume
in created in a public void
method, but cannot be resolved in another public void
method...
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
float x = (float) Math.random();
if (x < 0.5){
float targetVolume = 0;
} else {
float targetVolume = 1;
}
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
if (targetVolume.equals (1)){
mp.setVolume(startingVolume+volumeIncrement, startingVolume+volumeIncrement);
} else {
mp.setVolume(startingVolume-volumeIncrement, startingVolume-volumeIncrement);
}
}
}, 0, 1, TimeUnit.SECONDS);
}
}, 0, 5, TimeUnit.SECONDS);
How can I fix this?
EDIT: I can solve the problem by adding the final modifier to the float:
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
float x = (float) Math.random();
final float targetVolume=(x < 0.5)?0:1;
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
if (targetVolume >startingVolume){
startingVolume = startingVolume+volumeIncrement;
mp.setVolume((startingVolume), (startingVolume));
}
else if (targetVolume < startingVolume){
startingVolume = startingVolume-volumeIncrement;
mp.setVolume((startingVolume), (startingVolume));
}
}
}, 0, 1, TimeUnit.SECONDS);
}
}, 0, 5, TimeUnit.SECONDS);
However, does the final float targetVolume now hold the same value (a zero or a 1) each time the task is run? I need this value to change randomly...