I want to decode audio files to raw PCM data to stream it to a playback device in the local network. I use the new MediaExtractor and MediaCodec classes, introduced with API level 16, for that. The device requires the PCM data to be in 44,100 kHz, have 2 channels and a 16 bit sample size. This is working fine as long as the input file roughly matches these requirements. However whenever I'm decoding a MP3 file that uses - for example - a sample rate of 32,000 kHz and maybe has only one channel then I don't get the required output from the MediaCodec class.
As it seems I can't specify the output format of the MediaCodec class. So I decided to instantiate another MediaCodec object to re-encode the raw data into my desired format. According to the list of supported media formats Android supports encoding to PCM/Wave since Android 4.1. However I'm unable to create a MediaCodec object that encodes to PCM/Wave. I tried passing all kinds of MIME types to MediaCodec.createEncoderByType(type); but I always failed with an IOException:
java.io.IOException: Failed to allocate component instance
at android.media.MediaCodec.native_setup(Native Method)
at android.media.MediaCodec.<init>(MediaCodec.java:210)
at android.media.MediaCodec.createEncoderByType(MediaCodec.java:194)
[..]
Has anyone of you been able to successfully create a MediaCodec instance that encodes to PCM/Wave and can provide me with a working example?