Superhacker
Superhacker

Reputation: 11

Unable to play wav data using FileInputStream and Clip

I'm trying to play .wav files using AudioInputStream and clip methods. My code is as follows:

fc = new JFileChooser();
fc.setCurrentDirectory(new File(System.getProperty("user.home")));
int result = fc.showOpenDialog(add);
try {
            InputStream in = new FileInputStream(fc.getSelectedFile().getAbsolutePath();
            System.out.println(fc.getSelectedFile().getAbsolutePath());
            try {
                AudioInputStream as = AudioSystem.getAudioInputStream(in);
                try {
                    Clip clip = AudioSystem.getClip();
                    clip.open(as);
                    Thread.sleep(500);
                    clip.start();
                } catch (LineUnavailableException e) {
                    e.printStackTrace();
                }
            } catch (UnsupportedAudioFileException | IOException e) {
                System.out.println(e.getMessage());
            }
} catch (FileNotFoundException e) {

}       `

When selecting a .wav file i get following error message:

mark/reset not supported

Any Ideas how i can fix that? First time working with audio files.

Upvotes: 0

Views: 71

Answers (1)

Urbs
Urbs

Reputation: 131

I would recommend using the JavaFX MediaPlayer which is way easier to use and supports more codecs.

Upvotes: 0

Related Questions