0

Even though the sun.audio API says that .wav is a supported file apparently the one that I had must not have been. a .aiff file is now working but not in this way I found a better way thats a little more complicated though.

String strFilename = "C:\\Documents and Settings\\gkehoe\\Network\\GIM\\Explode.aiff";
    File soundFile = new File(strFilename);

    AudioInputStream    audioInputStream = null;
    try
    {
        audioInputStream = AudioSystem.getAudioInputStream(soundFile);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    AudioFormat audioFormat = audioInputStream.getFormat();
    SourceDataLine  line = null;
    DataLine.Info   info = new DataLine.Info(SourceDataLine.class,
                                             audioFormat);
    try
    {
        line = (SourceDataLine) AudioSystem.getLine(info);

        /*
          The line is there, but it is not yet ready to
          receive audio data. We have to open the line.
        */
        line.open(audioFormat);
    }
    catch (LineUnavailableException e)
    {
        e.printStackTrace();
        System.exit(1);
    }
    catch (Exception e)
    {
        e.printStackTrace();
        System.exit(1);
    }
    line.start();

    int nBytesRead = 0;
    byte[]  abData = new byte[EXTERNAL_BUFFER_SIZE];
    while (nBytesRead != -1)
    {
        try
        {
            nBytesRead = audioInputStream.read(abData, 0, abData.length);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        if (nBytesRead >= 0)
        {
            int nBytesWritten = line.write(abData, 0, nBytesRead);
        }
    }
    line.drain();

    /*
      All data are played. We can close the shop.
    */
    line.close();
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Gage Kehoe
  • 25
  • 7

2 Answers2

0

According to source code it is not recognized as supported file format.

teodozjan
  • 913
  • 10
  • 35
0

Wav files are supported, but there are many variables, and some of them are not supported. For example, you might get an unrecognized format exception if the wav is encoded at 48000 instead of 44100, or at 24 or 32 bits instead of 16 bit encoding.

What exact error did you get?

What are the specs (properties) of the wav file?

It is possible to convert from one wav to a compatible wav using a tool such as Audacity. A format that I use for wav files has the following properties:

16-bit encoding
little endian
44100 sample rate
stereo

I didn't really look closely at the code example itself. I like this playback example.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41