0

我正在尝试播放 .wav 文件,但由于某种原因,我总是收到找不到错误的错误。下面是我的代码:

package Program;

public class YuanVideoGameStore {

    //VARIABLES:
    static final String BACKGRUONDMUSIC = "/YuanVideoGameStore/src/reasources/b2.wav";

    public static void main(String[] args) {

        PlayMusic play = new PlayMusic();
        play.BackGroundSound(BACKGRUONDMUSIC);

    }

}

    package Program;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JOptionPane;

public class PlayMusic {

    static Clip clip;

    void BackGroundSound(String musicLocation) {

        try {
            File musicPath = new File(musicLocation);

            if(musicPath.exists()) {
                AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);

                clip = AudioSystem.getClip();
                clip.open(audioInput);
                clip.start();
                clip.loop(Clip.LOOP_CONTINUOUSLY);

                JOptionPane.showMessageDialog(null, "Please don't press the button");
            }else {System.out.print("Cannot find file!");}
        }catch(Exception ex) {ex.printStackTrace();}
    }
}

我不得不使用相关路径调用该文件,因为该程序将交给我的老师。而且我已经尝试了很多次,但音乐文件仍然无法播放。请帮忙。

4

0 回答 0