Too many objects on Android JNI


I’m using OpenSL ES on Android via JNI to play sounds.

I use an asset and a source for each sound, but I can’t destroy them. The destroy method works on the source, but not on asset. In this way the engine stores objects and I reach the limit of 30 sounds very soon simply using the app.

The destroy method is called in a callback on the buffer queue as the decode is done. Calling the destroy method on the player object, my app is in Application Not Responding state.

Which is the best way to create and destroy sound sources?


Hi Dariux881,

It sounds like you are working cross-context and destroying a player in a callback, and then when the callback returns the execution thread does not have a context in which to work. Since your callback is still active, I’m guessing the implementation can’t do a complete cleanup of the player and sources. Therefore your sources are still hanging around even though the player object is no longer valid. Note that contexts are implementation specific.

I suggest you take a look at section 3.3 of the OpenSL ES 1.0.1 spec. To handle avoid cross-context issues, post a message to the main thread in your callback and clean up your players from your main thread. I recommend that you create and destroy all your objects from the main thread and not in callbacks.