IllegalStateException while reading from ShortBuffer in Unity Android

I'm working on a game in Unity where I need raw audio data from a media buffer. The following code is used as a plugin jar in Unity. It works on android 4.x with no problem. But when I try to run it on Android 5.0.1 or 5.1, I get the following exception, but only when I try to open aac or m4a files. Any suggestions?

This is the function where I read the sound buffer:

   public short[] ReadNextSamples() {
        if (sawOutputEOS) {
            return new short[0];
        }
        int res;
        while (true)
        {
            res = codec.dequeueOutputBuffer(info, kTimeOutUs);

        if (res >= 0)
        {
            if (info.size > 0)
            {
                break;
            }
            else
            {
                codec.releaseOutputBuffer(res, false);

                if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM)!= 0)
                {
                    sawOutputEOS = true;
                    return new short[0];
                }
            }
        }
    }
    int outputBufIndex = res;
    ByteBuffer buf = codecOutputBuffers[outputBufIndex];
    int sampleSize = info.size / 2;
    short[] sampleValues = new short[sampleSize];
    ShortBuffer shBuf = buf.asShortBuffer();

    try {
        shBuf.get(sampleValues, 0, sampleSize);
    } catch (Exception e) {
        //the exception is being thrown here
        e.printStackTrace();
    }

    codec.releaseOutputBuffer(outputBufIndex, false);
    if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
        sawOutputEOS = true;
    }
    return sampleValues;
}

      

An exception:

06-04 16:18:01.188: W/System.err(20884): java.lang.IllegalStateException: buffer is inaccessible
06-04 16:18:01.188: W/System.err(20884): at java.nio.DirectByteBuffer.checkIsAccessible(DirectByteBuffer.java:551)
06-04 16:18:01.188: W/System.err(20884): at java.nio.DirectByteBuffer.get(DirectByteBuffer.java:155)
06-04 16:18:01.188: W/System.err(20884): at java.nio.ByteBufferAsShortBuffer.get(ByteBufferAsShortBuffer.java:103)
06-04 16:18:01.190: W/System.err(20884): at com.pocketgames.musiverse.MediaDecoder.ReadNextSamples(MediaDecoder.java:112)
06-04 16:18:01.190: W/System.err(20884): at com.unity3d.player.ReflectionHelper.nativeProxyInvoke(Native Method)
06-04 16:18:01.190: W/System.err(20884): at com.unity3d.player.ReflectionHelper.a(Unknown Source)
06-04 16:18:01.190: W/System.err(20884): at com.unity3d.player.ReflectionHelper$1.invoke(Unknown Source)
06-04 16:18:01.190: W/System.err(20884): at java.lang.reflect.Proxy.invoke(Proxy.java:397)
06-04 16:18:01.190: W/System.err(20884): at $Proxy0.run(Unknown Source)
06-04 16:18:01.191: W/System.err(20884): at java.lang.Thread.run(Thread.java:818)

      

+3


source to share


2 answers


Basically you should use API level dependent code. I think you may have figured this out by now. But for documentation purposes you should use

ByteBuffer buf = codecOutputBuffers[outputBufIndex];

      

for API level <21

and

ByteBuffer buf = codec.getOutputBuffer(outputBufIndex);

      

for API level> = 21.



The code looks something like this:

final int version = Build.VERSION.SDK_INT;
ByteBuffer buf;
if (version >= 21) {
    buf = codec.getOutputBuffer(outputBufIndex);
} else {
    buf = codecOutputBuffers[outputBufIndex];
}

      

If you look at the source code of the class MediaCodec

, the code for getOutputBuffer

:

@Nullable
public ByteBuffer getOutputBuffer(int index) {
    ByteBuffer newBuffer = getBuffer(false /* input */, index);
    synchronized(mBufferLock) {
        invalidateByteBuffer(mCachedOutputBuffers, index);
        mDequeuedOutputBuffers.put(index, newBuffer);
    }
    return newBuffer;
}

      

So the error you got is related to acquiring a lock mBufferLock

, which might be required when implementing MediaCodec

for API level> = 21, and hence you will get an unavailable error. Hope this helps.

+3


source


I ran into a similar problem in Android L and could be solved using the new MediaCodec API to get the buffer.

Instead

ByteBuffer buf = codecOutputBuffers[outputBufIndex];

      



you can use

ByteBuffer buf = codec.getOutputBuffer(outputBufIndex);

      

In my case, this solved the problem when accessing input buffers.

+2


source







All Articles