您现在的位置:首页 > 博客 > Android开发 > UI开发 > 正文
Android的NDK开发—JNI数据结构之JNINativeMethod
http://www.drovik.com/      2013-7-2 11:55:37      来源:www.drovik.com      点击:

1、JNINativeMethod 结构体的官方定义

  1. typedef struct {  
  2.   
  3. const char* name;  
  4. const char* signature;  
  5. void* fnPtr;  
  6. } JNINativeMethod;  
typedef struct { const char* name; const char* signature; void* fnPtr; } JNINativeMethod;

第一个变量name是Java中函数的名字。

第二个变量signature,用字符串是描述了Java中函数的参数和返回值

第三个变量fnPtr是函数指针,指向native函数。前面都要接 (void *)

第一个变量与第三个变量是对应的,一个是java层方法名,对应着第三个参数的native方法名字

更多内容请查看之前博文:http://blog.csdn.net/conowen/article/details/7521340


示例:

  1. /*  
  2.  * 由于gMethods[]是一个<名称,函数指针>对照表,在程序执行时,  
  3.  * 可多次调用registerNativeMethods()函数来更换本地函数的指针,  
  4.  * 从而达到弹性调用本地函数的目的。   
  5.  *具体可以参看http://blog.csdn.net/conowen/article/details/7521340 
  6.  */    
  7. static JNINativeMethod gMethods[] = {    
  8.     {"setDataSource",       "(Ljava/lang/String;)V",            (void *)com_media_ffmpeg_FFMpegPlayer_setDataSource},    
  9.     {"_setVideoSurface",    "(Landroid/view/Surface;)V",        (void *)com_media_ffmpeg_FFMpegPlayer_setVideoSurface},    
  10.     {"prepare",             "()V",                              (void *)com_media_ffmpeg_FFMpegPlayer_prepare},    
  11.     {"_start",              "()V",                              (void *)com_media_ffmpeg_FFMpegPlayer_start},    
  12.     {"_stop",               "()V",                              (void *)com_media_ffmpeg_FFMpegPlayer_stop},    
  13.     {"getVideoWidth",       "()I",                              (void *)com_media_ffmpeg_FFMpegPlayer_getVideoWidth},    
  14.     {"getVideoHeight",      "()I",                              (void *)com_media_ffmpeg_FFMpegPlayer_getVideoHeight},    
  15.     {"seekTo",              "(I)V",                             (void *)com_media_ffmpeg_FFMpegPlayer_seekTo},    
  16.     {"_pause",              "()V",                              (void *)com_media_ffmpeg_FFMpegPlayer_pause},    
  17.     {"isPlaying",           "()Z",                              (void *)com_media_ffmpeg_FFMpegPlayer_isPlaying},    
  18.     {"getCurrentPosition",  "()I",                              (void *)com_media_ffmpeg_FFMpegPlayer_getCurrentPosition},    
  19.     {"getDuration",         "()I",                              (void *)com_media_ffmpeg_FFMpegPlayer_getDuration},    
  20.     {"_release",            "()V",                              (void *)com_media_ffmpeg_FFMpegPlayer_release},    
  21.     {"_reset",              "()V",                              (void *)com_media_ffmpeg_FFMpegPlayer_reset},    
  22.     {"setAudioStreamType",  "(I)V",                             (void *)com_media_ffmpeg_FFMpegPlayer_setAudioStreamType},    
  23.     {"native_init",         "()V",                              (void *)com_media_ffmpeg_FFMpegPlayer_native_init},    
  24.     {"native_setup",        "(Ljava/lang/Object;)V",            (void *)com_media_ffmpeg_FFMpegPlayer_native_setup},    
  25.     {"native_finalize",     "()V",                              (void *)com_media_ffmpeg_FFMpegPlayer_native_finalize},    
  26.     {"native_suspend_resume""(Z)I",                           (void *)com_media_ffmpeg_FFMpegPlayer_native_suspend_resume},    
  27. };    
  28.     
/* * 由于gMethods[]是一个<名称,函数指针>对照表,在程序执行时, * 可多次调用registerNativeMethods()函数来更换本地函数的指针, * 从而达到弹性调用本地函数的目的。 *具体可以参看http://blog.csdn.net/conowen/article/details/7521340 */ static JNINativeMethod gMethods[] = { {"setDataSource", "(Ljava/lang/String;)V", (void *)com_media_ffmpeg_FFMpegPlayer_setDataSource}, {"_setVideoSurface", "(Landroid/view/Surface;)V", (void *)com_media_ffmpeg_FFMpegPlayer_setVideoSurface}, {"prepare", "()V", (void *)com_media_ffmpeg_FFMpegPlayer_prepare}, {"_start", "()V", (void *)com_media_ffmpeg_FFMpegPlayer_start}, {"_stop", "()V", (void *)com_media_ffmpeg_FFMpegPlayer_stop}, {"getVideoWidth", "()I", (void *)com_media_ffmpeg_FFMpegPlayer_getVideoWidth}, {"getVideoHeight", "()I", (void *)com_media_ffmpeg_FFMpegPlayer_getVideoHeight}, {"seekTo", "(I)V", (void *)com_media_ffmpeg_FFMpegPlayer_seekTo}, {"_pause", "()V", (void *)com_media_ffmpeg_FFMpegPlayer_pause}, {"isPlaying", "()Z", (void *)com_media_ffmpeg_FFMpegPlayer_isPlaying}, {"getCurrentPosition", "()I", (void *)com_media_ffmpeg_FFMpegPlayer_getCurrentPosition}, {"getDuration", "()I", (void *)com_media_ffmpeg_FFMpegPlayer_getDuration}, {"_release", "()V", (void *)com_media_ffmpeg_FFMpegPlayer_release}, {"_reset", "()V", (void *)com_media_ffmpeg_FFMpegPlayer_reset}, {"setAudioStreamType", "(I)V", (void *)com_media_ffmpeg_FFMpegPlayer_setAudioStreamType}, {"native_init", "()V", (void *)com_media_ffmpeg_FFMpegPlayer_native_init}, {"native_setup", "(Ljava/lang/Object;)V", (void *)com_media_ffmpeg_FFMpegPlayer_native_setup}, {"native_finalize", "()V", (void *)com_media_ffmpeg_FFMpegPlayer_native_finalize}, {"native_suspend_resume", "(Z)I", (void *)com_media_ffmpeg_FFMpegPlayer_native_suspend_resume}, };


主要是第二个参数比较复杂:

括号里面表示参数的类型,括号后面表示返回值。

"()" 中的字符表示参数,后面的则代表返回值。例如"()V" 就表示void Fun();

"(II)V" 表示 void Fun(int a, int b);

这些字符与函数的参数类型的映射表如下:


2、第二个参数之基本数据类型




3、第二个参数之对象类型与数组类型


对象类型:以"L"开头,以";"结尾,中间是用"/" 隔开。如上表第1个

数组类型:以"["开始。如上表第2个(n维数组的话,则是前面多少个"["而已,如"[[[D"表示“double[][][]”)

对象数组类型:上述两者结合,如上表第3个


3.1、对象类型与数组类型的举例:


分享到:
发表评论(0)
姓名 *
评论内容 *
验证码 *图片看不清?点击重新得到验证码