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???

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top