azchohfi / LottieUWP

UWP port of Lottie(https://github.com/airbnb/lottie-android)
Apache License 2.0
274 stars 45 forks source link

can't pause and set progress value manually #47

Open Hanzalah-Adalan opened 5 years ago

Hanzalah-Adalan commented 5 years ago

Unspecified error

Attempting to close a CanvasActiveLayer that is not top of the stack. The most recently created layer must be closed first.

at System.Runtime.InteropServices.WindowsRuntime.IClosable.Close()
at LottieUWP.LottieDrawable.Draw(CanvasDevice device, BitmapCanvas bitmapCanvas, CompositionLayer compositionLayer, Rect bounds, Single scale, Byte alpha, Matrix3X3 matrix, Double width, Double height, CanvasDrawingSession canvasDrawingSession)
at LottieUWP.LottieDrawable.CanvasControlOnDraw(ICanvasAnimatedControl canvasControl, CanvasAnimatedDrawEventArgs args)
Hanzalah-Adalan commented 5 years ago

currently here's my noob solution: -> Reload the Json file altogether to start fresh otherwise I got those canvas's IClosable, IDisposable error stuffs..

 public async void PlayLottieDownload()
        {
            await LottieDownloadAnimation.SetAnimationAsync("Assets/Lottie/file_download.json");
            LottieDownloadAnimation.AutoPlay = true;
            LottieDownloadAnimation.PlayAnimation();
        }

        public async void StopLottieDownload()
        {
            await LottieDownloadAnimation.SetAnimationAsync("Assets/Lottie/file_download.json");
            LottieDownloadAnimation.Progress = 0.5f;
        }
azchohfi commented 5 years ago

What code did you use to try to pause and set the progress? And were that being called from the UI thread?

Hanzalah-Adalan commented 5 years ago

I try to pause & set new progress value while it is active (animating) triggered by AutoPlay property set to True on App launch..

and Yes, that were being called from the UI thread.

I guess this is because there are two separate animations going on in this scenario, one from the AutoPlay trigger and another one from method call to pause and set the new value..

HHChaos commented 5 years ago

I also found this problem, some lottie files will crash if you change the Slider Value on LottieUWP.Sample. This bug is related to the CurrentDrawingSession of BitmapCanvas. I modified the Progress setter of LottieDrawable to avoid this bug. @azchohfi If a crash Lottie file is needed, I can provide it.

code in LottieDrawable .cs

    public float Progress
    {
        get => _animator.AnimatedValueAbsolute;
        set
        {
            if (_composition == null)
            {
                _lazyCompositionTasks.Add(c => { Progress = value; });
                return;
            }
            //added code
            lock (this)
            {
                _bitmapCanvas?.Dispose();
                _bitmapCanvas = new BitmapCanvas(Width, Height);
            }

            Frame = MiscUtils.Lerp(_composition.StartFrame, _composition.EndFrame, value);
        }
    }
azchohfi commented 5 years ago

You are disposing the bitmapcanvas every frame change? I don't think that is a good solution.

HHChaos commented 5 years ago
      You are disposing the bitmapcanvas every frame change? I don't think that is a good solution.

The Progress setter of LottieDrawable is called only you changed the Slider value manually,and I didn't solve the bug, I just found a way to avoid it.