1、下NDK并配置环境变量
2、配置JNI,具体参考http://blog.csdn.net/shimiso/article/details/43447785
3、配置自己的运行.h和.c文件以及util文件
#include "lame.h"
#include "com_eachpal_audio_MyLameUtil.h"
#include
#include
static lame_global_flags *lame = NULL;
JNIEXPORT void JNICALL Java_com_eachpal_audio_MyLameUtil_init(
JNIEnv *env, jclass cls, jint inSamplerate, jint inChannel, jint outSamplerate, jint outBitrate, jint quality) {
if (lame != NULL) {
lame_close(lame);
lame = NULL;
}
lame = lame_init();
lame_set_in_samplerate(lame, inSamplerate);
lame_set_num_channels(lame, inChannel);//输入流的声道
lame_set_out_samplerate(lame, outSamplerate);
lame_set_brate(lame, outBitrate);
lame_set_quality(lame, quality);
lame_init_params(lame);
}
JNIEXPORT jint JNICALL Java_com_eachpal_audio_MyLameUtil_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(lame, 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_com_eachpal_audio_MyLameUtil_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(lame, j_mp3buf, mp3buf_size);
(*env)->ReleaseByteArrayElements(env, mp3buf, j_mp3buf, 0);
return result;
}
JNIEXPORT void JNICALL Java_com_eachpal_audio_MyLameUtil_close
(JNIEnv *env, jclass cls) {
lame_close(lame);
lame = NULL;
}
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class com_eachpal_audio_MyLameUtil */
#ifndef _Included_com_eachpal_audio_MyLameUtil
#define _Included_com_eachpal_audio_MyLameUtil
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_eachpal_audio_MyLameUtil
* Method: init
* Signature: (IIIII)V
*/
JNIEXPORT void JNICALL Java_com_eachpal_audio_MyLameUtil_init
(JNIEnv *, jclass, jint, jint, jint, jint, jint);
/*
* Class: com_eachpal_audio_MyLameUtil
* Method: encode
* Signature: ([S[SI[B)I
*/
JNIEXPORT jint JNICALL Java_com_eachpal_audio_MyLameUtil_encode
(JNIEnv *, jclass, jshortArray, jshortArray, jint, jbyteArray);
/*
* Class: com_eachpal_audio_MyLameUtil
* Method: flush
* Signature: ([B)I
*/
JNIEXPORT jint JNICALL Java_com_eachpal_audio_MyLameUtil_flush
(JNIEnv *, jclass, jbyteArray);
/*
* Class: com_eachpal_audio_MyLameUtil
* Method: close
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_eachpal_audio_MyLameUtil_close
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
package com.eachpal.audio;
/**
* Created by eachpal on 2016/7/20.
*/
public class MyLameUtil {
static{
System.loadLibrary("mp3lame");
}
/**
* Initialize LAME.
*
* @param inSamplerate
* input sample rate in Hz.
* @param inChannel
* number of channels in input stream.
* @param outSamplerate
* output sample rate in Hz.
* @param outBitrate
* brate compression ratio in KHz.
* @param quality
* quality=0..9. 0=best (very slow). 9=worst.
* recommended:
* 2 near-best quality, not too slow
* 5 good quality, fast
* 7 ok quality, really fast
*/
public native static void init(int inSamplerate, int inChannel,
int outSamplerate, int outBitrate, int quality);
/**
* Encode buffer to mp3.
*
* @param bufferLeft
* PCM data for left channel.
* @param bufferRight
* PCM data for right channel.
* @param samples
* number of samples per channel.
* @param mp3buf
* result encoded MP3 stream. You must specified
* "7200 + (1.25 * buffer_l.length)" length array.
* @return number of bytes output in mp3buf. Can be 0.
* -1: mp3buf was too small
* -2: malloc() problem
* -3: lame_init_params() not called
* -4: psycho acoustic problems
*/
public native static int encode(short[] bufferLeft, short[] bufferRight,
int samples, byte[] mp3buf);
/**
* Flush LAME buffer.
*
* REQUIRED:
* lame_encode_flush will flush the intenal PCM buffers, padding with
* 0's to make sure the final frame is complete, and then flush
* the internal MP3 buffers, and thus may return a
* final few mp3 frames. 'mp3buf' should be at least 7200 bytes long
* to hold all possible emitted data.
*
* will also write id3v1 tags (if any) into the bitstream
*
* return code = number of bytes output to mp3buf. Can be 0
* @param mp3buf
* result encoded MP3 stream. You must specified at least 7200
* bytes.
* @return number of bytes output to mp3buf. Can be 0.
*/
public native static int flush(byte[] mp3buf);
/**
* Close LAME.
*/
public native static void close();
}
4、android.mk后面加上自己的.c文件名
5、jni目录运行cmd命令:ndk-build生成so文件
6、删除多余的armeabi文件夹
问题处理:
1、未添加自己的.h和.c会找不到util文件里的方法
2、不删除多余的armeabi会报错:UnsatisfiedLinkError