我試圖實現對Android應用有點扯,但我不斷收到錯誤:沒有實現發現本地UnsatisfiedLinkError異常的Android JNI
W/dalvikvm(2472): No implementation found for native Lcom/example/android/audio/util/LameWrapper;.init (IIIII)V
java.lang.UnsatisfiedLinkError: init
at com.example.android.audio.util.LameWrapper.init(Native Method)
at com.example.android.audio.util.LameWrapper.init(LameWrapper.java:22)
at com.example.android.audio.util.Recorder$1.run(Recorder.java:51)
我跟着步驟答案(Lame MP3 Encoder compile for Android)。我能夠生成libmp3lame.so文件。它被放在裏面/庫/ armeabi
我還創建Android.mk文件:
LOCAL_PATH := $(call my-dir)
LOCAL_C_INCLUDES := $(LOCAL_PATH)
include $(CLEAR_VARS)
LAME_LIBMP3_DIR := lame-3.99.5_libmp3lame
LOCAL_MODULE := mp3lame
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)
我用-javah創建com_example_android_audio_LameWrapper.h文件。我用.H創建com_example_android_audio_util_LameWrapper.c:
JNIEXPORT void JNICALL Java_com_example_android_audio_util_LameWrapper_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);
}
實際的Java包裝LameWrapper.java:
public class LameWrapper {
public static void init(int inSamplerate, int outChannel,
int outSamplerate, int outBitrate) {
init(inSamplerate, outChannel, outSamplerate, outBitrate, 7);
}
public native static void init(int inSamplerate, int outChannel,
int outSamplerate, int outBitrate, int quality);
我創建了一個 「Recorder.java」 對象在加載mp3lame庫。記錄器對象有一個「啓動」方法,它調用LameWrapper的函數。
public class Recorder {
static {
System.loadLibrary("mp3lame");
}
public void start(){
//starts a thread, sets some values
LameWrapper.init(RECORDER_SAMPLERATE, 2, RECORDER_SAMPLERATE, 32);
}
然後我調用MainActivity中的Recorder對象。
我已經搜索了很多堆棧溢出並找不到答案。通常對於「未發現實現錯誤」,文件被錯誤地命名。但是,我很確定我正確地命名了這些包。
難道有人指着我正確的方向嗎?任何幫助表示讚賞。謝謝!
我在Java代碼中看不到本地'init'方法聲明。 – Malcolm
對不起,當我試圖包含'init'方法時,我不小心加入了'encode'方法。我相應地更新了代碼。謝謝! – ShannonS