Android MediaExtractor readSampleData IllegalArgumentException

时间:2015-10-15 12:30:22

标签: android mediacodec mediamuxer mediaextractor

我尝试关注此问题Concatenate multiple mp4 audio files using android´s MediaMuxer并连接视频文件。

但由于某种原因,当我调用readSampleData时,MediaExtractor会抛出IllegalArgumentException

在官方API中,此功能根本不应该抛出任何异常!发生了什么事?

我发现了一个旧问题,表明ByteBuffer的大小可能是负责的:Android MediaMuxer readSampleData IllegalStateException

我已经尝试了大量的大小值,但没有一个让问题消失。我应该知道标准尺寸吗?

任何提示都有帮助!

    boolean VERBOSE = true;


private boolean concatenateFiles(File dst, List<File> sources) {
    if ((sources == null) || (sources.size() == 0)) {
        return false;
    }

    boolean result;
    MediaExtractor extractor = null;
    MediaMuxer muxer = null;
    try {
        // Set up MediaMuxer for the destination.
        muxer = new MediaMuxer(dst.getPath(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

        // Copy the samples from MediaExtractor to MediaMuxer.
        boolean sawEOS = false;

        int frameCount = 0;
        int offset = 100;

        ByteBuffer dstBuf = ByteBuffer.allocate(256 * 1024);
        BufferInfo bufferInfo = new BufferInfo();

        long timeOffsetUs = 0;
        int dstTrackIndex = -1;

        for (int fileIndex = 0; fileIndex < sources.size(); fileIndex++) {
            int numberOfSamplesInSource = getNumberOfSamples(sources.get(fileIndex));
            if (VERBOSE) {
                Log.d(TAG, String.format("Source file: %s", sources.get(fileIndex).getPath()));
            }

            if (!sources.get(fileIndex).canRead()) {
                throw new FileNotFoundException("Unable to read " + sources.get(fileIndex));
            }

            // Set up MediaExtractor to read from the source.
            extractor = new MediaExtractor();
            extractor.setDataSource(sources.get(fileIndex).getPath());

            // Set up the tracks.
            SparseIntArray indexMap = new SparseIntArray(extractor.getTrackCount());
            for (int i = 0; i < extractor.getTrackCount(); i++) {
                extractor.selectTrack(i);
                MediaFormat format = extractor.getTrackFormat(i);
                if (dstTrackIndex < 0) {
                    dstTrackIndex = muxer.addTrack(format);
                    muxer.start();
                }
                indexMap.put(i, dstTrackIndex);
            }

            long lastPresentationTimeUs = 0;
            int currentSample = 0;

            while (!sawEOS) {
                bufferInfo.offset = offset;

                bufferInfo.size = extractor.readSampleData(dstBuf, offset);


                if (bufferInfo.size < 0) {
                    sawEOS = true;
                    bufferInfo.size = 0;
                    timeOffsetUs += (lastPresentationTimeUs);
                }
                else {
                    lastPresentationTimeUs = extractor.getSampleTime();
                    bufferInfo.presentationTimeUs = extractor.getSampleTime() + timeOffsetUs;
                    bufferInfo.flags = extractor.getSampleFlags();
                    int trackIndex = extractor.getSampleTrackIndex();

                    if ((currentSample < numberOfSamplesInSource) || (fileIndex == sources.size() - 1)) {
                        muxer.writeSampleData(indexMap.get(trackIndex), dstBuf, bufferInfo);
                    }
                    extractor.advance();

                    frameCount++;
                    currentSample++;
                    if (VERBOSE) {
                        Log.d(TAG, "Frame (" + frameCount + ") " +
                                "PresentationTimeUs:" + bufferInfo.presentationTimeUs +
                                " Flags:" + bufferInfo.flags +
                                " TrackIndex:" + trackIndex +
                                " Size(KB) " + bufferInfo.size / 1024);
                    }
                }
            }
            extractor.release();
            extractor = null;
        }

        result = true;
    }
    catch (Exception e) {
        e.printStackTrace();
        result = false;
    }
    finally {
        if (extractor != null) {
            extractor.release();
        }
        if (muxer != null) {
            muxer.stop();
            muxer.release();
        }
    }
    return result;
}

1 个答案:

答案 0 :(得分:5)

解码4k电影时遇到此错误。其他人(包括其他非常高分辨率的电影)一直工作得很好,但大约在一半的时间里 - 在一个更复杂的场景附近 - 我得到了这个错误。

我的缓冲区大小为1mb(1024 * 1024),增加到2mb,问题就消失了。

所以你在源代码中找到的END_OF_STREAM大概是&#34;缓冲区的结尾&#34;