Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Convert a byte array to a .wav file

843802Aug 13 2009 — edited Aug 13 2009
I am trying to take a byte array and turn it into a .wav file. I have tried many different approaches, but none seem to work. Can anyone help?

This is where I declare totalByteArray, as you can see it is the exact byte array from and audio file, so it should produce results:
 while((numBytesRead = audioInputStream.read(totalByteArray)) != -1){
//....
}
Here is what I have tried so far:

BufferedOutputStream
BufferedOutputStream bos = null;
    try{
        //create an object of FileOutputStream
        FileOutputStream fos = new FileOutputStream(new File("BufferedStream.wav"));

        //create an object of BufferedOutputStream
        bos = new BufferedOutputStream(fos);


/*
* To write byte array to file use,
* public void write(byte[] b) method of BufferedOutputStream
* class.
*/
        System.out.println("Writing byte array to file");

        bos.write(totalByteArray);

        System.out.println("File written");
    }catch(FileNotFoundException fnfe){
        System.out.println("Specified file not found" + fnfe);
    }catch(IOException ioe){
        System.out.println("Error while writing file" + ioe);
    }
    finally{
        if(bos != null){
            try{

                //flush the BufferedOutputStream
                bos.flush();

                //close the BufferedOutputStream
                bos.close();

            }catch(Exception e){}
        }
    }
This produces a file, but when I try to open the file it says: "Windows Media Player cannot play the file. The Player might not support the file type or might not support the codec that was used to compress the file."

The byte array input stream
 try{
      DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
      SourceDataLine sourceDataLineTemp = (SourceDataLine)AudioSystem.getLine(dataLineInfo);                                 
      sourceDataLineTemp.open(audioFormat);
      sourceDataLineTemp.start();
      ByteArrayInputStream bais = new ByteArrayInputStream(totalByteArray);
      AudioInputStream audioInputStreamTemp = AudioSystem.getAudioInputStream(bais);
        File fileOut = new File("transmitted.wav");
        AudioFileFormat.Type fileType = AudioSystem.getAudioFileFormat(audioFile).getType();
        if (AudioSystem.isFileTypeSupported(fileType,
            audioInputStreamTemp)) {
            System.out.println("Trying to write");
            AudioSystem.write(audioInputStreamTemp, fileType, fileOut);
            System.out.println("Written successfully");
        }
      }catch(Exception e){
          System.out.println(e.getMessage());
      }
I can make a .wav file from an AudioInputStream, but this code throws an exception and says it can not get the AudioInputStream from bais.

This is somewhat urgent.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 10 2009
Added on Aug 13 2009
3 comments
7,106 views