Lame Mp3 Encoder Compile for Android

Lame MP3 Encoder compile for Android

There used to be a good blog post on how to compile lame for Android, but for some reason it's been offline for a while and only available on some dodgy Chinese sites.

I'll just copy the content below:

porting compiling lame encoder to Android ARM arch using Android NDK

I was looking for a mp3 encoding application in Android Market,
and found very few, the reason I think Android doesn't support mp3
encoding is because mp3 is patented technology. Another reason is I
guess people prefer Java programming and Android SDK rather than
Android native development kit.

Nevertheless compiling libmp3lame library for Android using Android
NDK is very easy actually.

  1. download Android NDK(also you need Android SDK and Eclipse with ADT plugin) and create simple project.
  2. create directory called "jni" in your project's directory.
  3. download lame sources, extract, copy all sources from directory libmp3lame to jni directory. Also copy lame.h which is located in include directory of lame sources.
  4. create jni/Android.mk file. it should look like this:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)`
LOCAL_MODULE := mp3lame
LOCAL_SRC_FILES := bitstream.c fft.c id3tag.c mpglib_interface.c presets.c quantize.c reservoir.c tables.c util.c VbrTag.c
encoder.c gain_analysis.c lame.c newmdct.c psymodel.c
quantize_pvt.c set_get.c takehiro.c vbrquantize.c version.c
include $(BUILD_SHARED_LIBRARY)

  1. clean lame sources, remove what's left from GNU autotools, Makefile.am, Makefile.in, libmp3lame_vc8.vcproj, logoe.ico, depcomp, folders i386, vector.
  2. edit file jni/utils.h, and replace definition extern ieee754_float32_t fast_log2(ieee754_float32_t x);
    with this extern float fast_log2(float x);
  3. go to the root directory of your Android project and run $pathtoandroidndk/ndk-build and you're done, you'll have limp3lame.so compiled.

Also, the following question on SO might be of interest for you, as it deals with some issues after compiling lame for Android:

  • Invoking native functions of ported library

How to add Lame 3.99.5 to Android Studio using NDK?

After 3 days getting stuck with the Android Studio + Lame + NDK. I figure out how to do this:

Step 1:

Download NDK: http://developer.android.com/ndk/downloads/index.html

Download Lame library: http://lame.sourceforge.net/download.php

My Android Studio 1.5.1

My NDK: android-ndk-r10e (you need to set path in order to use it)

My Lame library: 3.99.5

Note: lame library after download may have format .gz instead of .tar.gz. In that case, feel free to change it to .tar.gz and extract it by using 7zip (two times).

Step 2:

For example, your Project is "AudioRecorder". Create a folder name "jni" in side it. AudioRecorder/jni.

Copy all the libmp3lame folder (inside lame 3.99.5) to jni (you should replace its name to lame-3.99.5_libmp3lame).

Copy lame.h to AudioRecorder/jni/libmp3lame.

Step 3:

Create the Android.mk in AudioRecorder/jni:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LAME_LIBMP3_DIR := lame-3.99.5_libmp3lame
LOCAL_LDLIBS := -llog
LOCAL_MODULE := mp3lame
LOCAL_CFLAGS += -ffast-math -I $(LAME_LIBMP3_DIR)
LOCAL_SRC_FILES := $(LAME_LIBMP3_DIR)/bitstream.c \
$(LAME_LIBMP3_DIR)/fft.c \
$(LAME_LIBMP3_DIR)/id3tag.c \
$(LAME_LIBMP3_DIR)/mpglib_interface.c \
$(LAME_LIBMP3_DIR)/presets.c \
$(LAME_LIBMP3_DIR)/quantize.c \
$(LAME_LIBMP3_DIR)/reservoir.c \
$(LAME_LIBMP3_DIR)/tables.c \
$(LAME_LIBMP3_DIR)/util.c \
$(LAME_LIBMP3_DIR)/VbrTag.c \
$(LAME_LIBMP3_DIR)/encoder.c \
$(LAME_LIBMP3_DIR)/gain_analysis.c \
$(LAME_LIBMP3_DIR)/lame.c \
$(LAME_LIBMP3_DIR)/newmdct.c \
$(LAME_LIBMP3_DIR)/psymodel.c \
$(LAME_LIBMP3_DIR)/quantize_pvt.c \
$(LAME_LIBMP3_DIR)/set_get.c \
$(LAME_LIBMP3_DIR)/takehiro.c \
$(LAME_LIBMP3_DIR)/vbrquantize.c \
$(LAME_LIBMP3_DIR)/version.c \
include $(BUILD_SHARED_LIBRARY)

Step 4:

Remove Makefile.am, Makefile.in, logoe.ico, depcomp, lame.rc and i386 directory.

Step 5:

Edit file jni/lame-3.99.5_libmp3lame/util.h, and replace definition

extern ieee754_float32_t fast_log2(ieee754_float32_t x);

with this

extern float fast_log2(float x);

Step 6:

Move out, press shift + right click on the folder AudioRecorder choose open command line window:

"ndk-build" (to run NDK build)

"ndk-build clean" (to clean all built of NDK)

Step 7:

You may have some error like this:

"Cannot recognize <lame.h>"

replace all "#include " to "#include "lame.h""

"incompatible implicit declaration of built-in function 'xyz'"

add and to any files which have the problems (try to add at the top of the file below the first comment).

"undefined reference to 'index'"

go to files "id3tag.c" and "machine.h" comment the "#define strchr index"

Step 8:

Create the Application.mk in AudioRecorder/jni:

APP_ABI := all

If there is not Application.mk with the "APP_ABI := all", NDK only build the "armeabi"
Add this file the application will build up:

"arm64-v8a"

"armeabi"

"armeabi-v7a"

"mips"

"mips64"

"x86"

"x86_64"

Step 9:

After successfully build NDK, now you should have 2 new folder

"AudioRecorder/libs"

"AudioRecorder/obj"

Create the "jniLibs" in "AudioRecord\app\src\main" and copy all folders in "AudioRecorder/libs" to it.

Hope it may help. :)

How to call lame executable from Android?

You can't call Windows executable files in Android, because they have different architecture.
It easy to understand, you can't run apk file on Windows if you don't have any converting or simulation softwares.

You can use extended libraries such as ffmpeg, lame to change bitrate of mp3 files. You can access this link to build shared library in Android:

http://developer.samsung.com/android/technical-docs/Porting-and-using-LAME-MP3-on-Android-with-JNI

Add some sound data to existing mp3 file Android,lame encoder

Let me try to answer your question:

So how to add some new audio data to existing mp3 file, or maybe there
is any options to encode old bytes + new bytes to new mp3 file to join
together ?

if by "add some audio" you mean append or prepend the mp3 with other audio data, the answer is YES, you can do it without reencode

The class you will use is MediaMuxer
you can find usefull info at https://developer.android.com/reference/android/media/MediaMuxer

https://bigflake.com/mediacodec/



Related Topics



Leave a reply



Submit