Use this function: it will give you a random int
between min
and max
.
/** Returns a random int in the specified range. */
static int randomInt(float min, float max) {
if (max < min) {
max = min;
}
int randInt = (int) (new Random(System.nanoTime()).nextInt((int) (max - min + 1)) + min);
return randInt;
}
You can create a simple loop using a Handler
to fade the volume in and out as well:
void increaseVolume() {
if (volume < 1) {
volume += 0.01;
new Handler().postDelayed(new Runnable {
@Override
public void run() {
increaseVolume();
mp.setVolume(volume);
}
}, 100);
} else {
volume = 1;
}
}
Remember to also add float volume = 0
at the start of your activity to store the volume in between adjustments.