getAudioInputStream 的格式无效,尝试在 Java 中播放声音

Invalid format with getAudioInputStream, trying to play a sound in Java

我正在尝试在 Java 中播放基本声音...

这是我的代码,基于在各种论坛上找到的代码:

    Clip clip = null;

    try {
        clip = AudioSystem.getClip();
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(url));
        clip.open(inputStream);
    } catch (LineUnavailableException | IOException | UnsupportedAudioFileException ex) {
        Logger.getLogger(Pomodoro.class.getName()).log(Level.SEVERE, null, ex);
    }

    return clip;

我检查过:new File(url).exists(); returns 是的,我检查过文件类型确实是音频 WAV (audio/x-wav),我检查过另一个文件问题仍然存在。 .我不明白我做错了什么。

错误:

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: 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)

求助!

您可以使用下面的代码在Java中播放声音。

   try {
        DataLine.Info daInfo = new DataLine.Info(Clip.class, null);
        try {
            URL sounURL = ClassName.class.getResource("/com/notification.wav");

            AudioInputStream inputStream = AudioSystem.getAudioInputStream(sounURL);
            DataLine.Info info = new DataLine.Info(Clip.class, inputStream.getFormat());
            Clip clip = (Clip) AudioSystem.getLine(info);
            clip.open(inputStream);
            clip.start();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        } catch (UnsupportedAudioFileException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    } catch (Exception e) {
        System.out.println("music");
    }

我终于找到了我的问题的副本。对不起,打扰了。

AudioInputStream is not working

我的代码变成了:

    Clip clip = null;

    try {
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(url));
        DataLine.Info info = new DataLine.Info(Clip.class, inputStream.getFormat());
        clip = (Clip)AudioSystem.getLine(info);
        clip.open(inputStream);
    } catch (LineUnavailableException | IOException | UnsupportedAudioFileException ex) {
        Logger.getLogger(Pomodoro.class.getName()).log(Level.SEVERE, null, ex);
    }

    return clip;

尽管如此,令我感到非常惊讶的是我以前的代码不起作用。有时,我很绝望 Java...