FlandersNed
FlandersNed

Reputation: 63

Java error in Eclipse - soundresource cannot be resolved to a variable

I am trying to import a music file into my program, but also allow it to randomly choose between two of them when it starts. I use the URL system for importing, so I have a variable called soundResource to accomplish this task.

However, whenever I try to run it (using Eclipse, though I don't think that is causing the error) the program fails to, stating that

soundResource cannot be resolved to a variable

I have seen in other topics not about this in particular that it has something to do with the scope of the variables. How do I fix this code below in order to make the variables see each other?

Applicable code:

package Tetris;

//import java.io.InputStream;

import java.net.URL;
import java.util.Random;
import javax.sound.sampled.*;

public class Audio {
    Random rand = new Random(); 
    Boolean RandomSongNum = rand.nextBoolean();
    AudioInputStream ais;
    Clip clip;

    public Audio () {};

    public void playAudio () throws Exception {
        //open the sound file as a Java input stream
        //The Starter music file
        if(RandomSongNum){
            URL soundResource = this.getClass().getClassLoader().getResource("BH-Lightest.wav");
        } else {
            URL soundResource = this.getClass().getClassLoader().getResource("RY-Lightest.wav");
        }

        //Set audio path to within java file
        AudioInputStream in = AudioSystem.getAudioInputStream(soundResource); <<<--- Error occurs here

        clip = AudioSystem.getClip();

        clip.open(in);
        clip.loop(Clip.LOOP_CONTINUOUSLY);  //get the file to loop continuously 

    }

    public void playAudioMed () throws Exception {
        //open the sound file as a Java input stream
        //Medium score music file
        if (RandomSongNum) {
            URL soundResource = this.getClass().getClassLoader().getResource("BH-Light.wav");
        } else {
            URL soundResource = this.getClass().getClassLoader().getResource("RY-Light.wav");
        }

        //Set audio path to within java file
        AudioInputStream in = AudioSystem.getAudioInputStream(soundResource); <<<--- Error occurs here

        clip = AudioSystem.getClip();

        clip.open(in);
        clip.loop(Clip.LOOP_CONTINUOUSLY);  //get the file to loop continuously 

    }

    public void playAudioHi () throws Exception {
        //open the sound file as a Java input stream
        //High Score Music File
        if(RandomSongNum) {
            URL soundResource = this.getClass().getClassLoader().getResource("BH.wav");
        } else {
            URL soundResource = this.getClass().getClassLoader().getResource("RY.wav");
        }

        //Set audio path to within java file
        AudioInputStream in = AudioSystem.getAudioInputStream(soundResource); <<<--- Error occurs here

        clip = AudioSystem.getClip();

        clip.open(in);
        clip.loop(Clip.LOOP_CONTINUOUSLY);  //get the file to loop continuously 

    }

    public void stopAudio() {
        clip.stop();
    }



}

Upvotes: 0

Views: 1183

Answers (2)

McLovin
McLovin

Reputation: 3674

The variable is declared in the if block, so it is only in scope during the if. Therefore, you can't reference it later in the code. Move the declaration to before the if to fix it.

URL soundResource;
if(RandomSongNum){
    soundResource = this.getClass().getClassLoader().getResource("BH-Lightest.wav");
} else {
    soundResource = this.getClass().getClassLoader().getResource("RY-Lightest.wav");
}

Upvotes: 0

Jason
Jason

Reputation: 11822

This is because you are declaring your variable soundResource within an if block, then attempting to access it outside the block.

Try this pattern:

// declare the variable outside the if block
URL soundResource;
if(RandomSongNum){
    soundResource = this.getClass().getClassLoader().getResource("BH-Lightest.wav");
} else {
    soundResource = this.getClass().getClassLoader().getResource("RY-Lightest.wav");
}

//Set audio path to within java file
AudioInputStream in = AudioSystem.getAudioInputStream(soundResource);

Upvotes: 1

Related Questions