F.johansson
F.johansson

Reputation: 93

Specific wav file failing to load while others load without errors

I'm working on a system to play, pause and stop music. I'm testing this out with 2 different wav files, one with a length of 45 seconds and other with a length of 3:35 minutes. The problem I'm having is that the 45 second wav file plays without any problem. The 3:35 minute wav file on the other hand, doesn't load. Is there a maximum time limit to wav files in java or is it possible the wav file is broken? It plays without any problem on windows app "groove music".

I've searched around on stack overflow but no one seemed to experience the same problem as I am, one wav file playing, the other one not.

Error code I'm getting:

javax.sound.sampled.LineUnavailableException: line with format PCM_FLOAT 44100.0 Hz, 32 bit, stereo, 8 bytes/frame,  not supported.

The method i use for playing the wav file.

public static void playAudio(String name) {
    try {
        System.out.println("NOTE: Playing audio");
        clip = AudioSystem.getClip();
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(
                Engine.class.getResourceAsStream("/Audio/" + name));
        clip.open(inputStream);
        clip.start();
    } catch(Exception e) {
        System.out.println("ERROR: Failed to load audio");
    }
}

Calling the method

Engine.playAudio("easy2.wav");

Picture of the wav files in the "src/Audio/" folder

enter image description here

Upvotes: 0

Views: 853

Answers (2)

Radiodef
Radiodef

Reputation: 37875

Given the error message, one thing you could try is after opening the AudioInputStream from the resource, do the following:

AudioFormat fmt = inputStream.getFormat();
fmt = new AudioFormat(fmt.getSampleRate(),
                      16,
                      fmt.getChannels(),
                      true,
                      true);
inputStream = AudioSystem.getAudioInputStream(fmt, inputStream);

This attempts to convert the stream away from a floating-point format, hopefully to something that the system is more likely to support. (Also see JavaDoc for AudioSystem.getAudioInputStream(AudioFormat, AudioInputStream).)

You can run the following code to find out what formats are likely available for playback:

Arrays.stream(AudioSystem.getSourceLineInfo(new Line.Info(SourceDataLine.class)))
      .filter(info -> info instanceof DataLine.Info)
      .map(info -> (DataLine.Info) info)
      .flatMap(info -> Arrays.stream(info.getFormats()))
      .forEach(System.out::println);

If the above method of converting the audio stream doesn't work, then probably your best bet is to just convert the file with some editor, such as Audacity. Java sound is unfortunately still pretty limited in what formats it supports by default. (The most reliable format is probably CDDA, which is 44100Hz, 16-bit signed LPCM.)

There may also be 3rd-party SPIs which support conversions from floating-point PCM.

Upvotes: 1

F.johansson
F.johansson

Reputation: 93

The problem I was facing originated in the wav file itself. Since a wav file can have one of several different formats, the one it was encoded with was not compatible with java. The solution was to change the bitrate and Hz of the wav file to match an encoding that java supported.

More about wav formats: https://en.wikipedia.org/wiki/WAV

Upvotes: 0

Related Questions