Getting Activity instance with JNI call

I’m having trouble obtaining the activity instance through JNI call. Do you see an error in my code?

My AppActivity file, proj.android\src\org\cocos2dx\cpp\AppActivity.java:

package org.cocos2dx.cpp;

import android.os.Bundle;
//import android.content.Context;
import android.app.Activity;

import org.cocos2dx.lib.Cocos2dxActivity;

public class AppActivity extends Cocos2dxActivity {
	private static Activity _instance;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
	    super.onCreate(savedInstanceState);
	    _instance = this;
	}

	public static Activity getIn() 
	{
	    return _instance;
	}
}

Rest of the code:

void myFunc(){
	auto* env = JniHelper::getEnv();

	cocos2d::JniMethodInfo methodInfo;

	// I have tried with all three signatures, none of which worked

	if (! JniHelper::getStaticMethodInfo(
		methodInfo,
		"org/cocos2dx/cpp/AppActivity",
		"getIn",
		//"()Lorg/cocos2dx/cpp/AppActivity"))
		"()Landroid/app/Activity"))
		//"()Ljava/lang/Object"))
	{
	    // this segment is executed each time, meaning it failed
	    MXLOG_R("Error: Could not invoke Java method");
	    _ckEngineFailed = true;
	    return; 
	}

	jobject activity = methodInfo.env->CallStaticObjectMethod(methodInfo.classID, methodInfo.methodID);

	CkConfig config(env, activity); // I need the activity instance to initialize Cricket Audio

	methodInfo.env->DeleteLocalRef(methodInfo.classID);
	methodInfo.env->DeleteLocalRef(activity);
}

Hi check what we have done for this:

We have created JNI function:

JNIEXPORT void JNICALL Java_org_cocos2dx_cpp_AppActivity_initFirebase(JNIEnv* env, jobject thiz)

and after called it from AppActivity.java, so we manged to get then with JniHelper java class instance.

1 Like
1 Like

Thank you for posting your solutions! However I’ve gotten my version working too. The semicolon (" ; ") is mandatory for the signature, it looks like! When I tried the following:

if(!JniHelper::getStaticMethodInfo(
	methodInfo,
	"org/cocos2dx/cpp/AppActivity",
	"getIn",
	"()Landroid/app/Activity;"))
{
}

, now with the semicolon present, it worked (or at least for what I know the JNI call returns 1 instead of 0)! This seems very stupid design to me, but well this is it, keep writing the semicolons even if there is no list present!

(The code that followed the obtainment of the Activity instance still failed however, that needs further debugging)

If someone is interested in how to initialize the Cricket engine, you have to keep the activity reference alive until the CkInit(&config) function finishes. Now everything works fine. And by the way, if you use Cocos’ default audio engine (either the old or the new), you are doing it wrong :slight_smile: It has insane performance issues and a wide variety of bugs on most platforms. This is the alternative: http://www.crickettechnology.com

1 Like

Is it good one? Have you tested it already? we also were looking for new audio engine for cocos2d-x. Btw how to make music bank - do they have some software for it or it is some other softare which can be used with? Is loop music works without delay on Android devices?

On my Galaxy S7, using experimental::AudioEngine, playing a couple of short .ogg sounds every 256ms caused me -20 fps (!) and it didn’t even play half the sounds and their volumes were jumping around. With Cricket I haven’t noticed any fps drops and everything is as loud as I want it to be, plus it’s api has much more features.

Creating bank files is quite easy. Did you read their tutorial? http://www.crickettechnology.com/doc/Cricket.html

You list the files you want to put into your bank file in an xml file, then use the cktool that they provided with the rest of the engine. Easiest is to add the path of that cktool binary to your PATH variable (if u’re on windows) then go to where your .wav/.aiff and xml files are and do “cktool buildbank mybankxmlfile.ckbx mybank.ckb”.

Additionally, I think the easiest way to use the bank sounds then in your program is to initialize your bank AND your bank sounds at start of your program, and don’t destroy them at all (unless there’s sounds you really don’t want to use later in your program). I have a sound lookup where I store the CkSound pointers, whenever I want to play a sound I get a pointer to it and do sound->play()… that’s it. Setting the default volumes and such can be done at the step where you put them in your bank file but I just set all default settings at the time I initialize them. This way I can utilize procedural c++ tools instead of editing xml files.

2 Likes

Hi, can you please check my post I tagged u there. We now trying Cricket engine and so far don’t like that we should use wav files to make it work and it’s not streaming sounds by some reason when bank is not used.

Do you have any code snippet to do this for the Cricket config call? I have been using Cricket with Cocos2dx on Win32 and iOS, but haven’t been able to figure this part out for Android. Thanks!

Is Cricket engine good enough ?

I ddnt liked it :frowning: it was to simple and didnt worked:D so didnt got full feel of it.