private async void OnSourceChanged(IVideoAnimationSource newValue, IVideoAnimationSource oldValue) { var canvas = _canvas; if (canvas == null) { return; } if (newValue == null) { _source = null; Subscribe(false); return; } if (newValue?.Id == oldValue?.Id || newValue?.Id == _source?.Id) { return; } _source = newValue; var shouldPlay = _shouldPlay; var animation = await Task.Run(() => VideoAnimation.LoadFromFile(newValue, false, true)); if (animation == null || newValue?.Id != _source?.Id) { // The app can't access the file specified return; } if (_shouldPlay) { shouldPlay = true; } _animation = animation; _bitmap = null; if (AutoPlay || _shouldPlay) { _shouldPlay = false; Subscribe(true); } else if (!_unloaded) { Subscribe(false); if (_canvas.ReadyToDraw) { var buffer = ArrayPool<byte>.Shared.Rent(_animation.PixelWidth * _animation.PixelHeight * 4); _bitmap = CanvasBitmap.CreateFromBytes(_canvas, buffer, _animation.PixelWidth, _animation.PixelHeight, DirectXPixelFormat.R8G8B8A8UIntNormalized); ArrayPool<byte>.Shared.Return(buffer); } // Invalidate to render the first frame await Task.Run(Invalidate); _canvas?.Invalidate(); } }
protected override void Dispose() { if (_animation != null && !_animation.IsCaching) { _animation.Dispose(); } _source = null; _animation = null; }
private void OnUnloaded(object sender, RoutedEventArgs e) { _shouldPlay = false; _unloaded = true; Subscribe(false); if (_canvas != null) { _canvas.Draw -= OnDraw; _canvas.RemoveFromVisualTree(); _canvas = null; } _source = null; //_bitmap?.Dispose(); _bitmap = null; //_animation?.Dispose(); _animation = null; }
private async void OnSourceChanged(IVideoAnimationSource newValue, IVideoAnimationSource oldValue) { //var canvas = _canvas; //if (canvas == null && !Load()) //{ // return; //} if (newValue == null) { Unload(); return; } if (newValue?.Id == oldValue?.Id || newValue?.Id == _source?.Id) { return; } _source = newValue; var animation = await Task.Run(() => CachedVideoAnimation.LoadFromFile(newValue, 0, 0, _isCachingEnabled)); if (animation == null || newValue?.Id != _source?.Id) { // The app can't access the file specified Client.Execute(new AddLogMessage(5, $"Can't load animation for playback: {newValue.FilePath}")); return; } var frameRate = Math.Clamp(animation.FrameRate, 1, _isCachingEnabled ? 30 : 60); _interval = TimeSpan.FromMilliseconds(1000d / frameRate); _animation = animation; _hideThumbnail = null; OnSourceChanged(); }