Java AudioInputStream不工作

Java AudioInputStream不工作,java,audio,javasound,javax.sound.sampled,Java,Audio,Javasound,Javax.sound.sampled,我试图在用户每次按下按钮时播放.wav声音,但会引发异常: Exception in thread "Thread-0" java.lang.IllegalArgumentException: Invalid format at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.createStream(PulseAudioDataLine.java:142) at org.classpath.icedtea.pulseaud

我试图在用户每次按下按钮时播放.wav声音,但会引发异常:

Exception in thread "Thread-0" java.lang.IllegalArgumentException: Invalid format at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.createStream(PulseAudioDataLine.java:142) at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:99) at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:283) at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:402) at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:453) at Uber.play(Uber.java:534) at Uber$5.run(Uber.java:340) at java.lang.Thread.run(Thread.java:724)
您正在传递的文件路径有问题。当我使用相同的代码从
JFileChooser
获取文件时,效果很好。测试一下这个

另请参阅,以了解如何使用不支持的音频文件类型

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.SwingUtilities;

public class TestAudio {

    public TestAudio() {

        JButton button = new JButton("Choose file");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();

                File file = null;
                int returnVal = chooser.showOpenDialog(null);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    file = chooser.getSelectedFile();
                }

                String fileName = file.getAbsolutePath();
                try {
                    play(fileName);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        JFrame frame = new JFrame();
        frame.add(button);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    public void play(String file) throws LineUnavailableException, UnsupportedAudioFileException, IOException {
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(file));
        Clip clip = AudioSystem.getClip();
        clip.open(inputStream);
        clip.start();
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TestAudio();
            }
        });
    }
}

我设法使它工作起来。 这是我使用的代码。请记住,我需要它只是为了播放一个简短的beep.wav声音。它似乎在处理较长的声音文件时遇到了一些问题。让我知道它是否适用于你们,如果你们能用这段代码播放更长的声音

public void play(String file) throws LineUnavailableException, UnsupportedAudioFileException, IOException
    {

    try 
        {   
            AudioInputStream inputStream = AudioSystem.getAudioInputStream(this.getClass().getResource(file));
            AudioFormat format = inputStream.getFormat();
            DataLine.Info info = new DataLine.Info(Clip.class, format);
            Clip clip = (Clip)AudioSystem.getLine(info);
            clip.open(inputStream);
            clip.start();
        }

    catch (IOException | LineUnavailableException | UnsupportedAudioFileException e1)
        {
            e1.printStackTrace();
        }
    }

嘿@peeskillet,谢谢你的回复。但是如果文件的路径是错误的,这不会引发“没有这样的文件或目录”异常吗?顺便说一句,上面的代码给了我同样的异常。我开始觉得文件本身有问题。自从我尝试了.mp3、.aiff和.wav之后,这一切都很奇怪。@Aci89似乎你是对的。我得到了一个
FileNotFoundException
。我没有多少音频方面的经验。我只是在测试你的方法。我不确定是什么引发了
IllegalArgumentException
。我唯一能产生的另一个例外是
不支持的daudiofiletype
exception@Aci89是的,我不知道如何获得这些支持的文件类型。我唯一能让它玩的是
mid
au
。我无法让它与mp3相配。我可能会给你一些答案。还可以看看我在上面的答案中的链接;确保这是正确的路径,我之所以这么说是因为当你把所有东西都打包到一个罐子里的时候会变得非常棘手。确保在项目的根文件夹中创建资源文件夹。
public void play(String file) throws LineUnavailableException, UnsupportedAudioFileException, IOException
    {

    try 
        {   
            AudioInputStream inputStream = AudioSystem.getAudioInputStream(this.getClass().getResource(file));
            AudioFormat format = inputStream.getFormat();
            DataLine.Info info = new DataLine.Info(Clip.class, format);
            Clip clip = (Clip)AudioSystem.getLine(info);
            clip.open(inputStream);
            clip.start();
        }

    catch (IOException | LineUnavailableException | UnsupportedAudioFileException e1)
        {
            e1.printStackTrace();
        }
    }