本文介绍了getAudioInputStream 格式无效,尝试在 Java 中播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to just play a basic sound in Java...

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

Here is my code, based on the code found on various forums :

    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(); 返回true,我检查过文件类型确实是音频WAV (audio/x-wav),我检查过问题坚持使用另一个文件...我不明白我做错了什么.

I checked that : new File(url).exists(); returns true, I checked that the file type is really audio WAV (audio/x-wav), I checked that the problem persists with another file... I don't understand what I'm doing wrong.

错误:

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)

帮助!

推荐答案

我终于找到了我的问题的副本.抱歉打扰了.

I finally found a duplicate of my issue. Sorry I bothered you.

AudioInputStream 不工作

我的代码变成了:

    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 感到非常绝望...

Nevertheless, I'm very surprised that my previous code didn't work. Sometimes, I'm quite desperate by Java...

这篇关于getAudioInputStream 格式无效,尝试在 Java 中播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:56