// Returns false is no snapshot generation was required, true otherwise private async Task <Boolean> GenerateThumbnail(VideoItem videoItem) { if (videoItem.IsPictureLoaded) { return(false); } try { if (ContinueIndexing != null) { await ContinueIndexing.Task; ContinueIndexing = null; } 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.Type.ToLower())) { if (await videoItem.LoadFileFromPath()) { thumb = await ThumbsService.GetThumbnail(videoItem.File); } } // If MF thumbnail generation failed or wasn't supported: if (thumb == null) { if (await videoItem.LoadFileFromPath() || !string.IsNullOrEmpty(videoItem.Token)) { var res = await ThumbsService.GetScreenshot(videoItem.GetMrlAndFromType(true).Item2); image = res?.Bitmap(); } } if (thumb == null && image == null) { return(false); } // RunAsync won't await on the lambda it receives, so we need to do it ourselves var tcs = new TaskCompletionSource <bool>(); await DispatchHelper.InvokeAsync(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.IsPictureLoaded = true; tcs.SetResult(true); }); await tcs.Task; videoDatabase.Update(videoItem); return(true); } catch (Exception ex) { LogHelper.Log(ex.ToString()); } return(false); }
public async Task GetCover() { string fileName = Artist + "_" + Name; // fileName needs to be scrubbed of some punctuation. // For example, Windows does not like question marks in file names. fileName = System.IO.Path.GetInvalidFileNameChars().Aggregate(fileName, (current, c) => current.Replace(c, '_')); bool hasFoundCover = false; if (_storageItemThumbnail != null) { var file = await ApplicationData.Current.LocalFolder.CreateFileAsync( fileName + ".jpg", CreationCollisionOption.ReplaceExisting); var raStream = await file.OpenAsync(FileAccessMode.ReadWrite); using (var thumbnailStream = _storageItemThumbnail.GetInputStreamAt(0)) { using (var stream = raStream.GetOutputStreamAt(0)) { await RandomAccessStream.CopyAsync(thumbnailStream, stream); hasFoundCover = true; } } } else { if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) { try { HttpClient lastFmClient = new HttpClient(); var reponse = await lastFmClient.GetStringAsync( string.Format("http://ws.audioscrobbler.com/2.0/?method=album.getinfo&format=json&api_key=a8eba7d40559e6f3d15e7cca1bfeaa1c&artist={0}&album={1}", Artist, Name)); { var albumInfo = JsonConvert.DeserializeObject <AlbumInformation>(reponse); if (albumInfo.Album == null) { return; } if (albumInfo.Album.Image == null) { return; } // Last.FM returns images from small to 'mega', // So try and get the largest image possible. // If we don't get any album art, or can't find the album, return. var largestImage = albumInfo.Album.Image.LastOrDefault(url => !string.IsNullOrEmpty(url.Text)); if (largestImage != null) { hasFoundCover = true; await DownloadAndSaveHelper.SaveAsync( new Uri(largestImage.Text, UriKind.RelativeOrAbsolute), ApplicationData.Current.LocalFolder, fileName + ".jpg"); } } } catch { Debug.WriteLine("Unable to get album Cover from LastFM API"); } } } if (hasFoundCover) { await DispatchHelper.InvokeAsync(() => { Picture = "ms-appdata:///local/" + Artist + "_" + Name + ".jpg"; OnPropertyChanged("Picture"); }); } }
// 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); }