コード例 #1
0
        public static async Task <bool> RemoveFromFavorites(string path)
        {
            try
            {
                StorageFile favoritesFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("Favorites.xml", CreationCollisionOption.OpenIfExists);

                XmlDocument DOC = new XmlDocument();

                string xml = await FileIO.ReadTextAsync(favoritesFile);

                if (string.IsNullOrWhiteSpace(xml))
                {
                    return(false);
                }

                DOC.LoadXml(xml);

                IXmlNode node = DOC.SelectSingleNode("/Playlist/Song[@Path=\"" + path + "\"]");

                if (node == null)
                {
                    return(false);
                }

                IXmlNode parent = node.ParentNode;

                if (parent == null)
                {
                    return(false);
                }

                // remove the child node
                parent.RemoveChild(node);


                await DOC.SaveToFileAsync(favoritesFile);

                return(true);
            }
            catch
            {
                return(false);
            }
        }
コード例 #2
0
        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;
        }