notlesh
notlesh

Reputation: 645

Use C++ array directly in Java API requiring byte[]

As it stands, I have a C++ app that has a unsigned char* buffer containing PCM audio data. I need to call the Android API method AudioTrack.write() on an instance of AudioTrack over JNI (from C++ to Java) with this data, and I would like to avoid making an extra copy in doing so. Can I do this?

AudioTrack accepts as one of its arguments a Java byte[], the argument that should correspond to my PCM data (unsigned char*).

Sorry if this is a duplicate... it's hard to effectively search for this kind of thing.

Upvotes: 2

Views: 976

Answers (1)

bobby
bobby

Reputation: 2789

Something like this should do.
I haven't compiled this, and it would be wise to check the syntax also with the specs.

jbyteArray byteArray; byteArray = env->NewByteArray(audioDataLength);
env->SetByteArrayRegion(byteArray, 0, audioDataLength , (jbyte*) audioData);

Where audioDataLength is the length of the char* audioData

Upvotes: 1

Related Questions