尝试播放声音文件时出现错误

时间:2018-08-03 14:56:43

标签: audio media clip

我有以下代码是从程序包playound中复制的,因为我想问你是什么问题:

public class Playsound extends JFrame {

    public Playsound() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Test Sound Clip");
        this.setSize(300, 200);
        this.setVisible(true);

        try {
            URL url = this.getClass().getClassLoader().getResource("waterdrop.wav");
            AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
            Clip clip = AudioSystem.getClip();
            clip.open(audioIn);
            clip.start();
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Playsound();
    }
}

但是当我尝试运行它时,我得到了:

Exception in thread "main" java.lang.NullPointerException
    at com.sun.media.sound.StandardMidiFileReader.getSequence(StandardMidiFileReader.java:207)
    at javax.sound.midi.MidiSystem.getSequence(MidiSystem.java:841)
    at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(SoftMidiAudioFileReader.java:178)
    at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1147)
    at playsound.Playsound.<init>(Playsound.java:18)
    at playsound.Playsound.main(Playsound.java:32)

它死在这一行:

 AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);

我在Image文件夹中有此设置

所以我想问一下为什么它不起作用?也许是我,也许不是,但它不起作用,也不是很好。

0 个答案:

没有答案