Reputation: 2443
I am trying to play wav files usinf javafx on my raspbery pi, using the java sound library and the code below, i get an error as follow
javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian not supported.
After googleing around I found that
big-endian audio format is not supported by the raspberry pi soundcard driver, and that i need to change the getAudioFormat() function to request a little-endian format:
boolean bigEndian = false;
ok so far i figured that 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 do 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 );
Upvotes: 4
Views: 3323
Reputation: 4185
Have a look at the method signatures in AudioSystem
(API). There's a method getAudioInputStream(AudioFormat targetFormat, AudioInputStream sourceStream)
.
Once you have obtained the AudioFormat
by calling your overridden getAudioFormat()
(or, cf. below), you should be able to (quote from the API):
Obtain[...] an audio input stream of the indicated format, by converting the provided audio input stream.
Alternatively to overriding getAudioFormat()
(because what happens if you want to play other filetypes in the future?), have a look at the first snippet in the question Conflicting Jar Methods, which seems to do exactly what you want without having to override the method, and is also an example of converting audio streams with the above method.
Try this.
URL url = this.getClass().getClassLoader().getResource("Audio/athan1.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
AudioFormat littleEndianFormat = getAudioFormat();
AudioInputStream converted = AudioSystem.getAudioInputStream(littleEndianFormat, ais);
Upvotes: 3