private async void LoadImageForSong(Song song)
 {
     if (song != null)
     {
         this.SongImage.Source = await SongImageUtil.LoadImageAsync(song.SongId);
     }
 }
        private async void TracklistPlayer_SongChanged(object sender, SongChangedEventArgs e)
        {
            SystemMediaTransportControlsDisplayUpdater updater = this.systemMediaControls.DisplayUpdater;

            StorageFile file = await SongImageUtil.LoadStorageFileAsync(e.NewSong.SongId);

            if (file == null)
            {
                updater.Thumbnail = RandomAccessStreamReference.CreateFromUri(ImageUtil.GetAssetsImageUriByFileName("Favor.png"));
            }
            else
            {
                updater.Thumbnail = RandomAccessStreamReference.CreateFromFile(file);
            }

            Song currentSong = this.CurrentSong;

            updater.Type = MediaPlaybackType.Music;
            updater.MusicProperties.Title       = currentSong.Title;
            updater.MusicProperties.TrackNumber = (uint)currentSong.Track;
            updater.MusicProperties.AlbumTitle  = currentSong.Album.Name;
            updater.MusicProperties.AlbumArtist = currentSong.Album.Artist.Name;
            updater.MusicProperties.Artist      = currentSong.Album.Artist.Name;

            updater.Update();
        }
        public async Task <ImageSource> LoadImageAsync()
        {
            if (this.anySongs)
            {
                Song firstSong;
                using (MusicLibraryContext dbContext = new MusicLibraryContext())
                {
                    firstSong = dbContext
                                .Songs
                                .Where(song => song.AlbumId == this.data.AlbumId)
                                .First();
                }

                return(await SongImageUtil.LoadImageAsync(firstSong.SongId));
            }

            return(ImageUtil.GetAssetsBitmapImageByFileName("Favor.png"));
        }
Exemple #4
0
        public async Task ReadAsync()
        {
            this.RaiseProgressUpdate(0, null);

            int totalFileCount = await FileUtil.GetFilesCountInAllDirectoriesAsync(this.RootFolder);

            int progressStep          = totalFileCount / 1000;
            int filesAlreadyProcessed = 0;
            int filesAlreadyProcessedTillLastProgressUpdate = progressStep + 1; // set so that we get status update as soon as we start updating the DB

            async Task directorySearch(StorageFolder rootFolder)
            {
                // recursively search through all folders
                IReadOnlyList <StorageFolder> folders = await rootFolder.GetFoldersAsync(Windows.Storage.Search.CommonFolderQuery.DefaultQuery);

                foreach (StorageFolder folder in folders)
                {
                    await directorySearch(folder);
                }

                using (MusicLibraryContext dbContext = new MusicLibraryContext())
                {
                    // get all files in directory
                    foreach (StorageFile file in await rootFolder.GetFilesAsync())
                    {
                        filesAlreadyProcessed++;
                        filesAlreadyProcessedTillLastProgressUpdate++;

                        // continue for sound files only
                        if (allowedExtensions.Contains(file.FileType.ToUpperInvariant()))
                        {
                            Song song;

                            Mp3Stream tagStream = new Id3.Mp3Stream(await file.OpenStreamForReadAsync());
                            bool      anySongTags;
                            Id3Tag[]  songTags = null;
                            try
                            {
                                songTags    = tagStream.GetAllTags();
                                anySongTags = songTags.Length > 0;
                            }
                            catch (Exception e)
                            {
                                anySongTags = false;
                            }

                            if (anySongTags)
                            {
                                Id3Tag songTag = songTags[0];

                                // search for an existing artist by name
                                Artist artist = Artist.CreateOrFind(dbContext, songTag.Artists.Value);

                                // search for an existing album by name and artist
                                Album album = Album.CreateOrFind(dbContext, songTag.Album, artist.ArtistId);

                                // search for an existing song by path
                                song = Song.CreateOrFind(dbContext, file.Path);

                                // set reference to the album
                                song.Album = album;

                                // load other simpler properties
                                if (songTag.Year.IsAssigned)
                                {
                                    song.Year = songTag.Year.AsDateTime.Value.Year;
                                }

                                if (songTag.Track.IsAssigned)
                                {
                                    song.Track = songTag.Track.AsInt.Value;
                                }

                                song.Title   = songTag.Title;
                                song.Genre   = songTag.Genre;
                                song.Comment = string.Join(", ", songTag.Comments);

                                // insert/update to DB
                                dbContext.SaveChanges();

                                // save image to localappdata
                                if (songTag.Pictures.Any())
                                {
                                    await SongImageUtil.SaveImageAsync(song.SongId, songTag.Pictures.First().PictureData, songTag.Pictures.First().MimeType);
                                }
                            }
                            else
                            {
                                song = Song.CreateOrFind(dbContext, file.Path);

                                song.Title = Path.GetFileNameWithoutExtension(file.Path);

                                // insert/update to DB
                                dbContext.SaveChanges();
                            }

                            // update progress
                            if (filesAlreadyProcessedTillLastProgressUpdate > progressStep)
                            {
                                filesAlreadyProcessedTillLastProgressUpdate = 0;
                                this.RaiseProgressUpdate((float)filesAlreadyProcessed / totalFileCount, song);
                            }
                        }
                    }
                }
            };

            if (totalFileCount > 0)
            {
                await directorySearch(this.RootFolder);
            }
        }
 public async Task <ImageSource> LoadImageAsync()
 {
     return(await SongImageUtil.LoadImageAsync(this.data.SongId));
 }