1

I'm fairly new to working with audio files in Java and fairly new to Android (I know some basics).

I have two .aac files recorded using MediaRecorder and I needed to merge the two after a recording has taken place. I figured I'd attempt it in Java first and then move it over to Android since a lot looks pretty similar?

I was wondering what is the best way to achieve this? So far I've converted the .aac files to byte arrays and merging these however this only produces the first file (from the first byte array) with a bigger file size and no extra audio.

I thought perhaps this is due to audio headers? Should I find a way to target these bytes and remove them from the second (if so is there a documentation explaining where the header is?)

Alternatively I've also read about using .wavs and instead of using MediaRecorder use AudioRecord, create two PCM's, concatenate and then convert to .wav? Would I be able to then convert that .wav to .aac?

What is the fastest way of implementing this? Does anyone have any extra documentation that could help?

Malii
  • 448
  • 6
  • 17
  • In general taking 2 files as a byte array and appending one to the other is not going to make a valid file. You're lucky that even the original data was still playable. To do this you're going to have to actually read up on the AAC format and learn how to manipulate it. Or find a library which does so. – Gabe Sechan Jan 16 '17 at 16:40
  • I really wasn't expecting much from mashing the two together, but I read somewhere about them being raw audio files and got a funny idea or two. Thank you. – Malii Jan 16 '17 at 16:45
  • wav is actually raw audio once you get past the headers. Of course the wav format is screwy with hundreds of variants. But if you had the unheadered raw wav data you could combine those two and re-encode as aac. – Gabe Sechan Jan 16 '17 at 17:02
  • Thank you, you sent me off down the right course. I ended up using AudioRecord and have got the files concatenated. I've just got to fiddle with the WAV header's to get the sound to sound normal (it sounds a bit robotty and sped up at the moment). – Malii Jan 18 '17 at 09:19

1 Answers1

1

Thanks to Gabe, sending me down the right course.

In the end I ditched working with .aac's and attempting to remove headers. I used AudioRecord to record .pcm files, then concatenated these and saved as a .wav.

I did struggle with the sound sounding fast forwarded and distorted and initially thought it was down to the sample rate, it turns out I was converting between short to byte which was losing information, thus speeding up and the reason for the distortion.

Hope this helps someone, I might end up adding some .aac encoding library to reduce the size.

Some links I found helpful on my journey.

https://github.com/newventuresoftware/WaveformControl

Android AudioRecord example

How to convert .pcm file to .wav or .mp3?

http://www.topherlee.com/software/pcm-tut-wavformat.html

Community
  • 1
  • 1
Malii
  • 448
  • 6
  • 17