BigClip的问题

时间:2014-11-13 00:49:26

标签: java url audio nullpointerexception malformedurlexception

我一直在尝试在我的应用中播放音乐。我一直在使用示例BigClip代码:

try {
        url = new URL(Sounds.class.getResourceAsStream("title1.wav").toString());
        } catch (MalformedURLException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        BigClip clip = new BigClip();
        AudioInputStream ais = null;
        try {
            ais = AudioSystem.getAudioInputStream(url);
        } catch (UnsupportedAudioFileException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            clip.open(ais);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (LineUnavailableException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        clip.start();
        JOptionPane.showMessageDialog(null, "BigClip.start()");
        clip.loop(4);
        JOptionPane.showMessageDialog(null, "BigClip.loop(4)");
        clip.setFastForward(true);
        clip.loop(8);
        // the looping/FF combo. reveals a bug..
        // there is a slight 'click' in the sound that should not be audible
        JOptionPane.showMessageDialog(null, "Are you on speed?");
}

当我只使用title1.wav时,我收到此错误:

java.net.MalformedURLException: no protocol: java.io.BufferedInputStream

当我添加协议file://时,我得到NullPointerException,但我无法看到可能导致该问题的原因。

我使用了错误的协议,还是我做错了什么?提前谢谢!

1 个答案:

答案 0 :(得分:0)

假设您的文件与Sounds类位于同一个包("目录")中,请使用

url = Sounds.class.getResource("title1.wav");

,因为

new URL(Sounds.class.getResourceAsStream("title1.wav").toString())

刚刚开始工作。您正在InputStream的实例上调用toString

NPE可能是因为AudioSystem.getAudioInputStream因网址路径错误而失败,因此ais为空且BigClipopen引发NPE。