Creating a WAV file from raw PCM data using the Android SDK

29,857

OK, I've got this figured out. This post was crucial in helping me: http://computermusicblog.com/blog/2008/08/29/reading-and-writing-wav-files-in-java

Basically, I used ByteArrayOutputStream to write the raw PCM data from AudioRecord, which then lets me get the byte array and its size when the process is done. I can then use that data in conjunction with the SampleRate, BitRate, and Stereo/Mono settings to create the WAV header as per the link above. The resulting file works perfectly!

Share:
29,857
user496854
Author by

user496854

Updated on July 09, 2022

Comments

  • user496854
    user496854 almost 2 years

    I'm trying to use the AudioRecord class to record a WAV file. The problem is that it only supplies the raw PCM data, and if I write it to a file, there is no header information, so it will not play in any media player. How can I create a WAV file from this raw data?

    Or alternatively, is there any other way to record sound in Android to a WAV file (or, alternatively MP3)?

    Oh, and I know that MediaRecorder can'y be used because it doesn't support either WAV or MP3 formats.

  • user496854
    user496854 over 13 years
    I definitely need the output file to be either a WAV (PCM encoded), or an MP3. The service where it's being uploaded to requires this. So, as far as I can see, I can't use MediaRecorder. I'm also
  • user496854
    user496854 over 13 years
    I definitely need the output file to be either a WAV (PCM encoded), or an MP3. The service where it's being uploaded to requires this. So, as far as I can see, I can't use MediaRecorder. I'm also quite bad at all the low-level stuff, so I don't know how good I'd be at creating the heade myself. Aren't there java packages that have this capability?
  • user496854
    user496854 over 13 years
    I actually found this: computermusicblog.com/blog/2008/08/29/… , which made it a lot easier to get this working. But I'm not sure how to get the size of the datat chunk for byte 40. I wrote the raw PCM data to a file, so would it just be the size of that file?
  • user496854
    user496854 about 12 years
    Why? it has no support for WAV or MP3 output
  • Shrikanth Kalluraya
    Shrikanth Kalluraya over 11 years
    can you show a sample code how you achieved this. I have a similar requirement
  • Scorpion
    Scorpion over 10 years
    Can you give me some idea about how you have used wavIo file in your code? There are main 2 methods, read and save. I am recording audio in android using AudioRecord class. And i am writing data byte by byte.. Can you please help as my audio is not getting recorded properly.
  • topher217
    topher217 about 3 years
    Link is broken. Do you still have access to this code or some example illustrating the solution?