如何用java播放.wav文件

gre*_*ard 48 java audio wav

我正在尝试用Java播放*.wav文件.我希望它执行以下操作:
按下按钮时,播放一声短促的哔声.

我用谷歌搜索了它,但大部分代码都没有用.有人可以给我一个简单的代码片段来播放.wav文件吗?

gre*_*ard 41

最后我设法做了以下工作,并且工作正常

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class MakeSound {

    private final int BUFFER_SIZE = 128000;
    private File soundFile;
    private AudioInputStream audioStream;
    private AudioFormat audioFormat;
    private SourceDataLine sourceLine;

    /**
     * @param filename the name of the file that is going to be played
     */
    public void playSound(String filename){

        String strFilename = filename;

        try {
            soundFile = new File(strFilename);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        try {
            audioStream = AudioSystem.getAudioInputStream(soundFile);
        } catch (Exception e){
            e.printStackTrace();
            System.exit(1);
        }

        audioFormat = audioStream.getFormat();

        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
        try {
            sourceLine = (SourceDataLine) AudioSystem.getLine(info);
            sourceLine.open(audioFormat);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        sourceLine.start();

        int nBytesRead = 0;
        byte[] abData = new byte[BUFFER_SIZE];
        while (nBytesRead != -1) {
            try {
                nBytesRead = audioStream.read(abData, 0, abData.length);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (nBytesRead >= 0) {
                @SuppressWarnings("unused")
                int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
            }
        }

        sourceLine.drain();
        sourceLine.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这个对我不起作用.我推荐一个非常简单的方法:[http://alvinalexander.com/java/java-audio-example-java-au-play-sound ](http://alvinalexander.com/java/java-audio-example-java -au播放声音) (12认同)
  • 说真的,如果这是在Java中播放wav声音所需要的,那么我将停止使用Java,幸运的是我怀疑这是正确的. (9认同)

tsc*_*wab 35

这是我可以在不使用太阳的情况下出现的最优雅的形式.*:

import java.io.*;
import javax.sound.sampled.*;

try {
    File yourFile;
    AudioInputStream stream;
    AudioFormat format;
    DataLine.Info info;
    Clip clip;

    stream = AudioSystem.getAudioInputStream(yourFile);
    format = stream.getFormat();
    info = new DataLine.Info(Clip.class, format);
    clip = (Clip) AudioSystem.getLine(info);
    clip.open(stream);
    clip.start();
}
catch (Exception e) {
    //whatevers
}
Run Code Online (Sandbox Code Playgroud)

  • 要听到输出,您应该延迟应用程序终止一段播放时间.尝试在`clip.start()之后放置`Thread.sleep(1000)`. (4认同)

min*_*-me 16

最短的形式(无需安装随机库)?

public static void play(String filename)
{
    try
    {
        Clip clip = AudioSystem.getClip();
        clip.open(AudioSystem.getAudioInputStream(new File(filename)));
        clip.start();
    }
    catch (Exception exc)
    {
        exc.printStackTrace(System.out);
    }
}
Run Code Online (Sandbox Code Playgroud)

唯一的问题是没有好办法让这个方法阻止关闭并在*.wav完成后处理数据. clip.drain()说这是封锁,但事实并非如此.剪辑没有正确 运行start().我找到的唯一可行但非常简单的方法是:

// ...
clip.start();
while (!clip.isRunning())
    Thread.sleep(10);
while (clip.isRunning())
    Thread.sleep(10);
clip.close();
Run Code Online (Sandbox Code Playgroud)

  • clip有一个`addLineListener`所以你不需要使用`while`循环... (8认同)

mkd*_*dev 13

您可以使用事件侦听器在播放后关闭剪辑

import java.io.File;
import javax.sound.sampled.*;

public void play(File file) 
{
    try
    {
        final Clip clip = (Clip)AudioSystem.getLine(new Line.Info(Clip.class));

        clip.addLineListener(new LineListener()
        {
            @Override
            public void update(LineEvent event)
            {
                if (event.getType() == LineEvent.Type.STOP)
                    clip.close();
            }
        });

        clip.open(AudioSystem.getAudioInputStream(file));
        clip.start();
    }
    catch (Exception exc)
    {
        exc.printStackTrace(System.out);
    }
}
Run Code Online (Sandbox Code Playgroud)