Byte j = (Byte) _toFileBuffer.get(i);
audioBytes[i] = j.byteValue();
}
ByteArrayInputStream byteInputArrayStream = null;
AudioInputStream audioInputStream = null;
try {
byteInputArrayStream = new ByteArrayInputStream(audioBytes);
audioInputStream = new AudioInputStream(byteInputArrayStream,
_playToFileFormat, audioBytes.length / _frameSizeInBytes);
File outFile = new File(_fileName);
StringTokenizer st = new StringTokenizer(_fileName, ".");
// Do error checking:
if (st.countTokens() != 2) {
throw new IOException("Error: Incorrect "
+ "file name format. " + "Format: filename.extension");
}
st.nextToken(); // Advance to the file extension.
String fileExtension = st.nextToken();
if (fileExtension.equalsIgnoreCase("au")) {
// Save the file.
AudioSystem.write(audioInputStream, AudioFileFormat.Type.AU,
outFile);
} else if (fileExtension.equalsIgnoreCase("aiff")) {
// Save the file.
AudioSystem.write(audioInputStream, AudioFileFormat.Type.AIFF,
outFile);
} else if (fileExtension.equalsIgnoreCase("wave")) {
// Save the file.
AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE,
outFile);
} else if (fileExtension.equalsIgnoreCase("wav")) {
// Save the file.
AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE,
outFile);
} else if (fileExtension.equalsIgnoreCase("aifc")) {
// Save the file.
AudioSystem.write(audioInputStream, AudioFileFormat.Type.AIFC,
outFile);
} else {
throw new IOException("Error saving "
+ "file: Unknown file format: " + fileExtension);
}
} catch (IOException e) {
throw new IOException("SoundPlayback: error saving" + " file: " + e);
} finally {
if (byteInputArrayStream != null) {
try {
byteInputArrayStream.close();
} catch (Throwable throwable) {
System.out.println("Ignoring failure to close stream "
+ "on " + audioBytes.length + " bytes of data.");
throwable.printStackTrace();
}
}
if (audioInputStream != null) {
try {
audioInputStream.close();
} catch (Throwable throwable) {
System.out.println("Ignoring failure to close stream "
+ "on " + audioBytes.length + " bytes of data.");
throwable.printStackTrace();
}