How we Encode our Audio and Video File in MP3 Format(Container). | Techbirds

Using LAME MP3 on Android with JNI

About Lame

LAME is the one of most popular lossy audio codecs. It allows you to compress audio files to MPEG-1/MPEG-2 format, which is stored in MP3 files. Due to this codec popularity, one may be interested in implementing MP3 in native form through the Java Native Interface for an Android application

Environment Setup:

To Use JNI we have to use Android NDK to Run the Native Code. You Can Download NDK from: https://developer.android.com/sdk/ndk/index.html

After unpacking it, there will be an ndk-build script in the main directory
How we Prepare Lame Library:

The LAME library can be downloaded from the following website:

https://lame.sourceforge.net/download.php

⦁ Create a directory with the name JNI in the project. ⦁ Copy LibMp3lame from downloaded archive to JNI directory.

⦁ copy lame.h from the include folder of the downloaded archive.

Compilation with NDK

Now create Makefile for NDK and include the LAME source files. The file should be named Android.mk and placed in the jni directory.
It should look like this

LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := libmp3lame LOCAL_SRC_FILES := \ ./libmp3lame/bitstream.c \ ./libmp3lame/encoder.c \

./libmp3lame/fft.c \ …………………………

LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)

⦁ The shared library can now be built. Go to jni directory and call ndk-build script from there.

Now Create Wrapper

⦁ Create a wrapper which provide interface for the future android app. ⦁ Create wrapper.c file in jni directory and include it in Android.mk file in LOCAL_SRC_FILES variable: ⦁ When the wrapper is finished, rebuild the shared library with ndk-build. The output should look like Compile thumb : mp3lame libs/armeabi/libmp3lame.so

Now Create an Android application

⦁ Since a functional wrapper for the encoder has been made, it can be used in Java code
⦁ The next step is to load the native library. Do it in a static block, so it is performed only once. Like this…

static { System.loadLibrary(“mp3lame”); } private native void initEncoder(int numChannels, int sampleRate, int bitRate, int mode, int quality); private native void destroyEncoder();

private native int encodeFile(String sourcePath, String targetPath);

⦁ Now they can be used as normal.
Note : This is just a knowlege sharing of how can we use Lame library in your android project. Those will use this Lame shouls have basic Knowlege of NDK and JNI.

3,696 total views, 1 views today

Share this Onfacebook-9950766twitter-1343042linkedin-7109093google-4566969