Android NDK引用Lame动态链接库

LAME is a high quality MPEG Audio Layer III (MP3) encoder

Lame:http://lame.sourceforge.net/

native接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Mp3Lame {

public native static void close();

public native static int encode(short[] buffer_l, short[] buffer_r, int samples, byte[] mp3buf);

public native static int flush(byte[] mp3buf);

public native static void init(int inSampleRate, int outChannel, int outSampleRate, int outBitrate, int quality);

public static void init(int inSampleRate, int outChannel, int outSampleRate, int outBitrate) {
init(inSampleRate, outChannel, outSampleRate, outBitrate, 7);
}
}

C层实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "../common.h"
#include "lame.h"

static lame_global_flags *glf = NULL;

extern "C"
void Java_me_yezhou_libc_audio_Mp3Lame_close(JNIEnv *env, jclass clazz) {
lame_close(glf);
glf = NULL;
}

extern "C"
jint Java_me_yezhou_libc_audio_Mp3Lame_encode(JNIEnv *env, jclass clazz, jshortArray data_left,
jshortArray data_right, jint samples,
jbyteArray data) {
jshort *buffer_l = env->GetShortArrayElements(data_left, NULL);
jshort *buffer_r = env->GetShortArrayElements(data_right, NULL);
jbyte *mp3buf = env->GetByteArrayElements(data, NULL);

const jsize mp3buf_size = env->GetArrayLength(data);

int result = lame_encode_buffer(glf, buffer_l, buffer_r, samples, (u_char *) mp3buf,
mp3buf_size);

env->ReleaseShortArrayElements(data_left, buffer_l, 0);
env->ReleaseShortArrayElements(data_right, buffer_r, 0);
env->ReleaseByteArrayElements(data, mp3buf, 0);

return result;
}

extern "C"
jint Java_me_yezhou_libc_audio_Mp3Lame_flush(JNIEnv *env, jclass clazz, jbyteArray mp3data) {
jbyte *mp3buf = env->GetByteArrayElements(mp3data, NULL);

const jsize mp3buf_size = env->GetArrayLength(mp3data);

int result = lame_encode_flush(glf, (u_char *) mp3buf, mp3buf_size);

env->ReleaseByteArrayElements(mp3data, mp3buf, 0);

return result;
}

extern "C"
void Java_me_yezhou_libc_audio_Mp3Lame_init(JNIEnv *env, jclass clazz, jint inSampleRate,
jint outChannel,
jint outSampleRate, jint outBitrate,
jint quality) {
LOGI("me_yezhou_libc_audio_Mp3Lame_init");
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);

//编码方式: cbr abr vbr
lame_set_VBR(glf, vbr_mtrh);
lame_set_VBR_quality(glf, 5);

lame_set_quality(glf, quality);

lame_init_params(glf);
}

Android.mk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := mp3lame
LOCAL_SRC_FILES := $(LOCAL_PATH)/../libs/$(TARGET_ARCH_ABI)/libmp3lame.so #兼容平台
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := audio
LOCAL_C_INCLUDES := . \
$(LOCAL_PATH)/../
LOCAL_SRC_FILES := mp3lame.cpp
LOCAL_SHARED_LIBRARIES := mp3lame
LOCAL_LDLIBS := -llog \
-landroid

LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)

include $(BUILD_SHARED_LIBRARY)
#include $(BUILD_STATIC_LIBRARY)

so库引用

1
2
3
4
static {
System.loadLibrary("mp3lame");
System.loadLibrary("audio");
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :