android 4.1中的releaseEncoder MediaMuxer

时间:2017-05-22 08:19:19

标签: java android mediacodec mediamuxer

我已经为视频制作者创建了一个包含图像的应用程序,我使用MediaMuxer从图像序列创建视频,但SlideEncoder将第二个图像添加到自动关闭MediaMuxer并且应用程序已经粉碎。

05-22 07:58:33.691 6091-6122/com.aspiration.imagetovideomaker E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
        java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:299)
            at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
         Caused by: java.lang.IllegalStateException
            at android.media.MediaCodec.native_configure(Native Method)
            at android.media.MediaCodec.configure(MediaCodec.java:257)
            at com.aspiration.imagetovideomaker.encoding.SlideEncoder.prepareEncoder(SlideEncoder.java:57)
            at com.aspiration.imagetovideomaker.ImageToVideo$EncodingTask.doInBackground(ImageToVideo.java:221)
            at com.aspiration.imagetovideomaker.ImageToVideo$EncodingTask.doInBackground(ImageToVideo.java:205)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
            at java.lang.Thread.run(Thread.java:856) 

这是我的视频创作代码:

 SlideEncoder slideEncoder = new SlideEncoder();

        try {
            slideEncoder.prepareEncoder(outputFile);
            Bitmap prevBm = null;
            dialog.setMax(MyApplication.bitmapList.size());
            for (int idx = 0; idx < MyApplication.bitmapList.size(); idx++) {
                publishProgress(String.valueOf(idx + 1));
                SlideShow.init();

                if (idx > 0) prevBm = MyApplication.bitmapList.get(idx - 1);
                Bitmap curBm = MyApplication.bitmapList.get(idx);
                for (int i = 0; i < (MyApplication.FRAME_PER_SEC * MyApplication.SLIDE_TIME); i++) {
                    // Drain any data from the encoder into the muxer.
                    slideEncoder.drainEncoder(false);

                    // Generate a frame and submit it.
                    slideEncoder.generateFrame(curBm, curBm);
                    //slideEncoder.generateFrame(prevBm, curBm);
                }
            }
            slideEncoder.drainEncoder(true);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            slideEncoder.releaseEncoder();
        }

这里是我的prepareEncoder

 public void prepareEncoder(File outputFile) throws IOException {
    mBufferInfo = new MediaCodec.BufferInfo();

    try {
        MediaFormat format = MediaFormat.createVideoFormat(MIME_TYPE, WIDTH, HEIGHT);
        format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
        format.setInteger(MediaFormat.KEY_BIT_RATE, MyApplication.BIT_RATE);
        format.setInteger(MediaFormat.KEY_FRAME_RATE, MyApplication.FRAME_PER_SEC);
        format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);

        mEncoder = MediaCodec.createEncoderByType(MIME_TYPE);
        mEncoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
        mInputSurface = mEncoder.createInputSurface();
        mEncoder.start();

        mMuxer = new MediaMuxer(outputFile.getPath().toString(), MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);

        mTrackIndex = -1;
        mMuxerStarted = false;
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("SlideEncoder", e.toString());
    }
}

0 个答案:

没有答案