0

I am attempting to add an audio player to my application. Here's the code from the class that handles audio playing:

package me.pogostick29.audiorpg.audio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;

public class AudioPlayer {

    private AudioPlayer() { }

    private static AudioPlayer instance = new AudioPlayer();

    public static AudioPlayer getInstance() {
        return instance;
    }

    private static ArrayList<BigClip> clips = new ArrayList<BigClip>();

    private BigClip get(String name) {
        for (BigClip clip : clips) {
            if (clip.getName().equalsIgnoreCase(name)) return clip;
        }
        return null;
    }

    public void play(File file) {
        try {
            AudioInputStream audioIn = AudioSystem.getAudioInputStream(file);
            BigClip clip = get(file.getName());
            if (clip == null) {
                BigClip newClip = new BigClip(file.getName());
                clips.add(newClip);
                clip = newClip;
            }
            clip.open(audioIn);
            clip.start();
        }
        catch (Exception e) { e.printStackTrace(); }
    }
}

However, when I try to run it using:

AudioPlayer.getInstance().play(new File("audio/people/blacksmith/blacksmith01a_new.wav"));

I get the following stack trace:

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
    at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1170)
    at me.pogostick29.audiorpg.audio.AudioPlayer.play(AudioPlayer.java:38)
    at me.pogostick29.audiorpg.person.people.Blacksmith.playDialogue(Blacksmith.java:12)
    at me.pogostick29.audiorpg.AudioRPG.main(AudioRPG.java:33)

Here's the format I used when exporting the file with Audition.

1
  • Do this code and report the result. File f = new File("audio/people/blacksmith/blacksmith01a_new.wav"); System.out.println("File: " + f.exists()); Apr 10, 2013 at 0:13

1 Answer 1

1

Nevermind, I fixed it by using this library: http://www.javazoom.net/javalayer/javalayer.html

3
  • Please don't just post some tool or library as an answer. At least demonstrate how it solves the problem in the answer itself. Apr 11, 2019 at 16:03
  • This question is 6 years old. Leave it alone.
    – nrubin29
    Apr 12, 2019 at 17:33
  • Why should I leave it alone? If it is bad, it should go, if it is good, then leave it. This answer is bad and needs to go. Apr 12, 2019 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.