Example #1
0
        // Returns false is no snapshot generation was required, true otherwise
        public static async Task <Boolean> GenerateThumbnail(VideoItem videoItem)
        {
            if (videoItem.HasThumbnail)
            {
                return(false);
            }
            try
            {
                if (Locator.MediaPlaybackViewModel.ContinueIndexing != null)
                {
                    await Locator.MediaPlaybackViewModel.ContinueIndexing.Task;
                    Locator.MediaPlaybackViewModel.ContinueIndexing = null;
                }
#if WINDOWS_PHONE_APP
                if (MemoryUsageHelper.PercentMemoryUsed() > MemoryUsageHelper.MaxRamForResourceIntensiveTasks)
                {
                    return(false);
                }
#endif
                WriteableBitmap      image = null;
                StorageItemThumbnail thumb = null;
                // If file is a mkv, we save the thumbnail in a VideoPic folder so we don't consume CPU and resources each launch
                if (VLCFileExtensions.MFSupported.Contains(videoItem.File.FileType.ToLower()))
                {
                    thumb = await ThumbsService.GetThumbnail(videoItem.File).ConfigureAwait(false);
                }
                // If MF thumbnail generation failed or wasn't supported:
                if (thumb == null)
                {
                    var res = await ThumbsService.GetScreenshot(videoItem.File).ConfigureAwait(false);

                    if (res == null)
                    {
                        return(true);
                    }
                    image = res.Bitmap();
                    await App.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => videoItem.Duration = TimeSpan.FromMilliseconds(res.Length()));
                }
                if (thumb != null || image != null)
                {
                    // RunAsync won't await on the lambda it receives, so we need to do it ourselves
                    var tcs = new TaskCompletionSource <bool>();
                    await App.Dispatcher.RunAsync(CoreDispatcherPriority.Low, async() =>
                    {
                        if (thumb != null)
                        {
                            image = new WriteableBitmap((int)thumb.OriginalWidth, (int)thumb.OriginalHeight);
                            await image.SetSourceAsync(thumb);
                        }
                        await DownloadAndSaveHelper.WriteableBitmapToStorageFile(image, videoItem.Id.ToString());
                        videoItem.ThumbnailPath = String.Format("{0}{1}.jpg", Strings.VideoPicFolderPath, videoItem.Id);
                        tcs.SetResult(true);
                    });

                    await tcs.Task;
                }
                videoItem.HasThumbnail = true;
                return(true);
            }
            catch (Exception ex)
            {
                LogHelper.Log(ex.ToString());
            }
            return(false);
        }