1

我必须在我的项目中使用 OpenSL(尝试过 Soundpool,但它一点都不好)。但是,在多次播放声音后(大约连续 35 次),应用程序被关闭(我猜可能是因为溢出)。

我试图释放缓冲区/内存来解决问题(看在上帝的份上,我不知道如何使用这个 OpenSL)。所以我决定在文件播放完毕后尝试释放。OpenSL ES 为我们提供了RegisterCallback可能有所帮助的功能。在我的情况下(使用 URI 文件)发生了新问题,RegisterCallback导致我的应用程序再次崩溃。它卡住了。

这是我的代码。请帮忙。紧接着就崩溃了//register callback for uri

static short fdBuffer[SAWTOOTH_FRAMES];

// create URI audio player
    jboolean Java_jp_mobigame_ayakashi_CardGameActivity_createUriAudioPlayer(JNIEnv* env, jclass clazz,
            jstring uri)
    {
        __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n createUri \n");
        SLresult result;

        // convert Java string to UTF-8
        const jbyte *utf8 = (*env)->GetStringUTFChars(env, uri, NULL);
        assert(NULL != utf8);

        // configure audio source
        // (requires the INTERNET permission depending on the uri parameter)
        SLDataLocator_URI loc_uri = {SL_DATALOCATOR_URI, (SLchar *) utf8};
        SLDataFormat_MIME format_mime = {SL_DATAFORMAT_MIME, NULL, SL_CONTAINERTYPE_UNSPECIFIED};
        SLDataSource audioSrc = {&loc_uri, &format_mime};

        // configure audio sink
        SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, outputMixObject};
        SLDataSink audioSnk = {&loc_outmix, NULL};

        // create audio player
        const SLInterfaceID ids[3] = {SL_IID_SEEK, SL_IID_MUTESOLO /*SL_IID_BUFFERQUEUE*/, SL_IID_VOLUME};
        const SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
        result = (*engineEngine)->CreateAudioPlayer(engineEngine, &uriPlayerObject, &audioSrc,
                &audioSnk, 3, ids, req);
        // note that an invalid URI is not detected here, but during prepare/prefetch on Android,
        // or possibly during Realize on other platforms
        assert(SL_RESULT_SUCCESS == result);

        // release the Java string and UTF-8
        (*env)->ReleaseStringUTFChars(env, uri, utf8);

        // realize the player
        result = (*uriPlayerObject)->Realize(uriPlayerObject, SL_BOOLEAN_FALSE);
        // this will always succeed on Android, but we check result for portability to other platforms
        if (SL_RESULT_SUCCESS != result) {
            (*uriPlayerObject)->Destroy(uriPlayerObject);
            uriPlayerObject = NULL;
            return JNI_FALSE;
        }

        // get the play interface
        result = (*uriPlayerObject)->GetInterface(uriPlayerObject, SL_IID_PLAY, &uriPlayerPlay);
        assert(SL_RESULT_SUCCESS == result);

        // get the seek interface
        result = (*uriPlayerObject)->GetInterface(uriPlayerObject, SL_IID_SEEK, &uriPlayerSeek);
        assert(SL_RESULT_SUCCESS == result);


        __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n createUri 1\n");
        //get the buffer queue interface
        result = (*uriPlayerObject)->GetInterface(uriPlayerObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &uriPlayerBufferQueue);
        assert(SL_RESULT_SUCCESS == result);
        __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n createUri 2\n");


        //register callback for uri
        result = (*uriPlayerBufferQueue)->RegisterCallback(uriPlayerBufferQueue, bqUriPlayerCallback, NULL);
        assert(SL_RESULT_SUCCESS == result);
        __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n createUri 4\n");

        //Enqueue
        __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n createUri 3\n");
        result = (*uriPlayerBufferQueue)->Enqueue(uriPlayerBufferQueue, fdBuffer, sizeof(fdBuffer));


        // get the mute/solo interface
        result = (*uriPlayerObject)->GetInterface(uriPlayerObject, SL_IID_MUTESOLO, &uriPlayerMuteSolo);
        assert(SL_RESULT_SUCCESS == result);

        // get the volume interface
        result = (*uriPlayerObject)->GetInterface(uriPlayerObject, SL_IID_VOLUME, &uriPlayerVolume);
        assert(SL_RESULT_SUCCESS == result);

        return JNI_TRUE;

    }
4

1 回答 1

1

看起来您正在尝试获取缓冲区队列接口,即使您正在指定 uri 源。您似乎也在排队数据。如果您只是尝试从 uri 播放音频,则不需要这样做。如果您尝试输出到缓冲区而不是扬声器,那么这可能是有效的。无论如何,我都不是 OpenSL 专家,所以这可能是有原因的。但是,如果没有原因,请尝试在播放器对象上注册回调。

(*uriPlayerPlay)->RegisterCallback(uriPlayerPlay, bqUriPlayerCallback, NULL);

附带说明一下,您可能需要设置回调事件掩码。有几种选择,但我相信您正在寻找的是:

(*uriPlayerPlay)->SetCallbackEventsMask(uriPlayerPlay, SL_PLAYEVENT_HEADATEND)

请注意,这可能存在问题。请参阅https://groups.google.com/forum/?fromgroups=#!topic/android-ndk/7YaqlKlDMO4

如果这没有帮助,查看回调函数的定义可能会有所帮助。我相信它应该看起来像:

static void bqUriPlayerCallback (SLPlayItf caller, void *pContext, SLuint32 playevent)
于 2013-03-18T17:54:05.370 回答