You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 12以下版本录屏时MediaCodec报0x80001001错误崩溃求助

解决Android 12以下MediaCodec配置崩溃(Error 0x80001001)

问题分析

崩溃发生在MediaCodec.configure()阶段,错误码0x80001001通常表示配置参数不被设备编码器支持,结合仅在Android 12以下出现的现象,核心问题大概率是参数设置错误或旧版本兼容性问题。

核心修复点

1. 修正比特率参数错误

你的代码中存在明显的参数类型错误:

// 错误写法:1_000_000.millis 返回的是Duration对象,并非Long类型的比特率值
format.setLong(MediaFormat.KEY_BIT_RATE, 1_000_000.millis)

KEY_BIT_RATE要求传入Long类型的比特率数值(单位:bps),修改为:

format.setLong(MediaFormat.KEY_BIT_RATE, 1000000L) // 直接传入1Mbps的数值

这是导致旧版本崩溃的最可能原因——Android 12可能对参数类型有更宽松的兼容,而旧版本会严格校验参数类型和有效值。

2. 增加编码器兼容性校验

Android 12以下设备的编码器对参数支持差异较大,建议在配置前先校验设备支持的参数范围:

// 查询指定MIME类型的编码器信息
val codecInfo = MediaCodecList(MediaCodecList.REGULAR_CODECS).findEncoderForFormat(format)
    ?: throw IllegalArgumentException("No encoder found for $MIME_TYPE")

// 获取编码器支持的能力
val capabilities = codecInfo.getCapabilitiesForType(MIME_TYPE)
// 确认COLOR_FormatSurface是否被支持
val supportsSurfaceFormat = capabilities.colorFormats.contains(MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface)
if (!supportsSurfaceFormat) {
    // fallback到其他支持的格式,比如COLOR_FormatYUV420Flexible
    val supportedFormat = capabilities.colorFormats.firstOrNull {
        it == MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible
    } ?: throw IllegalArgumentException("No supported color format found")
    format.setInteger(MediaFormat.KEY_COLOR_FORMAT, supportedFormat)
}

// 校验帧率范围
val framerateRanges = capabilities.videoCapabilities.supportedFrameRateRanges
val isFramerateSupported = framerateRanges.any { it.contains(FRAME_RATE) }
if (!isFramerateSupported) {
    // 调整为设备支持的帧率,比如取范围中间值
    format.setInteger(MediaFormat.KEY_FRAME_RATE, framerateRanges[0].upper)
}

3. EGL上下文兼容调整

对于Android 12以下设备,使用EglBase.create时,确保EGL配置符合编码器的录制要求:

// 显式指定EGL配置属性,替代默认的CONFIG_RECORDABLE
val eglConfigAttributes = intArrayOf(
    EGL14.EGL_RED_SIZE, 8,
    EGL14.EGL_GREEN_SIZE, 8,
    EGL14.EGL_BLUE_SIZE, 8,
    EGL14.EGL_ALPHA_SIZE, 8,
    EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT,
    EGL_RECORDABLE_ANDROID, 1,
    EGL14.EGL_NONE
)
eglBase = EglBase.create(sharedContext, eglConfigAttributes)

4. 增强异常处理

在configure阶段捕获异常并打印详细信息,方便调试:

try {
    encoder = MediaCodec.createEncoderByType(MIME_TYPE)
    encoder!!.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE)
} catch (e: MediaCodec.CodecException) {
    Log.e("MediaCodec", "Configure failed: ${e.errorCode}, ${e.message}")
    // 打印编码器支持的格式信息
    val codecList = MediaCodecList(MediaCodecList.REGULAR_CODECS)
    val encoders = codecList.codecInfos.filter { it.isEncoder }
    encoders.forEach { info ->
        if (info.supportedTypes.contains(MIME_TYPE)) {
            Log.e("MediaCodec", "Supported formats for ${info.name}: ${info.getCapabilitiesForType(MIME_TYPE).colorFormats}")
        }
    }
    throw e
}

验证方案

  1. 先修复比特率参数错误,测试Android 12以下设备是否仍崩溃
  2. 若问题仍存在,添加兼容性校验代码,调整参数到设备支持的范围
  3. 最后调整EGL配置,确保上下文与编码器兼容

内容的提问来源于stack exchange,提问作者Aniket Nath

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 13:54:53