简体   繁体   中英

play wav files using java

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 ); 

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.

EDIT

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM