[SOLVED] AppSettings.setAudioRenderer(null) causes a crash on Android

Hello. I’m trying to disable audio on Android by using AppSettings.setAudioRenderer(null) and it causes java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference on this line jmonkeyengine/JmeAndroidSystem.java at master · jMonkeyEngine/jmonkeyengine · GitHub

Is this a bug or jME is not intended to be used on Android without an audio (which is will be weird :upside_down_face:)?

The code does indeed imply that it’s not possible to set a null audio driver on android (even the default path will set one).

Also, to me it’s bad form that those .equals() statements are backwards:

if( AppSettings.ANDROID_MEDIAPLAYER.equals(settings.getAudioRenderer()) )

Is the more defensive way and the one preferred by a lot of java devs.

Edit: is there some specific reason that you want to set the audio renderer to null on Android?

Yes, I’m rendering a single frame offscreen and saving it to an image file. So I do not need any audio capabilities.

EDIT: my app does not render a 3D scene on a screen at all. I just press a button and the app saves an image.

I would have said you can set an empty string instead, but the else statement makes it mandatory, but you still can destroy the audio or not update it anyway, if you are using a custom renderer, but why its just one frame and it will be destroyed with the rest of jme context.

1 Like

Can you please clarify how can I destroy the audio?

EDIT: Well, after some tries I came to this code:

audioRenderer.cleanup();
audioRenderer = null;
stateManager.detach(stateManager.getState(AudioListenerState.class));

It seems it’s does what I need.

1 Like

To minimize the resources on android, you can try to set the JmeContext#Type enum to Headless, by doing so, jme will not render audio and audioRenderer would be null in the initialization process which will disable the audio update.

The code for initializing the audio :
jmonkeyengine/LegacyApplication.java at f8251c3443fbf4960c8fbb3c1ee85d8be3ff6f66 · jMonkeyEngine/jmonkeyengine · GitHub

The JmeContext (is the OGLESContext on android) :

I’ve already tried to use OffscreenSurface render type and found that on Android JmeContext.Type parameter is ignored and jME alway starts in Type.Display mode. See: jmonkeyengine/JmeAndroidSystem.java at f8251c3443fbf4960c8fbb3c1ee85d8be3ff6f66 · jMonkeyEngine/jmonkeyengine · GitHub

We’ve discussed it here: [SOLVED] Unable to start app with offscreen rendering on Android

1 Like

Yeah, you are right, i was missing this, i don’t know if you can change the value of OGLESContext#getType() via reflection or not.

hey bro can you give me your android gradle setting? i still can’t run my game on android how you do it?

You need to add those dependencies :

 /*add the JMonkeyEngine Dependencies*/
    implementation "org.jmonkeyengine:jme3-core:3.5.1-stable"
    implementation "org.jmonkeyengine:jme3-effects:3.5.1-stable"
    implementation "org.jmonkeyengine:jme3-android-native:3.5.1-stable"

There are simple android use cases here :

1 Like

thank you so much, it helped a lot :slight_smile:
jme is so much powerful but lacks on having project generator, gui ide for looking to terrains and models, it is not good to look for hours on some resources to find how to just go for android, i hope in next releases jme aim for having a project generator, and a tool with wiki to easily port games on different platforms, windows marketplace, google play, ios appstore, consoles even, besides linux,macos,and pc, i know we can do it already (doubt for uwp windows) but i mean a very handy smooth tool to do it with little effort!

@richtea is already working on this !! and it shows promising progress.

The tool provide that too, and you can do it through this example via gradle :

AFAIK, iOS support is still unstable.