3

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) 
2
  • 1
    try using MediaCodec.createByCodecName(). Even i faced this issue and i solved it using the above method. i noticed in most of mediatek devices createDecoderByType doesn't work properly. Commented Sep 17, 2017 at 14:31
  • @Gautam I've just checked it. Changed MediaCodec.createDecoderByType("audio/mpeg") for MediaCodec.createByCodecName("OMX.google.mp3.decoder") but the effect is unfortunately the same. Thanks for try anyway. Commented Sep 17, 2017 at 15:06

1 Answer 1

1

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);.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, data was whole mp3 file. What do you mean by writing "split into invidual packets"? Packets mean samples, frames or something else?
Thank you. I have another problem now but this is for another question :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.