Search code examples
javaandroidaudio-recording

saving raw audio data to mp3 or a similar format


In my android app, am trying to split stereo audio input that I get from AudioRecord class, into two mono channels. Basically I need to work separately with the audio inputs from the two mics, and hence the splitting. I do this by picking alternate samples(2 bytes at a time) of the returned data from audioRecord.read method, and saving it in 2 separate DOUBLE type arrays - ArrayL and ArrayR. Now I need to save these 2 arrays in such a way as to be able to play it as mono recorded channels, say in .mp3 format. Is there a simple way to achieve this conversion from double array to any popular audio format and save it on external storage as L and R channel recordings? Please help out! Thanks in advance, Neetha


Solution

  • steps:

    1. Use read(byte[] audioData, int offsetInBytes, int sizeInBytes) to get audio into a byte array.
    2. Separate two channels by copying every other byte into two different byte arrays.
    3. Save two byte arrays as a mp3
    4. Play any of them.

    for step 3,4

    String path = "path to new mono mp3 file";
    File mp3File = new File(path);
    FileOutputStream fos = new FileOutputStream(mp3File) ;
    fos.write(byteArray);
    fos.close();
    
    mediaPlayer = MediaPlayer.create(getApplicationContext(),Uri.fromFile(mp3File));
    mediaPlayer.start();