Failed to stop the muxer

2026 views android
0

Hello i'm facing a problem with media muxer. Please help me to solve out this issue... why my app crash ? and what the solutation

java.lang.IllegalStateException: Failed to stop the muxer
    at android.media.MediaMuxer.nativeStop(Native Method)
    at android.media.MediaMuxer.stop(MediaMuxer.java:245)
    at com.app.filter.helper.config.VideoInfoUtils.combineVideo(VideoInfoUtils.java:159)
    at com.app.filter.helper.filter.video.VideoFilterView.handleSaveCompleted(VideoFilterView.java:74)
    at com.app.filter.helper.filter.video.VideoFilterView$ViewHandler.handleMessage(VideoFilterView.java:167)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)

and my java code is

    public class VideoInfoUtils {

    public static MediaFormat getVideoInfo(String path) {

        MediaExtractor audioExtractor = new MediaExtractor();
        try {
            audioExtractor.setDataSource(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int selectTrack = selectTrack(audioExtractor);
        if (selectTrack < 0) {
            throw new RuntimeException("No video track found in " + path);
        }
        MediaFormat format = audioExtractor.getTrackFormat(selectTrack);
        return format;

    }

    /**
     * Selects the video track, if any.
     *
     * @return the track index, or -1 if no video track is found.
     */
    private static int selectTrack(MediaExtractor mMediaExtractor) {
        // Select the first video track we find, ignore the rest.
        int numTracks = mMediaExtractor.getTrackCount();
        for (int i = 0; i < numTracks; i++) {
            MediaFormat format = mMediaExtractor.getTrackFormat(i);
            String mime = format.getString(MediaFormat.KEY_MIME);
            if (mime.startsWith("video/")) {
                return i;
            }
        }

        return -1;
    }


    @SuppressLint("NewApi")
    public static void combineVideo(String videoPath, String newVideo, String output) throws IllegalArgumentException {
        try {
            MediaExtractor videoExtractor = new MediaExtractor();
            videoExtractor.setDataSource(newVideo);
            MediaFormat videoFormat = null;
            int videoTrackIndex = -1;
            int videoTrackCount = videoExtractor.getTrackCount();
            for (int i = 0; i < videoTrackCount; i++) {
                videoFormat = videoExtractor.getTrackFormat(i);
                String mimeType = videoFormat.getString(MediaFormat.KEY_MIME);
                if (mimeType.startsWith("video/")) {
                    videoTrackIndex = i;
                    break;
                }
            }

            MediaExtractor audioExtractor = new MediaExtractor();
            audioExtractor.setDataSource(videoPath);
            MediaFormat audioFormat = null;
            int audioTrackIndex = -1;
            int audioTrackCount = audioExtractor.getTrackCount();
            for (int i = 0; i < audioTrackCount; i++) {
                audioFormat = audioExtractor.getTrackFormat(i);
                String mimeType = audioFormat.getString(MediaFormat.KEY_MIME);
                if (mimeType.startsWith("audio/")) {
                    audioTrackIndex = i;
                    break;
                }
            }
            videoExtractor.selectTrack(videoTrackIndex);
            audioExtractor.selectTrack(audioTrackIndex);

            MediaCodec.BufferInfo videoBufferInfo = new MediaCodec.BufferInfo();
            MediaCodec.BufferInfo audioBufferInfo = new MediaCodec.BufferInfo();
            MediaMuxer mediaMuxer;
            int writeVideoTrackIndex;
            int writeAudioTrackIndex;
            try {
                mediaMuxer = new MediaMuxer(output, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
                writeVideoTrackIndex = mediaMuxer.addTrack(videoFormat);
                writeAudioTrackIndex = mediaMuxer.addTrack(audioFormat);
                mediaMuxer.start();
            } catch (IOException ioe) {
                throw new RuntimeException("MediaMuxer creation failed", ioe);
            }

            ByteBuffer byteBuffer = ByteBuffer.allocate(500 * 1024);
            long videoStampTime = 0;
            {
                videoExtractor.readSampleData(byteBuffer, 0);
                if (videoExtractor.getSampleFlags() == MediaExtractor.SAMPLE_FLAG_SYNC) {
                    videoExtractor.advance();
                }
                videoExtractor.readSampleData(byteBuffer, 0);
                long secondTime = videoExtractor.getSampleTime();
                videoExtractor.advance();
                long thirdTime = videoExtractor.getSampleTime();
                videoStampTime = Math.abs(thirdTime - secondTime);
            }
            videoExtractor.unselectTrack(videoTrackIndex);
            videoExtractor.selectTrack(videoTrackIndex);

            long 

answered question

1 Answer

4

try by changing your code like below. You need to release video extractor as well

if (mediaMuxer != null) {

                videoExtractor.release();
                videoExtractor  = null;
                mediaMuxer.stop();
                mediaMuxer.release();
            }

posted this

Have an answer?

JD

Please login first before posting an answer.