문제

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