Use MP3 format to record sound on Android

Source: Internet
Author: User

0. Download lame and Unzip

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

Http://sourceforge.net/projects/lame/files/lame/3.99/lame-3.99.5.tar.gz

1. Create the JNI/LIBMP3LAME/SRC directory, and the uncompressed lame-3.99.5/libmp3lame/directory will be

BITSTREAM.C  fft.h            l3side.h                         presets.c   quantize_pvt.c  set_get.h   util.h         Version.cbitstream.h  gain_analysis.c  lame-analysis.h      machine.h           psymodel.c  quantize_pvt.h  tables.c    vbrquantize.c  version.hencoder.c    gain_analysis.h  lame.c               mpglib_ interface.c  psymodel.h  reservoir.c     tables.h    vbrquantize.hencoder.h    id3tag.c         Lame _global_flags.h  newmdct.c           quantize.c  reservoir.h     takehiro.c  vbrtag.cfft.c        Id3tag.h                     newmdct.h           quantize.h  set_get.c       util.c      VbrTag.h

Basically all the. c and. h files, except that lameerror.h can be used without

Copy all of the files to Jni/libmp3lame/src

Copy the lame.h from the lame-3.99.5/include/directory to JNI/LIBMP3LAME/SRC

2. Create Jni/android.mk

Root_path: = $ (call my-dir) # do Speex and Lamelocal_path: = $ (call My-dir) include $ (root_path)/libmp3lame/android.mk

Jni/libmp3lame/android.mk

Local_path: = $ (call My-dir) include $ (clear_vars) Lame_libmp3_dir: = Srclocal_module    : = mp3lamelocal_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 Li_zhiwei_mp3recorder_simplelame.cinclude $ (BUILD_SHARED_LIBRARY)

Jni/application.mk

App_abi: = Armeabi armeabi-v7aapp_platform: = android-8 app_platform: = android-9 App_stl: = stlport_static stlport_force_ REBUILD: = True

3. Modify the source code
Fft.c removed from the
#include "Vector/lame_intrin.h"

This is optimized with Intel CPU features, which are not available on arm

Lieutenant General Set_get.h
#include
Change into
#include "lame.h"

Lieutenant General Utils.h
extern ieee754_float32_t fast_log2 (ieee754_float32_t x);
Use
extern float fast_log2 (float x);
Replace

4. Creating an Encapsulated file
Li_zhiwei_mp3recorder_simplelame.c

#include "src/lame.h" #include "li_zhiwei_mp3recorder_simplelame.h" static lame_global_flags *GLF = NULL; Jniexport void Jnicall java_li_zhiwei_mp3recorder_simplelame_init (jnienv *env, Jclass cls, Jint insamplerate, Jint Outchannel,jint outsamplerate, Jint outbitrate, jint quality) {if (GLF! = NULL) {lame_close (GLF); GLF = NULL;} GLF = Lame_init (); Lame_set_in_samplerate (GLF, insamplerate); Lame_set_num_channels (GLF, outchannel); lame_set_out_ Samplerate (GLF, outsamplerate); Lame_set_brate (GLF, outbitrate); Lame_set_quality (GLF, quality); Lame_init_params ( GLF);} Jniexport jint jnicall Java_li_zhiwei_mp3recorder_simplelame_encode (jnienv *env, Jclass cls, JshortArray buffer_l, Jshortarray buffer_r,jint samples, Jbytearray mp3buf) {jshort* j_buffer_l = (*env)->getshortarrayelements (env, buffer_l, NULL); jshort* j_buffer_r = (*env)->getshortarrayelements (env, buffer_r, NULL); const JSIZE Mp3buf_size = (* ENV)->getarraylength (env, mp3buf); jbyte* j_mp3buf = (*env)->getbytearrayelements (env, mp3bUF, NULL); int result = Lame_encode_buffer (GLF, j_buffer_l, J_buffer_r,samples, J_mp3buf, mp3buf_size);(*env) Releaseshortarrayelements (env, buffer_l, j_buffer_l, 0);(*env)->releaseshortarrayelements (env, Buffer_r, j_ Buffer_r, 0);(*env)->releasebytearrayelements (env, MP3BUF, j_mp3buf, 0); return result; Jniexport jint jnicall Java_li_zhiwei_mp3recorder_simplelame_flush (jnienv *env, Jclass cls, JbyteArray mp3buf) {const Jsize mp3buf_size = (*env)->getarraylength (env, mp3buf); jbyte* j_mp3buf = (*env)->getbytearrayelements (env, Mp3buf, NULL); int result = Lame_encode_flush (GLF, J_mp3buf, mp3buf_size);(*env)->releasebytearrayelements (env, Mp3buf, j_mp3buf, 0); return result;} Jniexport void Jnicall java_li_zhiwei_mp3recorder_simplelame_close (jnienv *env, Jclass cls) {lame_close (GLF); GLF = NULL ;}

Li_zhiwei_mp3recorder_simplelame.h (this file is actually generated in the Javah li.zhiwei.mp3recorder.SimpleLame command)

/* Don't EDIT this file-it are machine generated */#include/* Header for Class Li_zhiwei_mp3recorder_simplelame */#ifnd EF _included_li_zhiwei_mp3recorder_simplelame#define _included_li_zhiwei_mp3recorder_simplelame#ifdef __ Cplusplusextern "C" {#endif/* * class:li_zhiwei_mp3recorder_simplelame * method:close * Signature: () V */jniexpor T void Jnicall java_li_zhiwei_mp3recorder_simplelame_close (jnienv *, jclass);/* * Class:li_zhiwei_mp3recorder_simpl Elame * Method:encode * Signature: ([s[si[b) I */jniexport jint jnicall Java_li_zhiwei_mp3recorder_simplelame_encode (    JNIEnv *, Jclass, Jshortarray, Jshortarray, Jint, Jbytearray);/* * Class:li_zhiwei_mp3recorder_simplelame * Method: Flush * Signature: ([B) I */jniexport jint jnicall java_li_zhiwei_mp3recorder_simplelame_flush (jnienv *, Jclass, Jbytea Rray);/* * Class:li_zhiwei_mp3recorder_simplelame * method:init * Signature: (IIIII) V */jniexport void Jnicall Ja  Va_li_zhiwei_mp3recorder_simplelame_init(JNIEnv *, Jclass, Jint, Jint, Jint, Jint, jint); #ifdef __cplusplus} #endif #endif 

6. Run Ndk_build to

Resources

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

Https://github.com/talzeus/AndroidMp3Recorder

http://ikinglai.blog.51cto.com/6220785/1228730

Use MP3 format to record sound on Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.