private Task<List<LocalAudio>> GetFromLibrary()
        {
            return Task.Run(async () =>
            {
                var musicFiles = FilesHelper.GetMusicFiles();

                double totalCount = musicFiles.Count;

                var tracks = new List<LocalAudio>();

                foreach (var filePath in musicFiles)
                {
                    TagLib.File audioFile = null;

                    try
                    {
                        audioFile = TagLib.File.Create(filePath);
                    }
                    catch (Exception ex)
                    {
                        LoggingService.Log(ex);
                        continue;
                    }

                    var track = new LocalAudio();
                    track.Id = Md5Helper.Md5(filePath);
                    if (!string.IsNullOrEmpty(audioFile.Tag.Title))
                        track.Title = StringHelper.ToUtf8(audioFile.Tag.Title);
                    else
                        track.Title = Path.GetFileNameWithoutExtension(filePath);

                    var artist = audioFile.Tag.FirstPerformer;
                    if (string.IsNullOrEmpty(artist))
                        artist = audioFile.Tag.FirstAlbumArtist;

                    track.Artist = StringHelper.ToUtf8(artist);

                    if (!string.IsNullOrEmpty(track.Artist))
                    {
                        track.Artist = track.Artist.Trim();
                        track.ArtistId = Md5Helper.Md5(track.Artist.Trim().ToLower());
                    }
                    else
                        track.Artist = string.Empty;

                    track.Duration = audioFile.Properties.Duration;
                    track.Source = filePath;

                    if (!string.IsNullOrWhiteSpace(audioFile.Tag.Album))
                    {
                        track.AlbumId = Md5Helper.Md5(track.Artist.Trim().ToLower() + "_" + StringHelper.ToUtf8(audioFile.Tag.Album).Trim());
                        track.Album = StringHelper.ToUtf8(audioFile.Tag.Album).Trim();
                    }

                    tracks.Add(track);
                    audioFile.Dispose();

                    await Task.Delay(50);
                }

                LoggingService.Log("Music scan finished. Found " + tracks.Count + " tracks");

                return tracks;
            });
        }
        private bool UpdateTrack(LocalAudio cachedTrack, LocalAudio updatedTrack)
        {
            bool changed = false;

            if (cachedTrack.Title != updatedTrack.Title)
            {
                cachedTrack.Title = updatedTrack.Title;
                changed = true;
            }

            if (cachedTrack.Artist != updatedTrack.Artist)
            {
                cachedTrack.Artist = updatedTrack.Artist;
                changed = true;
            }

            if (cachedTrack.Album != updatedTrack.Album)
            {
                cachedTrack.Album = updatedTrack.Album;
                changed = true;
            }

            if (cachedTrack.AlbumId != updatedTrack.AlbumId)
            {
                cachedTrack.AlbumId = updatedTrack.AlbumId;
                changed = true;
            }

            if (cachedTrack.ArtistId != updatedTrack.ArtistId)
            {
                cachedTrack.ArtistId = updatedTrack.ArtistId;
                changed = true;
            }

            if (cachedTrack.Duration != updatedTrack.Duration)
            {
                cachedTrack.Duration = updatedTrack.Duration;
                changed = true;
            }

            return changed;
        }
        private Task<List<AudioArtist>> GetFromLibrary()
        {
            return Task.Run(async () =>
            {
                var musicFiles = FilesHelper.GetMusicFiles();

                double totalCount = musicFiles.Count;

                var artists = new Dictionary<string, AudioArtist>();

                foreach (var filePath in musicFiles)
                {
                    TagLib.File audioFile = null;

                    try
                    {
                        audioFile = TagLib.File.Create(filePath);
                    }
                    catch (Exception ex)
                    {
                        LoggingService.Log(ex);
                        continue;
                    }

                    var track = new LocalAudio();
                    string artist = string.Empty;
                    if (!string.IsNullOrWhiteSpace(audioFile.Tag.FirstPerformer))
                        artist = audioFile.Tag.FirstPerformer;
                    else if (!string.IsNullOrWhiteSpace(audioFile.Tag.FirstAlbumArtist))
                        artist = audioFile.Tag.FirstAlbumArtist;

                    if (!string.IsNullOrWhiteSpace(artist))
                    {
                        track.ArtistId = Md5Helper.Md5(StringHelper.ToUtf8(artist).Trim().ToLower());
                        track.Artist = StringHelper.ToUtf8(artist).Trim();
                        if (!artists.ContainsKey(track.ArtistId))
                            artists.Add(track.ArtistId, new AudioArtist() { Id = track.ArtistId, Title = track.Artist });
                    }

                    audioFile.Dispose();

                    await Task.Delay(50);
                }

                LoggingService.Log("Music scan finished. Found " + artists.Count + " artists");

                return artists.Values.ToList();
            });
        }
