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

时间:2021-12-29 19:44:56

whenever i try to play a .wav or .mp3 sound file USING ECLIPSE IDE i get this error, check the code(it's very simple why would i get errors?) :

每当我尝试播放.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();
}}

and this is the exception :

这是例外:

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)

so what's the problem?

所以有什么问题?

edit 1 :

编辑1:

i also tried to change the path to C:\Users\Rev3rse\workspace\Project1\src\someSound.wav

我也尝试将路径更改为C:\ Users \ Rev3rse \ workspace \ Project1 \ src \ someSound.wav

and i got a diffrent excpetion check it :

我有一个不同的激发检查它:

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 个解决方案

#1


0  

FileInputStream expects the file path to be partitioned with double slashes.So instead of giving the source path as C:\Users\Rev3rse\workspace\Project1\src\someSound.wav use double slashes for the partition.

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

HOPE IT WORKS !!!

希望它工作!!!

#2


0  

I would recommend creating the InputStream from a resource instead so you are not tied down to a path. Something like:

我建议从资源创建InputStream,这样你就不会被绑定到路径了。就像是:

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

That eliminates the path and creates one less headache for you the developer to worry about.

这消除了路径,并为开发人员担心的问题减少了一个问题。

#1


0  

FileInputStream expects the file path to be partitioned with double slashes.So instead of giving the source path as C:\Users\Rev3rse\workspace\Project1\src\someSound.wav use double slashes for the partition.

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

HOPE IT WORKS !!!

希望它工作!!!

#2


0  

I would recommend creating the InputStream from a resource instead so you are not tied down to a path. Something like:

我建议从资源创建InputStream,这样你就不会被绑定到路径了。就像是:

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

That eliminates the path and creates one less headache for you the developer to worry about.

这消除了路径,并为开发人员担心的问题减少了一个问题。