public static async void CheckCollection() { LoadCollectionCompleted = false; StorageFolder musicFolder = KnownFolders.MusicLibrary; StorageFolder coversFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Covers", CreationCollisionOption.OpenIfExists); QueryOptions options = new QueryOptions(); options.FileTypeFilter.Add(".mp3"); options.FileTypeFilter.Add(".wma"); options.FileTypeFilter.Add(".aac"); options.FileTypeFilter.Add(".m4a"); options.FolderDepth = FolderDepth.Deep; var songs = await musicFolder.CreateFileQueryWithOptions(options).GetFilesAsync(); Stream fileStream = null; TagLib.File tagFile = null; Song aux = null; string title = ""; string artist = ""; string album = ""; string genre = ""; string trackNumber = ""; string year = ""; int totalCount = songs.Count; double currentValue = 0; double percentagePerItem = Convert.ToDouble(100) / Convert.ToDouble(totalCount); List <Song> listOfSongs = new List <Song>(); XmlDocument DOC = new XmlDocument(); XmlElement mainElem = DOC.DocumentElement; XmlElement ELE = DOC.CreateElement("Collection"); DOC.AppendChild(ELE); XmlElement x = null; foreach (StorageFile file in songs) { try { x = DOC.CreateElement("Song"); fileStream = await file.OpenStreamForReadAsync(); tagFile = TagLib.File.Create(new StreamFileAbstraction(file.Name, fileStream, fileStream)); aux = new Song(); if (tagFile.Tag.Album != null || string.IsNullOrWhiteSpace(tagFile.Tag.Album) == false) { album = tagFile.Tag.Album; } else { album = "Unknown"; } if (tagFile.Tag.Title == null || string.IsNullOrWhiteSpace(tagFile.Tag.Title)) { title = file.DisplayName; } else { title = tagFile.Tag.Title; } if (tagFile.Tag.FirstAlbumArtist != null && string.IsNullOrWhiteSpace(tagFile.Tag.FirstAlbumArtist) == false) { artist = tagFile.Tag.FirstAlbumArtist; } else if (tagFile.Tag.FirstPerformer != null && string.IsNullOrWhiteSpace(tagFile.Tag.FirstPerformer) == false) { artist = tagFile.Tag.FirstPerformer; } else { artist = "Unknown"; } trackNumber = Convert.ToString(tagFile.Tag.Track); year = Convert.ToString(tagFile.Tag.Year); if (tagFile.Tag.FirstGenre != null && string.IsNullOrWhiteSpace(tagFile.Tag.FirstGenre) == false) { genre = tagFile.Tag.FirstGenre; } else { genre = string.Empty; } var matchingResults = listOfSongs.Where(s => s.GetArtist() == artist && s.GetAlbum() == album).ToList(); string albumid; string hexColor = string.Empty; if (matchingResults.Count() > 0) { albumid = matchingResults[0].GetAlbumID(); hexColor = matchingResults[0].HexColor; } else { albumid = Guid.NewGuid().ToString(); if (tagFile.Tag.Pictures.Length > 0) { var dataVector = tagFile.Tag.Pictures[0].Data.Data; SaveCoverImageResult result = await ImageHelper.SaveAlbumCover(dataVector, albumid); hexColor = result.HexColor; //ImageHelper.BlurAlbumCover(albumid); } } string songid = Guid.NewGuid().ToString(); x.SetAttribute("Artist", artist); x.SetAttribute("Title", title); x.SetAttribute("Album", album); x.SetAttribute("Path", file.Path); x.SetAttribute("Genre", genre); x.SetAttribute("Year", year); x.SetAttribute("Track", trackNumber); x.SetAttribute("AlbumID", albumid); x.SetAttribute("ID", Guid.NewGuid().ToString()); x.SetAttribute("HexColor", hexColor); aux.Set(title, artist, album, albumid, songid, year, trackNumber, genre, file.Path, hexColor); listOfSongs.Add(aux); DOC.FirstChild.AppendChild(x); fileStream.Dispose(); } catch { } currentValue = currentValue + percentagePerItem; if (PageHelper.PreparingCollection != null) { PageHelper.PreparingCollection.UpdateStatus(Convert.ToInt32(currentValue)); } else if (PageHelper.SetupWizard != null) { PageHelper.SetupWizard.UpdateStatus(Convert.ToInt32(currentValue)); } } StorageFolder collectionFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Collection", CreationCollisionOption.OpenIfExists); StorageFile collectionFile = await collectionFolder.CreateFileAsync("Collection.xml", CreationCollisionOption.ReplaceExisting); await DOC.SaveToFileAsync(collectionFile); await CollectionHelper.LoadCollectionFile(); LoadCollectionCompleted = true; if (PageHelper.PreparingCollection != null) { PageHelper.PreparingCollection.LoadingCompleted(); } else if (PageHelper.SetupWizard != null) { PageHelper.SetupWizard.LoadingCompleted(); } }
public static async Task <SaveCoverImageResult> SaveAlbumCover(byte[] dataVector, string albumid) { var displayInformation = DisplayInformation.GetForCurrentView(); StorageFolder coversFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Covers", CreationCollisionOption.OpenIfExists); SaveCoverImageResult result; string hexColor = string.Empty; SaveCoverImageResult.Result resultState; if (dataVector != null) { try { MemoryStream ms = new MemoryStream(dataVector); ms.Position = 0; StorageFile f = await coversFolder.CreateFileAsync("cover_" + albumid + ".jpg", CreationCollisionOption.ReplaceExisting); IRandomAccessStream ras = ms.AsRandomAccessStream(); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(ras); double oldHeight = decoder.PixelHeight; double oldWidth = decoder.PixelWidth; double newWidth = 500; if (oldWidth < 500) { newWidth = oldWidth; } double newHeight = (oldHeight * newWidth) / oldWidth; BitmapTransform transform = new BitmapTransform() { ScaledWidth = (uint)newWidth, ScaledHeight = (uint)newHeight, InterpolationMode = BitmapInterpolationMode.NearestNeighbor }; PixelDataProvider pixelData = await decoder.GetPixelDataAsync( BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage); using (var destinationStream = await f.OpenAsync(FileAccessMode.ReadWrite)) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, destinationStream); encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied, (uint)newWidth, (uint)newHeight, displayInformation.RawDpiX, displayInformation.RawDpiY, pixelData.DetachPixelData()); await encoder.FlushAsync(); } hexColor = await ImageHelper.GetDominantColorHex(ras); ras.Dispose(); ms.Dispose(); resultState = SaveCoverImageResult.Result.Succeded; } catch { resultState = SaveCoverImageResult.Result.Failed; } } else { resultState = SaveCoverImageResult.Result.Failed; } result = new SaveCoverImageResult(hexColor, resultState); return(result); }
public static async void LoadCollectionChanges() { if (LoadCollectionCompleted == false) { return; } Debug.WriteLine("INICIANDO BUSCA"); //if (ApplicationInfo.Current.IsMobile) //{ // StatusBar.GetForCurrentView().ProgressIndicator.ProgressValue = null; // StatusBar.GetForCurrentView().ProgressIndicator.Text = "Looking for your music..."; //} LoadCollectionCompleted = false; bool changed = false; if (CollectionHelper.IsCollectionLoaded) { List <string> currentSongs = CollectionHelper.GetAllSongsPaths(); List <string> folderSongs = new List <string>(); if (currentSongs != null) { List <Song> listOfSongs = new List <Song>(); Song aux = null; foreach (XmlElement element in GetAllSongs(false)) { aux = new Song(); aux.Set(element.GetAttribute("Title"), element.GetAttribute("Artist"), element.GetAttribute("Album"), element.GetAttribute("AlbumID"), element.GetAttribute("ID"), element.GetAttribute("Year"), element.GetAttribute("Track"), element.GetAttribute("Genre"), element.GetAttribute("Path"), element.GetAttribute("HexColor")); listOfSongs.Add(aux); } StorageFolder musicFolder = KnownFolders.MusicLibrary; StorageFolder coversFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Covers", CreationCollisionOption.OpenIfExists); QueryOptions options = new QueryOptions(); options.FileTypeFilter.Add(".mp3"); options.FileTypeFilter.Add(".wma"); options.FileTypeFilter.Add(".aac"); options.FileTypeFilter.Add(".m4a"); options.FolderDepth = FolderDepth.Deep; var songs = await musicFolder.CreateFileQueryWithOptions(options).GetFilesAsync(); XmlElement x = null; Stream fileStream = null; TagLib.File tagFile = null; string title = ""; string artist = ""; string album = ""; string genre = ""; string trackNumber = ""; string year = ""; int totalCount = songs.Count; foreach (StorageFile file in songs) { folderSongs.Add(file.Path); } foreach (StorageFile file in songs) { if (file.FileType == ".mp3" || file.FileType == ".wma" || file.FileType == ".aac" || file.FileType == ".m4a") { if (currentSongs.Contains(file.Path) == false) { try { x = CollectionDocument.CreateElement("Song"); fileStream = await file.OpenStreamForReadAsync(); tagFile = TagLib.File.Create(new StreamFileAbstraction(file.Name, fileStream, fileStream)); aux = new Song(); if (tagFile.Tag.Album != null || string.IsNullOrWhiteSpace(tagFile.Tag.Album) == false) { album = tagFile.Tag.Album; } else { album = "Unknown"; } if (tagFile.Tag.Title == null || string.IsNullOrWhiteSpace(tagFile.Tag.Title)) { title = file.DisplayName; } else { title = tagFile.Tag.Title; } if (tagFile.Tag.FirstAlbumArtist != null && string.IsNullOrWhiteSpace(tagFile.Tag.FirstAlbumArtist) == false) { artist = tagFile.Tag.FirstAlbumArtist; } else if (tagFile.Tag.FirstPerformer != null && string.IsNullOrWhiteSpace(tagFile.Tag.FirstPerformer) == false) { artist = tagFile.Tag.FirstPerformer; } else { artist = "Unknown"; } trackNumber = Convert.ToString(tagFile.Tag.Track); year = Convert.ToString(tagFile.Tag.Year); if (tagFile.Tag.FirstGenre != null && string.IsNullOrWhiteSpace(tagFile.Tag.FirstGenre) == false) { genre = tagFile.Tag.FirstGenre; } else { genre = string.Empty; } var matchingResults = listOfSongs.Where(s => s.Artist == artist && s.Album == album).ToList(); string albumid; string hexColor = string.Empty; if (matchingResults.Count() > 0) { albumid = matchingResults[0].AlbumID; hexColor = matchingResults[0].HexColor; } else { albumid = Guid.NewGuid().ToString(); if (tagFile.Tag.Pictures.Length > 0) { var dataVector = tagFile.Tag.Pictures[0].Data.Data; SaveCoverImageResult result = await ImageHelper.SaveAlbumCover(dataVector, albumid); hexColor = result.HexColor; //ImageHelper.BlurAlbumCover(albumid); } } string songid = Guid.NewGuid().ToString(); x.SetAttribute("Artist", artist); x.SetAttribute("Title", title); x.SetAttribute("Album", album); x.SetAttribute("Path", file.Path); x.SetAttribute("Genre", genre); x.SetAttribute("Year", year); x.SetAttribute("Track", trackNumber); x.SetAttribute("AlbumID", albumid); x.SetAttribute("ID", songid); x.SetAttribute("HexColor", hexColor); aux.Set(title, artist, album, albumid, songid, year, trackNumber, genre, file.Path, hexColor); CollectionDocument.FirstChild.AppendChild(x); fileStream.Dispose(); changed = true; listOfSongs.Add(aux); } catch { } } } } foreach (string file in currentSongs) { if (folderSongs.Contains(file) == false) { IXmlNode node = CollectionDocument.SelectSingleNode("/Collection/Song[@Path=\"" + file + "\"]"); IXmlNode parent = node.ParentNode; // remove the child node parent.RemoveChild(node); changed = true; } } StorageFolder collectionFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Collection", CreationCollisionOption.OpenIfExists); StorageFile collectionFile = await collectionFolder.CreateFileAsync("Collection.xml", CreationCollisionOption.ReplaceExisting); await CollectionDocument.SaveToFileAsync(collectionFile); await CollectionHelper.LoadCollectionFile(); if (changed) { if (PageHelper.Artists != null) { PageHelper.Artists.CollectionHasBeenUpdated = true; } if (PageHelper.Albums != null) { PageHelper.Albums.CollectionHasBeenUpdated = true; } if (PageHelper.Songs != null) { PageHelper.Songs.CollectionHasBeenUpdated = true; } } } } Debug.WriteLine("BUSCA CONCLUÍDA"); if (ApplicationInfo.Current.IsMobile) { StatusBar.GetForCurrentView().ProgressIndicator.ProgressValue = 0; StatusBar.GetForCurrentView().ProgressIndicator.Text = ""; } LoadCollectionCompleted = true; }