【问题标题】:AudioInputStream is not working音频输入流不工作
【发布时间】:2014-02-03 09:54:33
【问题描述】:

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

线程“Thread-0”java.lang.IllegalArgumentException 中的异常:格式无效
    在 org.classpath.icedtea.pulseaudio.PulseAudioDataLine.createStream(PulseAudioDataLine.java:142)
    在 org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:99)
    在 org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:283)
    在 org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:402)
    在 org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:453)
    在 Uber.play(Uber.java:534)
    在优步 $5.​​run(Uber.java:340)
    在 java.lang.Thread.run(Thread.java:724)

代码如下:

//Play Audio File
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();
}

【问题讨论】:

    标签: java audio javasound javax.sound.sampled


    【解决方案1】:

    我设法让它工作。 这是我使用的代码。请记住,我需要这个只是为了播放一个简短的 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();
            }
        }
    

    【讨论】:

    • 当你将文件的路径传递给函数play("path/to/file");确保这是正确的路径,我之所以这么说是因为当你将所有东西打包到一个罐子里时,它会变得非常棘手。确保在项目的根文件夹中创建一个资源文件夹。
    【解决方案2】:

    您传递的文件路径有问题。当我使用相同的代码从JFileChooser 获取文件时,它工作正常。测试一下。

    另请参阅Javasound wiki tag,了解如何处理不受支持的音频文件类型

    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();
                }
            });
        }
    }
    

    【讨论】:

    • 嘿@peeskillet,谢谢你的回复。但是如果文件的路径不对,是不是会抛出“没有这样的文件或目录”的异常呢?
    • 顺便说一句,上面的代码给了我同样的例外。我开始认为文件本身有问题。很奇怪,因为我尝试了 .mp3、.aiff 和 .wav 都没有运气。
    • @Aci89 似乎你是对的。我有一个FileNotFoundException。我对音频没有太多经验。我只是在测试你的方法。我不确定是什么引发了IllegalArgumentException。我能够产生的唯一其他异常是UnsupportedAudioFileType 异常
    • @Aci89 是的,我不确定如何获取那些受支持的文件类型。我唯一能玩的就是midau。我无法使用mp3
    • @Aci89 可能Sound trial 可能会给你一些答案。另请查看我在上面的答案中的链接。
    猜你喜欢
    • 1970-01-01
    • 2022-07-31
    • 2021-11-08
    • 2018-07-18
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    相关资源
    最近更新 更多