21

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.

RzR
  • 3,068
  • 29
  • 26
user496854
  • 6,461
  • 10
  • 47
  • 84

3 Answers3

13

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!

user496854
  • 6,461
  • 10
  • 47
  • 84
  • 1
    can you show a sample code how you achieved this. I have a similar requirement – Shrikanth Kalluraya Oct 17 '12 at 01:12
  • 1
    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. – Scorpion Sep 13 '13 at 10:08
  • Link is broken. Do you still have access to this code or some example illustrating the solution? – topher217 Mar 29 '21 at 05:52
0

Check the MediaRecorder.setOutputFormat(), you can set different container formats for your recording; there is MediaRecorder.OutputFormat.MPEG_4 and MediaRecorder.OutputFormat.THREE_GPP; the only allowed format along RAW is setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

Sorry but MP3 is not avail. You really need mp3 for recording? WAV on the opposite of MP3 is a container, not a format; WAV can be any kind of encoding format.

You are always free to prepend some WAV RIFF header in front of your raw pcm data (as long as you exactly know the format). Check here for how it has to look like: http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html

Oliver
  • 1,269
  • 13
  • 21
  • 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 Jan 24 '11 at 03:09
  • 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 Jan 24 '11 at 03:14
  • I actually found this: http://computermusicblog.com/blog/2008/08/29/reading-and-writing-wav-files-in-java , 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 Jan 24 '11 at 07:01
-3

You may want to use mediarecord class

zhoubo
  • 39
  • 8