JNI Crash on method signature

I’m using the JNI methods to use the Android Media player in app. I get a crash when running the following code on Android 5.0 (Lollipop) but not Android 4.4 (KitKat):

Code:

IntPtrsetSurfaceMethodId = AndroidJNI.GetMethodID( mediaPlayer.GetRawClass(),“setSurface”,“(Landroid/view/Surface;)V”);
jvalue[ ] parms = newjvalue[1];
parms[0] = newjvalue();
parms[0].l = androidSurface;
AndroidJNI.CallObjectMethod( mediaPlayer.GetRawObject(), setSurfaceMethodId, parms );

Error:

JNI DETECTED ERROR IN APPLICATION: the return type of CallObjectMethodA does not match void android.media.MediaPlayer.setSurface(android.view.Surface)

Method Signature (as per MediaPlayer doc):

void setSurface(Surface surface);

Perhaps the Lollipop method has a different signature from the KitKat one? But I don’t see any separate docs for Lollipop. I’m new to Android development so just wondering if anyone knows what the issue might be or what the best forum would be to discuss it.

Thanks,
Saswat

Just tried out something I found on the Oculus forums and it works:

I replaced this:

AndroidJNI.CallObjectMethod( mediaPlayer.GetRawObject(), setSurfaceMethodId, parms );

With this:

AndroidJNI.CallVoidMethod( mediaPlayer.GetRawObject(), setSurfaceMethodId, parms );

You can use the AndroidJavaObject and AndroidJavaClass APIs when interacting with native Java code.

These APIs automatically match and build the JNI method signature according to the passed in parameters & return type, so it’s less error prone than trying to do it yourself.

Thanks for the info. I’m actually using sample code (part of the Oculus Mobile SDK) and I noticed this commented code right above the code I referenced earlier:

//Can’t use AndroidJavaObject.Call() with a jobject, must use low level interface
//mediaPlayer.Call( “setSurface”, androidSurface );

Not sure what the reason for that is. Will look into it and post here if I find anything.

there are some limitations with the AndroidJavaObject API, but it should work in most cases. Perhaps the sample is old from a time where this didn’t work properly…