Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java “接收”;“无帧输出”;使用AndroidSequenceEncoder时_Java_Android_Video_Runtime Error - Fatal编程技术网

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

Java “接收”;“无帧输出”;使用AndroidSequenceEncoder时,java,android,video,runtime-error,Java,Android,Video,Runtime Error,我正在尝试使用AndroidSequenceEncoder创建视频幻灯片。为了做到这一点,我有一个方法可以检索位图的ArrayList和表示视频文件名的字符串。以下是我的方法的代码: public class ImageToVideo { //used to input frames (images) into the video file private AndroidSequenceEncoder ase; //Arraylist containing the fra

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

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;
}
后面是一长串指向该命令的系统错误

ase.encodeImage(image);

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

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

  • 将循环完全移动到异步任务中,然后完成
  • 如果只有一个执行器线程,那么您也可以在异步任务中执行'finish',这样您就可以依靠内部执行器队列在最后执行'finish'
  • 使用线程同步原语,如“等待/通知”。因此,在主线程中,您可以在调用“finish”之前“wait”,在异步任务中,您可以“notify”。虽然这是一个可行的选择,但我强烈反对你出于其他各种原因使用它
  • ase.encodeImage(image);