当我使用eclipse IDE播放.wav声音文件时,为什么会出现此错误?

时间:2011-07-16 04:42:43

标签: java eclipse audio

每当我尝试播放.wav或.mp3声音文件时,我会得到ECLIPSE IDE 这个错误,请检查代码(这很简单,为什么我会收到错误?):

import sun.audio.*;
import java.io.*;
public class Sound {
 String Filename = "someSound.wav";
    InputStream in;
    AudioStream as;

public Sound() {
    try {
        in = new FileInputStream(Filename);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        as = new AudioStream(in);
    } catch (IOException e) {
        e.printStackTrace();
    }
    AudioPlayer.player.start(as);
    AudioPlayer.player.stop(as);

}

static public void main(String[] args) {
    Sound s = new Sound();
}}

这是例外:

java.io.FileNotFoundException: someSound.wav (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at Sound.<init>(Sound.java:14)
at Sound.main(Sound.java:10)
Exception in thread "main" java.lang.NullPointerException
    at sun.audio.AudioStream.<init>(AudioStream.java:65)
    at Sound.<init>(Sound.java:19)
    at Sound.main(Sound.java:10)

那么问题是什么?

编辑1:

我也试图改变路径 C:\ Users \用户Rev3rse \工作空间\ PROJECT1 \ SRC \ someSound.wav

我得到了不同的激励检查:

java.io.IOException: could not create audio stream from input stream
at sun.audio.AudioStream.<init>(AudioStream.java:82)
at Sound.<init>(Sound.java:16)
at Sound.main(Sound.java:26)

2 个答案:

答案 0 :(得分:0)

FileInputStream期望使用双斜杠对文件路径进行分区。因此,不要将源路径指定为C:\ Users \ Rev3rse \ workspace \ Project1 \ src \ someSound.wav,请为分区使用双斜杠。

希望它工作!!!

答案 1 :(得分:0)

我建议您从资源创建InputStream,这样您就不会被绑定到路径。类似的东西:

in = this.getClass().getResourceAsStream("someSound.wav");

这样可以消除路径,让开发人员担心的问题少了一些。