Question

I'm trying to randomly change the volume of the media player (mp) every 2 seconds:

while (mp.isPlaying()) {
        float x = (float) Math.random();
        float y = (float) Math.random();
        setInterval((mp.setVolume(x,y)),2000);}

"The method setInterval is undefined for the type MainActivity"

How do I solve this???

Was it helpful?

Solution

Your use of a while loop is causing the problem. It is looping and not allowing other tasks to be handled. Refactor your code without the while loop.

Create a function that holds the two random calls and the set volume call and call that with setInterval. Then if that works go back to the function and add your mp.isPlaying condition in an if statement that wraps the other three statements. Then read up on clearing a setInterval.

Alternatively take a look at: Java equivalent of setInterval in javascript if you haven't implemented setInterval.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top