I have a problem with decoding mp3 file using MediaCodec
. Logcat says the problem is with the line codec.queueInputBuffer(inputBufferId, 0, data.length, 0, 0)
but it looks fair to me.
Code:
if(Build.VERSION.SDK_INT >= 21 ) {
try {
codec = MediaCodec.createDecoderByType("audio/mpeg");
} catch (IOException e) {
e.printStackTrace();
}
codec.setCallback(new MediaCodec.Callback() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onInputBufferAvailable(MediaCodec mc, int inputBufferId) {
ByteBuffer inputBuffer = codec.getInputBuffer(inputBufferId);
inputBuffer.wrap(data);
codec.queueInputBuffer(inputBufferId, 0, data.length, 0, 0); // java.lang.IllegalArgumentException
}
}
}
logcat:
FATAL EXCEPTION: main
Process: pl.test.projectx, PID: 18252
java.lang.IllegalArgumentException
at android.media.MediaCodec.native_queueInputBuffer(Native Method)
at android.media.MediaCodec.queueInputBuffer(MediaCodec.java:2334)
at pl.test.projectx.Decoder$2.onInputBufferAvailable(Decoder.java:107)
at android.media.MediaCodec$EventHandler.handleCallback(MediaCodec.java:1663)
at android.media.MediaCodec$EventHandler.handleMessage(MediaCodec.java:1621)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
What is data
here - the whole mp3 file? You need to split it into individual packets (e.g. using MediaExtractor
)
Then when feeding the data into the input buffer, inputBuffer.wrap(data);
doesn't do what you want. wrap
is actually a static method that creates a new ByteBuffer
. What you want is inputBuffer.clear(); inputBuffer.put(data);
.