MediaMuxer+MediaCodec生成MP4视频报错

项目使用MediaMuxer+MediaCodec录制视频时遇到崩溃问题,错误源于`pushBuffer called before start`。分析源码发现,可能由于文件大小超过FAT32格式的4GB限制,而MediaMuxer未设置为64位文件偏移,导致录制中断。解决方案可能涉及设置最大文件大小和启用64位文件偏移。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目中遇到一个问题,现象是录制视频时,录制一段时间后应用会crash。我们项目中是用MediaMuxer+MediaCodec来录制,最后生成一个MP4格式视频。
关键报错信息如下:

E/AndroidRuntime(23507): java.lang.IllegalStateException: writeSampleData returned an error
E/AndroidRuntime(23507):    at android.media.MediaMuxer.nativeWriteSampleData(Native Method)
E/AndroidRuntime(23507):    at android.media.MediaMuxer.writeSampleData(MediaMuxer.java:473)

可以看到是native报错,直接去找项目源码(https://android.googlesource.com/platform/frameworks/av/ 这里面可以看到framework的代码):

status_t MediaMuxer::writeSampleData(const sp<ABuffer> &buffer, size_t trackIndex,
                                     int64_t timeUs, uint32_t flags) {
    Mutex::Autolock autoLock(mMuxerLock);
    if (buffer.get() == NULL) {
        ALOGE("WriteSampleData() get an NULL buffer.");
        return -EINVAL;
    }
    if (mState != STARTED) {
        ALOGE("WriteSampleData() is called in invalid state %d", mState);
        return INVALID_OPERATION;
    }
    if (trackIndex >= mTrackList.size()) {
        ALOGE("WriteSampleData() get an invalid index %zu", trackIndex);
        return -EINVAL;
    }
    MediaBuffer* mediaBuffer = new MediaBuffer(buffer);
    mediaBuffer->add_ref(); // Released in MediaAdapter::signalBufferReturned().
    mediaBuffer->set_range(buffer->offset(), buffer->size());
    sp<MetaData> sampleMetaData = mediaBuffer->meta_data();
    sampleMetaData->setInt64(kKeyTime, timeUs);
    // Just set the kKeyDecodingTime as the presentation time for 
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值