java application中如何播放声音?我找到一段代码
try {
        // From file
        AudioInputStream stream = AudioSystem.getAudioInputStream(new File("audiofile"));
    
        // From URL
        stream = AudioSystem.getAudioInputStream(new URL("http://hostname/audiofile"));
    
        // At present, ALAW and ULAW encodings must be converted
        // to PCM_SIGNED before it can be played
        AudioFormat format = stream.getFormat();
        if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
            format = new AudioFormat(
                    AudioFormat.Encoding.PCM_SIGNED,
                    format.getSampleRate(),
                    format.getSampleSizeInBits()*2,
                    format.getChannels(),
                    format.getFrameSize()*2,
                    format.getFrameRate(),
                    true);        // big endian
            stream = AudioSystem.getAudioInputStream(format, stream);
        }
    
        // Create the clip
        DataLine.Info info = new DataLine.Info(
            Clip.class, stream.getFormat(), ((int)stream.getFrameLength()*format.getFrameSize()));
        Clip clip = (Clip) AudioSystem.getLine(info);
    
        // This method does not return until the audio file is completely loaded
        clip.open(stream);
    
        // Start playing
        clip.start();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    } catch (LineUnavailableException e) {
    } catch (UnsupportedAudioFileException e) {
    }
参见:http://www.exampledepot.com/egs/javax.sound.sampled/Load.html
把其中audiofile改成sound.wav,
http://hostname/audiofile改成file:\\E:\\sound.wav,但运行并没有播放声音
是什么问题呢?或者还有其他什么方法能实现?
谢谢!

解决方案 »

  1.   

    有exception吗,你这样全部catch又不打印,怎么调试
      

  2.   

    哦~~~
    异常信息如下:
    java.io.FileNotFoundException: Global.wav (系统找不到指定的文件。)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:106)
    at com.sun.media.sound.WaveFileReader.getAudioInputStream(WaveFileReader.java:205)
    at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:778)
    at Test.main(Test.java:19)但是..为什么呢?
      

  3.   

    我帮你把上面的程序改好了,但是在改的时候发现这段示例很bt,不建议楼主使用这套程序. public static void main(String[] args) {
    Thread td = new Thread() {
    public void run() {
    try {
    // From URL
    AudioInputStream stream = AudioSystem
    .getAudioInputStream(new File("C:\\TEMP\\a.wav"));
    // At present, ALAW and ULAW encodings must be converted
    // to PCM_SIGNED before it can be played
    AudioFormat format = stream.getFormat();
    if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
    format = new AudioFormat(
    AudioFormat.Encoding.PCM_SIGNED, format
    .getSampleRate(), format
    .getSampleSizeInBits() * 2, format
    .getChannels(),
    format.getFrameSize() * 2, format
    .getFrameRate(), true); // big
    // endian
    stream = AudioSystem
    .getAudioInputStream(format, stream);
    } // Create the clip
    DataLine.Info info = new DataLine.Info(Clip.class, stream
    .getFormat(),
    ((int) stream.getFrameLength() * format
    .getFrameSize()));
    Clip clip = (Clip) AudioSystem.getLine(info); // This method does not return until the audio file is
    // completely
    // loaded
    clip.open(stream); // Start playing
    clip.start();
    synchronized (this) {
    try {
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (LineUnavailableException e) {
    e.printStackTrace();
    } catch (UnsupportedAudioFileException e) {
    e.printStackTrace();
    }
    }
    };
    td.setDaemon(false);
    td.start();
    }
      

  4.   

    我把线程wait();了你要根据实际环境唤醒,主要是我没找到播放wav文件的线程.
      

  5.   

    在你的app中可以把
                  synchronized (this) {
                            try {
                                wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
    去掉
    下面的也去掉
    Thread td = new Thread() {
                public void run() {
    .....
           td.setDaemon(false);
            td.start();
        }这些信息我是为了调试加上的,你的程序如果有主线程的话,无需这些代码了
      

  6.   

    to sunyujia:
    异常?
    javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
    at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:786)
    at Test$1.run(Test.java:70)
      

  7.   

    这个你改的文件也是wav吗
    我试过了
    "C:\\TEMP\\a.wav"
      

  8.   

    不知道为什么,我换了个wav文件就可以了
    谢谢你啊~另,找到了一个更简洁的办法..
    try {
        InputStream in = new FileInputStream("E:\\a.wav");//流文件
        try {
         AudioStream as = new AudioStream(in);//创建AudioStream 对象
         AudioPlayer.player.start(as);//开始播放
         //AudioPlayer.player.stop(as);//停止播放,本例没有设置播放时间,歌曲结束自动停止
        } catch (IOException e){
         e.printStackTrace();
        }    } catch (FileNotFoundException e) {
        e.printStackTrace();
       }
      

  9.   

    上面那个简单的方法无法播放mp3转换成wav的文件,
    处理不了复杂的波形