AudioInputStream is not working

╄→гoц情女王★ 提交于 2019-11-27 08:18:06

问题


I'm trying to play a .wav sound every time the user presses a button, but an exception gets thrown:

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)

Here's the code:

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

回答1:


I managed to get it working. This is the code that I used. Keep in mind that I needed this just to play a short beep.wav sound. It seems to have some trouble with longer sound files. Let me know if it works for you guys and if you manage to play longer sounds with this code.

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



回答2:


It's something wrong with the file path you are passing. When I use your same code getting the file from a JFileChooser it works fine. Test this out.

Also see the Javasound wiki tag for working with unsupported audio file types

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


来源:https://stackoverflow.com/questions/21128797/audioinputstream-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!