Search code examples
javaarraysbase64audioinputstream

AudioInputStream to base64


I'm using some code to read some audio files from HTTP GET requests and mixing them up. I end up with an AudioInputStream. If I write it to a file (ais is the AudioInputStream) with the code bellow the file is created with success and if I try to read it javascript I can get the base64 of the file with no problems.

AudioSystem.write(
      ais
      ,AudioFileFormat.Type.WAVE
      ,new File("test.wav"));

But what I need to do is with the AudioInputStream extract the byte array (and convert it to base64 to send it to some other part of the code. I can't save the file to disk, I need to do this from the application memory


Solution

  • Managed to do it by writing it to a ByteArrayOutputStream instead and then converting the byte array to base64

    baos = new ByteArrayOutputStream();
    
    AudioSystem.write(
           ais
           ,AudioFileFormat.Type.WAVE
           ,baos);
    
    audioInByteArray = baos.toByteArray();