开发者

why do i get this error when i play .wav sound file using eclipse IDE?

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?) :

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 :

i also tried to change the path to C:\Users\Rev3rse\workspace\Project1\src\som开发者_开发知识库eSound.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)


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.

HOPE IT WORKS !!!


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

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