This article will be based on the following three questions.

1. What is Lame?

2. Why did you use Lame?

3. How do I use Lame on Android?

What is Lame

We see a Lame website (Lame. Sourceforge. IO/index. PHP) to describe

   LAME is a high quality MPEG Audio Layer III (MP3) encoder licensed under the LGPL.

LAME is a high quality MPEG audio layer III (MP3) encoder licensed under LGPL.

Ok, so we now know that Lame is an open source encoder that is specifically used to encode MP3 files.

Why do you use Lame

For those of you who have developed recording on Android, there are two ways to implement recording on Android. One is to use the way of AudioRecord, AudioRecord records directly is the original PCM data, the other way is to use MediaRecord, can record files similar to AMR, AAC format.

Here post MediaRecord currently support the recording format, you can see that mp3 is not currently supported. So when we want to encode mp3 files, we need to use LAME.

    public final class AudioEncoder {
      /* Do not change these values without updating their counterparts
       * in include/media/mediarecorder.h!
       */
        private AudioEncoder() {}
        public static final int DEFAULT = 0;
        /** AMR (Narrowband) audio codec */
        public static final int AMR_NB = 1;
        /** AMR (Wideband) audio codec */
        public static final int AMR_WB = 2;
        /** AAC Low Complexity (AAC-LC) audio codec */
        public static final int AAC = 3;
        /** High Efficiency AAC (HE-AAC) audio codec */
        public static final int HE_AAC = 4;
        /** Enhanced Low Delay AAC (AAC-ELD) audio codec */
        public static final int AAC_ELD = 5;
        /** Ogg Vorbis audio codec */
        public static final int VORBIS = 6;
        /** @hide EVRC audio codec */
        public static final int EVRC = 10;
        /** @hide QCELP audio codec */
        public static final int QCELP = 11;
        /** @hide Linear PCM audio codec */
        public static final int LPCM = 12;
    }
Copy the code

How do I use Lame on Android

Integration of open source code on Android, most of the time the routine is similar, is to download the source code, compile the relevant SO library, and then is to call the method provided by these libraries, of course, call the method of so library, the application needs to use JNI to achieve.

I won’t show you how to download lame and code the so library. Compiling lame gives us a libmp3lame.so library file, which calls the methods in the so library and requires us to implement the JNI code.

The idea is that the application layer encodes mp3 files by calling native methods provided by JNI and then calling methods in libmp3lame.so.

An example given here is encoding a PCM file into an MP3 file.

Git address: github.com/yorkZJC/And… Here is the code logic:

LameEncodeJniNative. Java is a native method encapsulated locally, providing two methods for the application layer, one is encoding, one is ending. These two methods correspond to the c++ layer specific implementation, you can see the project CPP/directory file implementation, CPP file implementation is relatively simple.

When the application code is called, it passes in the corresponding PCM source file and the path of the MP3 file to be generated, as well as the format information of the PCM source file (adoption rate, number of channels, encoded data bits).

   File pcmFile = new File(getExternalFilesDir(null), "input.pcm");
   File mp3File = new File(getExternalFilesDir(null), "output.mp3");
   mLameEncoder = new LameEncodeJniNative();
   mLameEncoder.encode(pcmFile.getAbsolutePath(), mp3File.getAbsolutePath(), 44100, 2, 128);
Copy the code

I have been engaged in Android Camera related development for 5 years

Now I work in Shenzhen

Welcome to follow my wechat official account “Xiaochi Notes”.

We learn and communicate together