Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaExtractor throws IllegalArgumentException when used with wav file

I am using the Android MediaExtractor like this:

MediaExtractor extractor = new MediaExtractor();
extractor.setDataSource("path/to/my/wav/file.wav");
extractor.selectTrack(0);

ByteBuffer inputBuffer = codec.getInputBuffer(inputBufferIndex);
int sampleSize =  extractor.readSampleData(inputBuffer, 0);

The inputBuffer is provided by MediaCodec which is configured as AAC Encoder. The goal is to convert the wave file to aac. The above code is abbreviated, of course, but I tracked the error down to the last line.

It also only occurs when using the MediaExtractor with a wav file. For example, I used a .m4a instead and everything worked fine.

The documentation of MediaExtractor says:

MediaExtractor facilitates extraction of demuxed, typically encoded, media data from a data source.

"typically encdoded" does not exclude un-encodec PCM audio....right? Anyone tried this before or knows another stable(!) way to:

  • extract the audio samples from a wav (excluding 44byte or 46byte header)?
  • convert an wav file to aac on Android?

UPDATE

Here is the logcat:

W/System.err: java.lang.IllegalArgumentException
W/System.err:     at android.media.MediaExtractor.readSampleData(Native Method)
W/System.err:     at com.myproject.android.audiosandbox.convert.MediaEncoder2.encodeLollipopStyle(MediaEncoder2.java:247)
W/System.err:     at com.myproject.android.audiosandbox.convert.MediaEncoder2.encodeSong(MediaEncoder2.java:119)
W/System.err:     at com.myproject.android.audiosandbox.convert.MediaEncoder2.encode(MediaEncoder2.java:70)
W/System.err:     at com.myproject.android.audiosandbox.fragments.AudioConvertFragment$1.onClick(AudioConvertFragment.java:40)
W/System.err:     at android.view.View.performClick(View.java:4763)
W/System.err:     at android.view.View$PerformClick.run(View.java:19821)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:135)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5272)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)

I am using API 16, so MediaMuxer is not an option (was added in API 18).

like image 659
muetzenflo Avatar asked Jun 24 '16 08:06

muetzenflo


1 Answers

I could not figure out "if" or "why not" the MediaExtractor seems to be unable to read data from a simple wav file, so I chose a different approach. Since WAV file don't really need to be decoded, I now use a simple FileInputStream and skip the 44-byte header.

I got a working sample published in this gist: https://gist.github.com/muetzenflo/3e83975aba6abe63413abd98bb33c401

After almost one week of research, try and error, this piece of code seems to be rare. I documented almost every line and hope that I can help others who struggle with the MediaCodec class on Android.

like image 103
muetzenflo Avatar answered Nov 08 '22 14:11

muetzenflo