src/core/android/SDL_android.cpp
changeset 6802 8a6b8ce97656
parent 6792 a15821a3471f
child 6806 9e57ff36fd7a
     1.1 --- a/src/core/android/SDL_android.cpp	Sun Jan 06 18:19:30 2013 -0800
     1.2 +++ b/src/core/android/SDL_android.cpp	Mon Jan 07 12:22:26 2013 -0300
     1.3 @@ -120,7 +120,7 @@
     1.4      midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass,
     1.5                                  "flipBuffers","()V");
     1.6      midAudioInit = mEnv->GetStaticMethodID(mActivityClass, 
     1.7 -                                "audioInit", "(IZZI)Ljava/lang/Object;");
     1.8 +                                "audioInit", "(IZZI)V");
     1.9      midAudioWriteShortBuffer = mEnv->GetStaticMethodID(mActivityClass,
    1.10                                  "audioWriteShortBuffer", "([S)V");
    1.11      midAudioWriteByteBuffer = mEnv->GetStaticMethodID(mActivityClass,
    1.12 @@ -433,13 +433,30 @@
    1.13      audioBuffer16Bit = is16Bit;
    1.14      audioBufferStereo = channelCount > 1;
    1.15  
    1.16 -    audioBuffer = env->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
    1.17 +    env->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
    1.18 +
    1.19 +    /* Allocating the audio buffer from the Java side and passing it as the return value for audioInit no longer works on
    1.20 +     * Android >= 4.2 due to a "stale global reference" error. So now we allocate this buffer directly from this side. */
    1.21 +    
    1.22 +    if (is16Bit) {
    1.23 +        jshortArray audioBufferLocal = env->NewShortArray(desiredBufferFrames * (audioBufferStereo ? 2 : 1));
    1.24 +        if (audioBufferLocal) {
    1.25 +            audioBuffer = env->NewGlobalRef(audioBufferLocal);
    1.26 +            env->DeleteLocalRef(audioBufferLocal);
    1.27 +        }
    1.28 +    }
    1.29 +    else {
    1.30 +        jbyteArray audioBufferLocal = env->NewByteArray(desiredBufferFrames * (audioBufferStereo ? 2 : 1));
    1.31 +        if (audioBufferLocal) {
    1.32 +            audioBuffer = env->NewGlobalRef(audioBufferLocal);
    1.33 +            env->DeleteLocalRef(audioBufferLocal);
    1.34 +        }
    1.35 +    }
    1.36  
    1.37      if (audioBuffer == NULL) {
    1.38 -        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: didn't get back a good audio buffer!");
    1.39 +        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: could not allocate an audio buffer!");
    1.40          return 0;
    1.41      }
    1.42 -    audioBuffer = env->NewGlobalRef(audioBuffer);
    1.43  
    1.44      jboolean isCopy = JNI_FALSE;
    1.45      if (audioBuffer16Bit) {