I am
looking at the file:///opt/android-ndk-r7/docs/opensles/index.html
document. The "Decode audio to PCM" section seems to indicate that
this is possible in API 14. I tried it, but cannot get it to work.
My project build target is Android 4.0.
I modified the asset player creation:
// create asset audio player
jboolean
Java_com_example_nativeaudio_NativeAudio_createAssetAudioPlayer(JNIEnv*
env, jclass clazz,
jobject assetManager, jstring filename)
{
SLresult result;
// convert Java string to UTF-8
const jbyte *utf8 = (*env)->GetStringUTFChars(env, filename,
NULL);
assert(NULL != utf8);
// use asset manager to open asset by filename
AAssetManager* mgr = AAssetManager_fromJava(env, assetManager);
assert(NULL != mgr);
AAsset* asset = AAssetManager_open(mgr, (const char *) utf8,
AASSET_MODE_UNKNOWN);
// release the Java string and UTF-8
(*env)->ReleaseStringUTFChars(env, filename, utf8);
// the asset might not be found
if (NULL == asset) {
return JNI_FALSE;
}
// 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};
// configure audio sink
SLDataLocator_AndroidSimpleBufferQueue loc_bufq =
{SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
SLDataFormat_PCM format_pcm = {SL_DATAFORMAT_PCM, 2,
SL_SAMPLINGRATE_44_1,
SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16,
SL_SPEAKER_FRONT_LEFT|SL_SPEAKER_FRONT_RIGHT,
SL_BYTEORDER_LITTLEENDIAN};
SLDataSink audioSnk = {&loc_bufq, &format_pcm};
result = (*engineEngine)->CreateAudioPlayer(engineEngine,
&fdPlayerObject, &audioSrc, &audioSnk,
0, NULL, NULL);
assert(SL_RESULT_SUCCESS == result);
// realize the player
result = (*fdPlayerObject)->Realize(fdPlayerObject,
SL_BOOLEAN_FALSE);
assert(SL_RESULT_SUCCESS == result);
// get the play interface
result = (*fdPlayerObject)->GetInterface(fdPlayerObject,
SL_IID_PLAY, &fdPlayerPlay);
assert(SL_RESULT_SUCCESS == result);
// get the buffer queue interface
result = (*fdPlayerObject)->GetInterface(fdPlayerObject,
SL_IID_BUFFERQUEUE,
&fdPlayerBufferQueue);
assert(SL_RESULT_SUCCESS == result);
// register callback on the buffer queue
// result = (*fdPlayerBufferQueue)-
>RegisterCallback(fdPlayerBufferQueue, fdPlayerCallback, NULL);
// assert(SL_RESULT_SUCCESS == result);
return JNI_TRUE;
}
This returns SL_RESULT_FEATURE_UNSUPPORTED:
// get the buffer queue interface
result = (*fdPlayerObject)->GetInterface(fdPlayerObject,
SL_IID_BUFFERQUEUE,
&fdPlayerBufferQueue);
assert(SL_RESULT_SUCCESS == result);
Can anyone verify if this should really work or not?
Thank you very much.
Bob