使用AndroidSequenceEncoder时接收“无帧输出”

时间:2017-12-01 07:10:24

标签: java android video runtime-error

我正在尝试使用AndroidSequenceEncoder创建视频幻灯片。为了做到这一点,我有一个方法来检索Bitmaps的ArrayList和一个表示视频文件名的String。以下是我方法的代码:

public class ImageToVideo
{
    //used to input frames (images) into the video file
    private AndroidSequenceEncoder ase;
    //Arraylist containing the frames to input
    private ArrayList<Bitmap> bi;
    //temporary variable used for AsyncTask below
    private Bitmap image;
    //This method is called until main code can be written to retrieve the proper length of the audio file
    public AndroidSequenceEncoder videoEncode(final ArrayList<Bitmap> images, String filename) throws  IOException
    {
        ase = AndroidSequenceEncoder.createSequenceEncoder(new File(filename), 3);
        bi = images;
        Log.d("SEQUENCEENCODER", "SequenceEncoder created");
        //outer for-loop; goes through each image individually
        for(int i = 0; i < images.size(); i++)
        {
            Log.d("SEQUENCEENCODER", "Retrieving image " + i + " of " + (images.size() - 1));
        //inner for-loop; adds the given image a number of times determined by how long each image is on-screen
        //since encoder's fps is currently set to 3, this loops once for each second the picture should be on-screen times 3
        for(int j = 0; j < 3; j++)
        {
            Log.d("SEQUENCEENCODER", "Encoding image " + i + ", " + (j+1) + " of " + 3);
            image = bi.get((i));
            //used because of error "Choreographer: Skipped ~90 frames! The application may be doing too much work on its main thread"
            AsyncTask.execute(new Runnable()
            {
                @Override
                public void run()
                {
                    try
                    {
                        ase.encodeImage(image);
                    }
                    catch(IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    ase.finish();
    return ase;
}

AndroidSequenceEncoder的fps设置为3作为临时值;当我更多地处理代码时,我希望这种方法也能获取视频的长度,以便该方法根据每个图像应该在屏幕上显示的时间来确定fps,因为它们每个都有相同的时间。使用4张图像运行此方法时,我收到以下消息

D/SEQUENCEENCODER: SequenceEncoder created
D/SEQUENCEENCODER: Retrieving image 0 of 3
D/SEQUENCEENCODER: Encoding image 0, 1 of 3
D/SEQUENCEENCODER: Encoding image 0, 2 of 3
D/SEQUENCEENCODER: Encoding image 0, 3 of 3
D/SEQUENCEENCODER: Retrieving image 1 of 3
D/SEQUENCEENCODER: Encoding image 1, 1 of 3
D/SEQUENCEENCODER: Encoding image 1, 2 of 3
D/SEQUENCEENCODER: Encoding image 1, 3 of 3
D/SEQUENCEENCODER: Retrieving image 2 of 3
D/SEQUENCEENCODER: Encoding image 2, 1 of 3
D/SEQUENCEENCODER: Encoding image 2, 2 of 3
D/SEQUENCEENCODER: Encoding image 2, 3 of 3
D/SEQUENCEENCODER: Retrieving image 3 of 3
D/SEQUENCEENCODER: Encoding image 3, 1 of 3
D/SEQUENCEENCODER: Encoding image 3, 2 of 3
D/SEQUENCEENCODER: Encoding image 3, 3 of 3
I/System.out: [WARN]    . (:0): No frames output.
D/EGL_emulation: eglMakeCurrent: 0xa8a05240: ver 2 0 (tinfo 0xa8a03250)
D/EGL_emulation: eglMakeCurrent: 0xa8a05240: ver 2 0 (tinfo 0xa8a03250)
I/chatty: uid=10080(projectname) RenderThread identical 1 line
D/EGL_emulation: eglMakeCurrent: 0xa8a05240: ver 2 0 (tinfo 0xa8a03250)
I/zygote: Do partial code cache collection, code=122KB, data=94KB
I/zygote: After code cache collection, code=117KB, data=92KB
I/zygote: Increasing code cache capacity to 512KB

后面是一长串指向命令的系统错误

ase.encodeImage(image);

新的视频文件确实在Android虚拟设备中创建,但它在播放后几乎立即结束,尽管它说它的长度为4秒。我想知道我在代码中做错了什么导致这个。

1 个答案:

答案 0 :(得分:0)

我在上面的代码中看到的一个问题是帧的实际编码是在一个单独的(后台)线程中进行的。因此,在某些情况下,有可能在调用“完成”时没有实际编码任何帧。 你需要做的是确保在实际发生所有帧的编码后调用'finish'。有多种方法可以实现这一目标:

  1. 完全将你的循环移到异步任务中,然后完成;
  2. 如果只有一个执行程序线程,你也可以在异步任务中执行'finish',这样你就可以依靠内部执行程序队列在最后执行'finish';
  3. 使用线程同步原语,例如'wait / notify'。因此,在主线程中,您可以在调用“完成”之前“等待”,并在异步任务中“通知”。虽然这是一个可行的选择,但我强烈建议您不要因为其他各种原因而使用它。