Play wav files using java

I am trying to play javafx usinf wav files on my raspbery pi using the java sound library and code below, I get an error message following

javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian not supported. 

After googleing, I found that

the big-endian audio format is not supported by the raspberry pi sound card driver, and I need to change the getAudioFormat () function to request the little-endian format:

 boolean bigEndian = false; 

ok so far i realized i need the following

 private AudioFormat getAudioFormat() { float sampleRate = 8000.0F; int sampleInbits = 16; int channels = 1; boolean signed = true; boolean bigEndian = false; return new AudioFormat(sampleRate, sampleInbits, channels, signed, bigEndian); } 

but where I call getAudioFormat() from the following code.

 URL url = this.getClass().getClassLoader().getResource("Audio/athan1.wav"); Clip clip = AudioSystem.getClip(); AudioInputStream ais = AudioSystem.getAudioInputStream( url ); 
+4
source share
1 answer

Look at the method signatures in the AudioSystem ( API ). There is a getAudioInputStream(AudioFormat targetFormat, AudioInputStream sourceStream) method getAudioInputStream(AudioFormat targetFormat, AudioInputStream sourceStream) .

Once you get AudioFormat by calling your overridden getAudioFormat() (or, cf. below), you should be able to (quote from the API):

Get the [...] audio input stream of the specified format, by converting the provided audio input stream.

Alternatively, to override getAudioFormat() (because what happens if you want to play other file types in the future?), See the first snippet in the Conflicting Jar Methods question, which seems to do exactly what you want, without having to override the method, It is also an example of audio stream conversion using the above method.

EDIT

Try it.

 URL url = this.getClass().getClassLoader().getResource("Audio/athan1.wav"); AudioInputStream ais = AudioSystem.getAudioInputStream(url); AudioFormat littleEndianFormat = getAudioFormat(); AudioInputStream converted = AudioSystem.getAudioInputStream(littleEndianFormat, ais); 
+3
source

Source: https://habr.com/ru/post/1502035/


All Articles