Question

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

Was it helpful?

Solution

You have declared it locally to that method

cheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
    public void run() {
        float x = (float) Math.random();
        if (x <0.5){
            float targetVolume =0;}

declare it as a member variable to the class to have class scope. This means to define it anywhere outside of a method (usually just before onCreate() for an Activity).

Read about Java variable scope

OTHER TIPS

Change to:

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==1){
                    mp.setVolume(startingVolume+volumeIncrement, startingVolume+volumeIncrement);
                } else {
                    mp.setVolume(startingVolume-volumeIncrement, startingVolume-volumeIncrement);
                }
            }
        }, 0, 1, TimeUnit.SECONDS);
    }
}, 0, 5, TimeUnit.SECONDS);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top