Dylan Turney
Dylan Turney

Reputation: 85

How to pass file to JNI for playing in OpenSL ES AudioPlayer?

I'm trying to create AudioPlayer's (as per the Native-Audio NDK sample) but without using the AssetManager as the files to be played are downloaded dynamically, and hence not packaged as an Assset.

So I was wondering if it were possible to pass a FileDescriptor from Java to the JNI to be used in such a situation as the sample JNI code below (without using AssetManager):

// open asset as file descriptor
off_t start, length;
int fd = AAsset_openFileDescriptor(asset, &start, &length);
assert(0 <= fd);
AAsset_close(asset);

// configure audio source
SLDataLocator_AndroidFD loc_fd = {SL_DATALOCATOR_ANDROIDFD, fd, start, length};
SLDataFormat_MIME format_mime = {SL_DATAFORMAT_MIME, NULL, SL_CONTAINERTYPE_UNSPECIFIED};
SLDataSource audioSrc = {&loc_fd, &format_mime}

Unfortunately on the Java side the FileDescriptor is a class and not an int (SLuint32) as to be used by SLDataLocator_AndroidFD.

Constructor below:

/** File Descriptor-based data locator definition, locatorType must be SL_DATALOCATOR_ANDROIDFD */ typedef struct SLDataLocator_AndroidFD_ {
    SLuint32        locatorType;
    SLint32         fd;
    SLAint64        offset;
    SLAint64        length; } SLDataLocator_AndroidFD;

Any help would be appreciated!

Upvotes: 1

Views: 778

Answers (1)

Jens Zalzala
Jens Zalzala

Reputation: 2576

Not sure if this answers the question exactly, but here's a bit of code I found to read files from the sd card. If you're downloading the files to external storage, this should work for you:

SLchar path[] = "/sdcard/audio/my_audio.mp3" ;
SLDataLocator_URI loc_uri = {SL_DATALOCATOR_URI, path};
SLDataFormat_MIME format_mime = {SL_DATAFORMAT_MIME, NULL,
        SL_CONTAINERTYPE_UNSPECIFIED};
SLDataSource audioSrc = {&loc_uri, &format_mime};

Upvotes: 1

Related Questions