AudioInputStream不起作用

时间:2014-01-15 03:51:35

标签: 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.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)

以下是代码:

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

2 个答案:

答案 0 :(得分:11)

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

答案 1 :(得分:0)

您传递的文件路径有问题。当我使用相同的代码从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();
            }
        });
    }
}