Been having a bit of trouble over the last few days trying to get this to work. But what I want is we have an application that sends raw data over the network. I then read this binary data in and want to save it to a wav(any audio) file. Might look at compression later.
So the problematic code:
byte[] allBytes = ...
InputStream b_in = new ByteArrayInputStream(allBytes);
try
{
AudioFormat format = new AudioFormat(8000f, 16, 1, true, true);
AudioInputStream stream = new AudioInputStream(b_in, format, allBytes.length);
//AudioInputStream stream = AudioSystem.getAudioInputStream(b_in);
Have tried to use the above statement as well but i get the exception: javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from stream
. So what I think is happening is that because my stream is raw audio data and does not have a wave header this is throwing an exception?
File newPath = new File(SystemConfiguration.getLatest().voiceNetworkPathDirectory + currentPhoneCall.fileName);
if (!AudioSystem.isFileTypeSupported(Type.WAVE, stream))
{
Logger.error("Audio System file type not supported");
}
AudioSystem.write(stream, Type.WAVE, newPath);
The file does successfully write but it is all static, Do I need to create a wave header on the output using the something like this. When I look at the outputted wav file in notepad and it does seem to have a header as it starts with 'RIFF'.
Do I need to add a fake header into my input stream? Should i just create my own output header and just save it with a binary writer?
length
argument in constructor ofAudioInputStream
should be a frame length value instead of data length.frame-length = data-length / channel / sample-size-in-byte
– LiuYan 刘研