gpt4 book ai didi

Eclipse 中的 Java 错误 - soundresource 无法解析为变量

转载 作者:行者123 更新时间:2023-12-01 12:43:11 25 4
gpt4 key购买 nike

我正在尝试将音乐文件导入到我的程序中,但也允许它在启动时在其中两个文件之间随机选择。我使用 URL 系统进行导入,因此我有一个名为 soundResource 的变量来完成此任务。

但是,每当我尝试运行它(使用 Eclipse,尽管我不认为这会导致错误)时,程序都会失败,并指出

soundResource cannot be resolved to a variable

我在其他主题中看到与此无关的内容,特别是它与变量的范围有关。如何修复下面的代码以使变量能够相互看到?

适用代码:

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();
}



}

最佳答案

这是因为您在 if block 内声明变量 soundResource,然后尝试在 block 外访问它。

尝试这个模式:

// 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);

关于Eclipse 中的 Java 错误 - soundresource 无法解析为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24899723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com