Example #1
0
        public void CreateVideoFromByteArray(string filePath, int width, int height, int frameRate)
        {
            byte[]   frameData   = new byte[width * height * 4];
            GCHandle frameHandle = GCHandle.Alloc(frameData, GCHandleType.Pinned);

            // Start the recording session
            int encoderHandle = NativePlugin.CreateRecorderAVI(filePath, (uint)width, (uint)height, frameRate, (int)NativePlugin.PixelFormat.RGBA32, false, _videoCodecIndex, false, 0, 0, -1, -1, false, false, false);

            if (encoderHandle >= 0)
            {
                NativePlugin.Start(encoderHandle);

                // Write out 100 frames
                int numFrames = 100;
                for (int i = 0; i < numFrames; i++)
                {
                    // TODO: fill the byte array with your own data :)


                    // Wait for the encoder to be ready for the next frame
                    int numAttempts = 32;
                    while (numAttempts > 0)
                    {
                        if (NativePlugin.IsNewFrameDue(encoderHandle))
                        {
                            // Encode the new frame
                            NativePlugin.EncodeFrame(encoderHandle, frameHandle.AddrOfPinnedObject());
                            break;
                        }
                        System.Threading.Thread.Sleep(1);
                        numAttempts--;
                    }
                }

                // End the session
                NativePlugin.Stop(encoderHandle, false);
                NativePlugin.FreeRecorder(encoderHandle);
            }

            if (frameHandle.IsAllocated)
            {
                frameHandle.Free();
            }
        }