Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IllegalStateException when MediaCodec.configure Android

I try create encoder for "audio/3gpp" and my app crash...

I use this code

String mMime = "audio/3gpp";
MediaCodec mMediaCodec = MediaCodec.createEncoderByType(mMime);
MediaFormat mMediaFormat = MediaFormat.createAudioFormat(mMime, RECORDER_SAMPLERATE, 1);
mMediaCodec.configure(mMediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mMediaCodec.start();

Exception
java.lang.IllegalStateException
android.media.MediaCodec.native_configure(Native Method)
at android.media.MediaCodec.configure(MediaCodec.java:256)
at com.agent.mobile.TestAppActivity.initMediaCodec(TestAppActivity.java:234)

like image 992
Agent P Avatar asked Jun 21 '13 11:06

Agent P


1 Answers

There are some mandatory values that must be set in the format. If you look at the docs for MediaFormat, it says "all keys not marked optional are mandatory". If you fail to set a mandatory key, MediaCodec throws an error because it has been left in an illegal state.

Add:

mMediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, <bit rate>);
mMediaFormat.setInteger(MediaFormat.KEY_SAMPLE_RATE, <sample rate>);
mMediaFormat.setInteger(MediaFormat.KEY_CHANNEL_COUNT, 1);

KEY_MIME should have been set for you by createEncoderByType().

like image 101
fadden Avatar answered Nov 04 '22 04:11

fadden