Android NDK - JNI - 通过Logcat进行调试

时间:2015-07-10 19:55:27

标签: android c++ android-ndk java-native-interface logcat

我试图通过Logcat调试C ++ ..在myMethod()它工作正常,而在LoadPlayerA()它没有。

#define  ALOG(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)

void SuperpoweredExample::myMethod() {
ALOG("This message comes from C at line %d.", __LINE__);
}



void SuperpoweredExample::LoadPlayerA(const char *audioPath, int *params) {
ALOG("This message comes from D at line %d.", __LINE__);
}

extern "C" {    
JNIEXPORT void Java_de_meetspot_ndktest_MainActivity_LoadPlayerA(JNIEnv *javaEnvironment, jobject self, jstring audioPath, jlongArray offsetAndLength); 
JNIEXPORT void Java_de_meetspot_ndktest_MainActivity_myMethod(JNIEnv *javaEnvironment, jobject self);
}

JNIEXPORT void Java_de_meetspot_ndktest_MainActivity_LoadPlayerA(JNIEnv *javaEnvironment, jobject self, jstring audioPath, jlongArray params) {
   jlong *longParams = javaEnvironment->GetLongArrayElements(params, JNI_FALSE);
   int arr[6];
   for (int n = 0; n < 6; n++) arr[n] = longParams[n];
   javaEnvironment->ReleaseLongArrayElements(params, longParams, JNI_ABORT);

   const char *path = javaEnvironment->GetStringUTFChars(audioPath, JNI_FALSE);
   example = new SuperpoweredExample(path, arr);
   javaEnvironment->ReleaseStringUTFChars(audioPath, path);
}

JNIEXPORT void Java_de_meetspot_ndktest_MainActivity_myMethod(JNIEnv *javaEnvironment, jobject self) {
example->myMethod();
}

从Java MainActivity

调用这两个方法
textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String audioPath = "/sdcard/MP3/" + textView.getText();
                File baseDir = Environment.getExternalStorageDirectory();
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(audioPath);
                    fd = fis.getFD();


                    String samplerateString = null, buffersizeString = null;
                    if (Build.VERSION.SDK_INT >= 17) {
                        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                        samplerateString = audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE);
                        buffersizeString = audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
                    }
                    if (samplerateString == null) samplerateString = "44100";
                    if (buffersizeString == null) buffersizeString = "512";

                    statusTxt = (TextView) findViewById(R.id.statusTxt);
                    File audioFile = new File(audioPath);
                    int length = (int) audioFile.length();
                    long[] params = {
                            0,
                            length,
                            0,
                            length,
                            Integer.parseInt(samplerateString),
                            Integer.parseInt(buffersizeString)
                    };
                    myMethod();
                    LoadPlayerA(audioPath, params);
                    mp3List.setVisibility(View.GONE);

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });


private native void myMethod();
private native void LoadPlayerA(String path, long[] offsetAndLength);

那怎么可能?

0 个答案:

没有答案