![](/img/trans.png)
[英]Error: Driver cannot be resolved to a variable- selenium java eclipse
[英]Java error in Eclipse - soundresource cannot be resolved to a variable
我正在嘗試將音樂文件導入到我的程序中,但也允許它在啟動時在其中兩個中隨機選擇。 我使用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
塊中聲明變量soundResource
,然后嘗試在該塊外訪問它。
試試這個模式:
// 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);
該變量在if塊中聲明,因此僅在if范圍內。 因此,您以后不能在代碼中引用它。 將聲明移至if之前進行修復。
URL soundResource;
if(RandomSongNum){
soundResource = this.getClass().getClassLoader().getResource("BH-Lightest.wav");
} else {
soundResource = this.getClass().getClassLoader().getResource("RY-Lightest.wav");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.