I have five wav files. I want to play them serially from a single Java program using sourceDataLine. But my program is not maintaining the proper sequence. Can anyone provide me code segment?
Asked
Active
Viewed 632 times
-1
-
7No. Show us **your** code. – Matt Ball Jan 19 '12 at 05:12
-
For better help sooner, post an [SSCCE](http://sscce.org/). Though make it for 2 sound samples, not 5. – Andrew Thompson Jan 19 '12 at 05:39
1 Answers
3
Did you check the Documentation?
try this example from here:
import java.io.*;
import javax.sound.sampled.*;
/**
* Use SourceDataLine to read line-by-line from the external sound file.
*/
public class SoundLineTest {
public static void main(String[] args) {
SourceDataLine soundLine = null;
int BUFFER_SIZE = 64*1024; // 64 KB
// Set up an audio input stream piped from the sound file.
try {
File soundFile = new File("gameover.wav");
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundFile);
AudioFormat audioFormat = audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
soundLine = (SourceDataLine) AudioSystem.getLine(info);
soundLine.open(audioFormat);
soundLine.start();
int nBytesRead = 0;
byte[] sampledData = new byte[BUFFER_SIZE];
while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(sampledData, 0, sampledData.length);
if (nBytesRead >= 0) {
// Writes audio data to the mixer via this source data line.
soundLine.write(sampledData, 0, nBytesRead);
}
}
} catch (UnsupportedAudioFileException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (LineUnavailableException ex) {
ex.printStackTrace();
} finally {
soundLine.drain();
soundLine.close();
}
}
}

CloudyMarble
- 36,908
- 70
- 97
- 130
-
1
-
-
+1 for the answer. "Edit following soon" would have discouraged me to comment. – Scorpion Jan 19 '12 at 06:32