Tuesday, March 15, 2022

[SOLVED] Playing audio files from a JAR-application does not work on Linux

Issue

I need some help with my Java application. Its purpose is to read a certain website, so I need to play many audio files in a row. The JAR is compiled using Java 8. I tested my application with Windows 11 and Java 16.0.1, everything works fine. Then I used the latest Ubuntu Linux and Java 11.0.13 as well as Java 8: It plays some audio, but not every file.

I wrote a test class and the result was, that - no matter in which order I play the audio - only the first (exactly!) 62 files are played. Every next file (even the ones, that were successfully played at first) produces the exception my code throws at this position:

if (mixerSelected != null) {
    audioClip0 = AudioSystem.getClip(mixerSelected);
} else {
    throw new IllegalArgumentException("File is not compatible: '" + audioFilePath + "'.");
}

I ensured that every audio file is .WAV with

  • 8k sample rate,
  • 16k Bytes per second in average,
  • 16 Bits, and
  • pcm_s16le codec.

My application is built as JAR-file including my audio files in the resources directory.

This is my code:

public class PlayAudio {

    /**
     * plays an audio file
     *
     * @param audioFilePath String: path to the audio file
     * @param speed double: speed applied to the audios
     */
    public boolean singleFile(String audioFilePath, double speed) {

        //audioFilePath = "audio" + File.separator + audioFilePath;
        audioFilePath = "audio" + "/" + audioFilePath;

        AudioInputStream audioStream0;

        //create new file using path to the audio
        try {
            //load files from resources folder as stream
            ClassLoader classLoader = getClass().getClassLoader();
            InputStream inputStream = classLoader.getResourceAsStream(audioFilePath);
            InputStream bufferedInputStream = new BufferedInputStream(inputStream);

            if (bufferedInputStream == null) {
                throw new IllegalArgumentException("File not found: '" + audioFilePath + "'.");
            } else {
                //create new AudioStream
                audioStream0 = AudioSystem.getAudioInputStream(bufferedInputStream);
            }
        } catch (IllegalArgumentException e) {
            //handle
            return false;
        } catch (IOException e) {
            //handle
            return false;
        } catch (UnsupportedAudioFileException e) {
            //handle
            return false;
        }

        try {
            //create new AudioFormat
            AudioFormat audioFormat0 = audioStream0.getFormat();

            //create new Info
            DataLine.Info info0 = new DataLine.Info(Clip.class, audioFormat0);

            //initialize new Mixer
            Mixer.Info mixerSelected = null;

            for (Mixer.Info mixerInfo : AudioSystem.getMixerInfo()) {
                Mixer mixer = AudioSystem.getMixer(mixerInfo);

                if (mixer.isLineSupported(info0)) {
                    mixerSelected = mixerInfo;
                    break;
                }
            }

            //create new Clip
            Clip audioClip0;

            if (mixerSelected != null) {
                audioClip0 = AudioSystem.getClip(mixerSelected);
            } else {
                //THIS EXCEPTION GETS THROWN!!!
                throw new IllegalArgumentException("File is not compatible: '" + audioFilePath + "'.");
            }

            //open created Clips via created AudioStream
            audioClip0.open(audioStream0);

            //start the play of audio file
            audioClip0.start();

            //wait until play completed
            double waitTime = (double)((((double)audioClip0.getMicrosecondLength()/1000.0)/speed + 50.0) * 0.8);
            Thread.sleep((long)waitTime);

            return true;

        //handle exceptions
        } catch (LineUnavailableException e) {
            //handle
            return false;
        } catch (IOException e) {
            //handle
            return false;
        } catch (InterruptedException e) {
            //handle
            return false;
        } catch (IllegalArgumentException e) {
            //THIS EXCEPTION GETS THROWN!!!
            //handle invalid audio clips
            System.out.println(e);
            e.printStackTrace();
            return false;
        }
    }



    /**
     * plays multiple audio files in the order they are stored in an ArrayList
     *
     * @param fileNames ArrayList<String>: list with filenames of audio files to play
     * @param speaker String: speaker to use for playing the audios (can be 'm' or 'w')
     * @param speed double: speed applied to the audios
     * @return boolean: true if playing audios completed successfully, otherwise false
     */
    public static boolean multiFiles(ArrayList<String> fileNames, String speaker, double speed) {

        PlayAudio player = new PlayAudio();

        //play every audio file in the array of file names
        for (int i = 0; (i < fileNames.toArray().length); i ++) {
            //generate file names
            String fullFileName = speaker + "_" + fileNames.toArray()[i];

            //play audio
            player.singleFile(fullFileName, speed);
        }

        return true;
    }
}

What did I already try?

  1. I tried it on another computer that runs Ubuntu Linux as well.
  2. I created a new instance of PlayAudio() everytime a new audio is played.
  3. I used audioClip0.stop(); after every audio.
  4. I increased the milliseconds of sleep after every audio to length of the audio plus 1 second.
  5. I rebuilt the projects ... nearly 1k times.

How can I reproduce the error?

I simply need to play more than 62 audio files running my JAR-file under Linux Ubuntu.

How can you help me?

I don't know how to handle this issue. What is the problem playing .WAV-files with Linux? Is there a common way to fix this? (I am not allowed to use any library except OracleJDK and OpenJDK.)


Solution

The #1 suggestion is by Mark Rotteveel. The AudioInputStream class needs closing. This is often a surprise for people, because Java is well known for managing garbage collection. But for AudioInputStream there are resources that need to be released. The API doesn't do an adequate job of pointing this out, imho, but the need for handling can be inferred from the description for the AudioInputStream.close() method:

Closes this audio input stream and releases any system resources associated with the stream.

The #2 suggestion is from both Andrew Thompson and Hendrik may be more a helpful hint than a direct solution, but it is still a very good idea, and it seems plausible to me that the inefficiency of all the additional, unneeded infrastructure (ClassLoader, InputStream, BufferedInputStream) might be contributing to the issue. But I really don't have a good enough understanding of the underlying code to know how pertinent that is.

However, I think you can do even better. Don't use Clip. You current use of Clip goes against the concept of its design. Clips are meant for short duration sounds that are to be held in memory and played multiple times, not files that are repeatedly reloaded before each playback. The proper class for this sort of use (load and play) is the SourceDataLine.

An example of playback using a SourceDataLine can be found in the javasound wiki. This example also illustrates the use of URL for obtaining the necessary AudioInputStream. I will quote it here verbatim.

public class ExampleSourceDataLine {

    public static void main(String[] args) throws Exception {
        
        // Name string uses relative addressing, assumes the resource is  
        // located in "audio" child folder of folder holding this class.
        URL url = ExampleSourceDataLine.class.getResource("audio/371535__robinhood76__06934-distant-ship-horn.wav");
        // The wav file named above was obtained from https://freesound.org/people/Robinhood76/sounds/371535/ 
        // and matches the audioFormat.
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
        
        AudioFormat audioFormat = new AudioFormat(
                Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);
        Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
        SourceDataLine sourceDataLine = (SourceDataLine)AudioSystem.getLine(info);
        sourceDataLine.open(audioFormat);
        
        int bytesRead = 0;
        byte[] buffer = new byte[1024];
        sourceDataLine.start();
        while((bytesRead = audioInputStream.read(buffer)) != -1)
        {
            // It is possible at this point manipulate the data in buffer[].
            // The write operation blocks while the system plays the sound.
            sourceDataLine.write(buffer, 0, bytesRead);                                 
        }   
        sourceDataLine.drain();
        // release resources
        sourceDataLine.close();
        audioInputStream.close();
    }
}

You will have to do some editing, as the example was set up to run via a main method. Also, you'll be using your audio format, and that the names of the audio files with their folder locations will have to match the relative or absolute location specified in the argument you use in getResource() method. Also, a larger size for the buffer array might be preferred. (I often use 8192).

But most importantly, notice that in this example, we close both the SourceDataLine and the AudioInputStream. The alternate suggestion to use try-with-resources is a good one and will also release the resources.

If there are difficulties altering the above to fit into your program, I'm sure if you show us what you try, we can help with making it work.



Answered By - Phil Freihofner
Answer Checked By - Mildred Charles (WPSolving Admin)