Ejemplo n.º 1
0
 async void AlbumButton_Clicked(object sender, RoutedEventArgs e)
 {
     var albumID = (string)(((Button)sender).Tag);
     var dbConnection = new SQLiteAsyncConnection("PlaneAudioU.db");
     playingAlbum = await dbConnection.Table<Album>().Where(a => a.AlbumID.Equals(albumID)).FirstOrDefaultAsync();
     if (playingAlbum == null)
     {
         return;
     }
     PlayTrack(await StorageFile.GetFileFromPathAsync(playingAlbum.TrackPaths[0]));
     albumTitleBlock.Text = playingAlbum.Title + "\n" + playingAlbum.Artist;
     trackPanel.Children.Clear();
     foreach (var title in playingAlbum.Titles)
     {
         var textBlock = new TextBlock();
         textBlock.FontSize = 14;
         textBlock.Text = title;
         trackPanel.Children.Add(textBlock);
     }
     playingTrackNum = 0;
     ((TextBlock)(trackPanel.Children[playingTrackNum])).FontWeight = Windows.UI.Text.FontWeights.Bold;
 }
Ejemplo n.º 2
0
        async Task PickAlbumsFromDir()
        {
            var now = DateTime.Now;
            var albumFolders = await KnownFolders.MusicLibrary.GetFoldersAsync(Windows.Storage.Search.CommonFolderQuery.GroupByArtist);
            foreach (var albumFolder in albumFolders)
            {
                Debug.WriteLine(albumFolder.Name);
                var files = from b in await albumFolder.GetFilesAsync()
                            where !b.Path.EndsWith(".jpg")
                            select b;
                var propDict = (from b in files
                                select new { b.Properties.GetMusicPropertiesAsync().AsTask().Result,
                                             Value = b }
                                ).ToDictionary(b => b.Result, b => b.Value);
                var musicProperties = from b in propDict.Keys
                                      orderby b.TrackNumber
                                      group b by b.Album;
                var albumPool = new List<Album>() { };

                foreach (var tracks in musicProperties)
                {
                    var props = tracks.ToList();
                    var paths = (from b in props
                                 select propDict[b].Path).ToList();
                    var yearDesc = 10000 - props[0].Year;
                    var albumID = $"{props[0].Artist} {yearDesc} {props[0].Album}";

                    var db1 = new SQLiteAsyncConnection("PlaneAudioU.db");
                    var album = await db1
                                      .Table<Album>()
                                      .Where(a => a.AlbumID == albumID)
                                      .FirstOrDefaultAsync();
                    var albumNew = new Album()
                    {
                        AlbumID = albumID,
                        Title = props[0].Album,
                        Artist = props[0].Artist,
                        Titles = props.Select(a => a.Title).ToList(),
                        TrackPaths = paths,
                        Length = props.Count(),
                        Added = now
                    };

                    var db2 = new SQLiteAsyncConnection("PlaneAudioU.db");
                    if (album == null)
                    {

                        await db2.InsertAsync(albumNew);
                    }
                    else
                    {
                        await db2.UpdateAsync(albumNew);
                    }

                    albumPool.Add(albumNew);
                }

                Debug.WriteLine("album pool. " + albumPool.Count.ToString());
                if (albumPool.Count() > 0)
                {
                    var albumToShow = (from a in albumPool
                                       where !shownAlbums.Contains(a.AlbumID)
                                       select a).ToList();
                    if (albumToShow.Count() > 0)
                    {
                        var artistRect = GetArtistRect(albumToShow[0].Artist);
                        albumPanel.Items.Add(artistRect);
                        foreach (var album in albumToShow.OrderBy(a => a.AlbumID))
                        {
                            var button = await album.GetButton();
                            button.Click += AlbumButton_Clicked;
                            albumPanel.Items.Add(button);
                            shownAlbums.Add(album.AlbumID);
                        }
                    }
                }

            }

            Debug.WriteLine("Saving to db.");
            var db3 = new SQLiteAsyncConnection("PlaneAudioU.db");
            foreach (var oldAlbum in await db3.Table<Album>().Where(a => now > a.Added).ToListAsync())
            {
                var db4 = new SQLiteAsyncConnection("PlaneAudioU.db");
                await db4.DeleteAsync(oldAlbum);
            }

        }