示例#4
0
        public async Task ScanMusic(IProgress<double> progress)
        {
            LoggingService.Log("Music scan started");

            try
            {
                int count = 0;

                var tracks = new List<LocalAudio>();


                await Task.Run(async () =>
                {
                    var musicFiles = FilesHelper.GetMusicFiles();

                    double totalCount = musicFiles.Count;

                    //not cool but ¯\_(ツ)_/¯

                    var albums = new Dictionary<string, AudioAlbum>();
                    var artists = new Dictionary<string, AudioArtist>();

                    foreach (var filePath in musicFiles)
                    {
                        TagLib.File audioFile = null;

                        try
                        {
                            audioFile = TagLib.File.Create(filePath);
                        }
                        catch (Exception ex)
                        {
                            LoggingService.Log(ex);
                            continue;
                        }

                        var track = new LocalAudio();
                        track.Id = Md5Helper.Md5(filePath);
                        if (!string.IsNullOrEmpty(audioFile.Tag.Title))
                            track.Title = StringHelper.ToUtf8(audioFile.Tag.Title);
                        else
                            track.Title = Path.GetFileNameWithoutExtension(filePath);

                        var artist = audioFile.Tag.FirstPerformer;
                        if (string.IsNullOrEmpty(artist))
                            artist = audioFile.Tag.FirstAlbumArtist;

                        track.Artist = StringHelper.ToUtf8(artist);
                        if (!string.IsNullOrEmpty(track.Artist))
                            track.Artist = track.Artist.Trim();
                        else
                            track.Artist = string.Empty;

                        track.Duration = audioFile.Properties.Duration;
                        track.Source = filePath;

                        if (!string.IsNullOrWhiteSpace(audioFile.Tag.Album))
                        {
                            track.AlbumId = Md5Helper.Md5(track.Artist.Trim().ToLower() + "_" + StringHelper.ToUtf8(audioFile.Tag.Album).Trim());
                            track.Album = StringHelper.ToUtf8(audioFile.Tag.Album).Trim();
                            if (!albums.ContainsKey(track.AlbumId))
                                albums.Add(track.AlbumId, new AudioAlbum() { Id = track.AlbumId, Artist = track.Artist, ArtistId = !string.IsNullOrEmpty(track.Artist) ? Md5Helper.Md5(track.Artist.Trim().ToLower()) : null, Title = StringHelper.ToUtf8(audioFile.Tag.Album), Year = (int)audioFile.Tag.Year });
                            else
                            {
                                if (string.IsNullOrEmpty(albums[track.AlbumId].CoverPath) && audioFile.Tag.Pictures != null && audioFile.Tag.Pictures.Length > 0)
                                {
                                    albums[track.AlbumId].CoverPath = filePath;
                                }
                            }
                        }

                        if (!string.IsNullOrWhiteSpace(track.Artist))
                        {
                            track.ArtistId = Md5Helper.Md5(track.Artist.Trim().ToLower());
                            track.Artist = track.Artist.Trim();
                            if (!artists.ContainsKey(track.ArtistId))
                                artists.Add(track.ArtistId, new AudioArtist() { Id = track.ArtistId, Title = track.Artist });
                        }

                        tracks.Add(track);

                        count++;

                        progress.Report(count / totalCount * 100);
                        audioFile.Dispose();
                    }

                    await ServiceLocator.DataBaseService.SaveItems(tracks);
                    await ServiceLocator.DataBaseService.SaveItems(albums.Values);
                    await ServiceLocator.DataBaseService.SaveItems(artists.Values);

                    LoggingService.Log("Music scan finished. Found " + count + " tracks; " + albums.Count + " albums; " + artists.Count + " artists");

                    tracks.Clear();
                    albums.Clear();
                    artists.Clear();

                }, _scanCancellationToken.Token);
            }
            catch (Exception ex)
            {
                LoggingService.Log("Music scan error. " + ex);
            }
        }
        private Task<List<AudioAlbum>> GetFromLibrary()
        {
            return Task.Run(async () =>
            {
                var musicFiles = FilesHelper.GetMusicFiles();

                double totalCount = musicFiles.Count;

                var albums = new Dictionary<string, AudioAlbum>();

                foreach (var filePath in musicFiles)
                {
                    TagLib.File audioFile = null;

                    try
                    {
                        audioFile = TagLib.File.Create(filePath);
                    }
                    catch (Exception ex)
                    {
                        LoggingService.Log(ex);
                        continue;
                    }

                    var track = new LocalAudio();

                    if (!string.IsNullOrWhiteSpace(audioFile.Tag.Album))
                    {
                        string artist = string.Empty;
                        if (!string.IsNullOrEmpty(audioFile.Tag.FirstPerformer))
                            artist = StringHelper.ToUtf8(audioFile.Tag.FirstPerformer);
                        else if (!string.IsNullOrEmpty(audioFile.Tag.FirstAlbumArtist))
                            artist = StringHelper.ToUtf8(audioFile.Tag.FirstAlbumArtist);

                        track.AlbumId = Md5Helper.Md5(artist.Trim().ToLower() + "_" + StringHelper.ToUtf8(audioFile.Tag.Album).Trim());
                        track.Album = StringHelper.ToUtf8(audioFile.Tag.Album).Trim();
                        if (!albums.ContainsKey(track.AlbumId))
                            albums.Add(track.AlbumId, new AudioAlbum()
                            {
                                Id = track.AlbumId,
                                Artist = artist.Trim(),
                                Title = StringHelper.ToUtf8(audioFile.Tag.Album).Trim(),
                                Year = (int)audioFile.Tag.Year,
                                ArtistId = Md5Helper.Md5(artist.Trim().ToLower())
                            });
                        else
                        {
                            if (string.IsNullOrEmpty(albums[track.AlbumId].CoverPath) && audioFile.Tag.Pictures != null && audioFile.Tag.Pictures.Length > 0)
                            {
                                albums[track.AlbumId].CoverPath = filePath;
                            }
                        }
                    }
                    audioFile.Dispose();

                    await Task.Delay(50);
                }

                LoggingService.Log("Music scan finished. Found " + albums.Count + " albums");

                return albums.Values.ToList();
            });
        }