三星Galaxy note 4中的MediaCodec CodecException

时间:2017-06-26 16:59:40

标签: android mediacodec android-mediacodec

我正在尝试使用https://github.com/saki4510t/ScreenRecordingSample/tree/master/app/src/main/java/com/serenegiant/media录制屏幕,并提供了一些相关的代码段:

@Override
void prepare() throws IOException {
    if (DEBUG) Log.i(TAG, "prepare: ");
    mSurface = prepare_surface_encoder(MIME_TYPE, fps, bitrate);
    mMediaCodec.start();
    mIsRecording = true;
    new Thread(mScreenCaptureTask, "ScreenCaptureThread").start();
    if (DEBUG) Log.i(TAG, "prepare finishing");
    if (mListener != null) {
        try {
            mListener.onPrepared(this);
        } catch (final Exception e) {
            Log.e(TAG, "prepare:", e);
        }
    }
}   
protected MediaFormat create_encoder_format(final String mime, final int frame_rate, final int bitrate) {
    if (DEBUG) Log.v(TAG, String.format("create_encoder_format:(%d,%d),mime=%s,frame_rate=%d,bitrate=%d", mWidth, mHeight, mime, frame_rate, bitrate));
    final MediaFormat format = MediaFormat.createVideoFormat(mime, mWidth, mHeight);
    format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);  // API >= 18
    format.setInteger(MediaFormat.KEY_BIT_RATE, bitrate > 0 ? bitrate : calcBitRate(frame_rate)); //800000
    format.setInteger(MediaFormat.KEY_FRAME_RATE,30);
    format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 10);
    return format;
}

protected Surface prepare_surface_encoder(final String mime, final int frame_rate, final int bitrate)
    throws IOException, IllegalArgumentException {

    if (DEBUG) Log.v(TAG, String.format("prepare_surface_encoder:(%d,%d),mime=%s,frame_rate=%d,bitrate=%d", mWidth, mHeight, mime, frame_rate, bitrate));

    mTrackIndex = -1;
    mMuxerStarted = mIsEOS = false;

    final MediaCodecInfo videoCodecInfo = selectVideoCodec(mime);
    if (videoCodecInfo == null) {
        throw new IllegalArgumentException("Unable to find an appropriate codec for " + mime);
    }
    if (DEBUG) Log.i(TAG, "selected codec: " + videoCodecInfo.getName());

    final MediaFormat format = create_encoder_format(mime, frame_rate, bitrate);
    if (DEBUG) Log.i(TAG, "format: " + format);

    mMediaCodec = MediaCodec.createEncoderByType(mime);
    mMediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
    // get Surface for encoder input
    // this method only can call between #configure and #start
    return mMediaCodec.createInputSurface();    // API >= 18
}

它适用于大多数设备,但不适用于运行Android 6.0.1的Samsung Galaxy note 4。我在哪里得到以下错误。

`Non-fatal Exception: android.media.MediaCodec$CodecException: Error 0xffffec77
   at android.media.MediaCodec.native_configure(MediaCodec.java)
   at android.media.MediaCodec.configure(MediaCodec.java:1778)
   at com.urcaddy.utilities.utils.media.MediaVideoEncoderBase.prepare_surface_encoder(MediaVideoEncoderBase.java:90)
   at com.urcaddy.utilities.utils.media.MediaScreenEncoder.prepare(MediaScreenEncoder.java:87)
   at com.urcaddy.utilities.utils.media.MediaMuxerWrapper.prepare(MediaMuxerWrapper.java:74)
   at com.urcaddy.services.backgroundservices.ScreenRecorderService.startScreenRecord(ScreenRecorderService.java:229)
   at com.urcaddy.services.backgroundservices.ScreenRecorderService.onStartCommand(ScreenRecorderService.java:139)
   at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4062)
   at android.app.ActivityThread.access$2400(ActivityThread.java:221)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1897)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:158)
   at android.app.ActivityThread.main(ActivityThread.java:7225)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)`

1 个答案:

答案 0 :(得分:0)

经过大量的讨论,我们发现问题在于高度和宽度。下面是代码对我们有用。

protected MediaFormat create_encoder_format(final String mime, final int frame_rate, final int bitrate) {
if (DEBUG) Log.v(TAG, String.format("create_encoder_format:(%d,%d),mime=%s,frame_rate=%d,bitrate=%d", 720, 1280, mime, frame_rate, bitrate));
final MediaFormat format = MediaFormat.createVideoFormat(mime, 720, 1280);
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);  // API >= 18
format.setInteger(MediaFormat.KEY_BIT_RATE, bitrate > 0 ? bitrate : calcBitRate(frame_rate)); //800000
format.setInteger(MediaFormat.KEY_FRAME_RATE,30);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 10);
return format;
}