private async void RefreshLyricsAsync(PlayableTrack track)
        {
            if (!this.nowPlayingIsSelected || !this.lyricsScreenIsActive)
            {
                return;
            }
            if (track == null)
            {
                return;
            }
            if (this.previousTrack != null && this.previousTrack.Equals(track))
            {
                return;
            }

            this.previousTrack = track;

            this.StopHighlighting();

            FileMetadata fmd = await this.metadataService.GetFileMetadataAsync(track.Path);

            await Task.Run(() =>
            {
                // If we're in editing mode, delay changing the lyrics.
                if (this.LyricsViewModel != null && this.LyricsViewModel.IsEditing)
                {
                    this.updateLyricsAfterEditingTimer.Start();
                    return;
                }

                // No FileMetadata available: clear the lyrics.
                if (fmd == null)
                {
                    this.ClearLyrics();
                    return;
                }
            });

            try
            {
                Lyrics lyrics             = null;
                bool   mustDownloadLyrics = false;

                await Task.Run(() =>
                {
                    lyrics = new Lyrics(fmd != null && fmd.Lyrics.Value != null ? fmd.Lyrics.Value : String.Empty, string.Empty);

                    // If the file has no lyrics, and the user enabled automatic download of lyrics, indicate that we need to try to download.
                    if (!lyrics.HasText)
                    {
                        if (SettingsClient.Get <bool>("Lyrics", "DownloadLyrics"))
                        {
                            string artist = fmd.Artists != null && fmd.Artists.Values != null && fmd.Artists.Values.Length > 0 ? fmd.Artists.Values[0] : string.Empty;
                            string title  = fmd.Title != null && fmd.Title.Value != null ? fmd.Title.Value : string.Empty;

                            if (!string.IsNullOrWhiteSpace(artist) & !string.IsNullOrWhiteSpace(title))
                            {
                                mustDownloadLyrics = true;
                            }
                        }
                    }
                });

                // No lyrics were found in the file: try to download.
                if (mustDownloadLyrics)
                {
                    this.IsDownloadingLyrics = true;

                    try
                    {
                        var factory = new LyricsFactory();
                        lyrics = await factory.GetLyricsAsync(fmd.Artists.Values[0], fmd.Title.Value);
                    }
                    catch (Exception ex)
                    {
                        LogClient.Error("Could not get lyrics online {0}. Exception: {1}", track.Path, ex.Message);
                    }

                    this.IsDownloadingLyrics = false;
                }

                await Task.Run(() =>
                {
                    this.LyricsViewModel = new LyricsViewModel(container, track, metadataService);
                    this.LyricsViewModel.SetLyrics(lyrics);
                });
            }
            catch (Exception ex)
            {
                this.IsDownloadingLyrics = false;
                LogClient.Error("Could not show lyrics for Track {0}. Exception: {1}", track.Path, ex.Message);
                this.ClearLyrics();
                return;
            }

            this.StartHighlighting();
        }
        private async void RefreshLyricsAsync(PlayableTrack track)
        {
            if (!this.nowPlayingIsSelected || !this.lyricsScreenIsActive)
            {
                return;
            }
            if (track == null)
            {
                return;
            }
            if (this.previousTrack != null && this.previousTrack.Equals(track))
            {
                return;
            }

            this.previousTrack = track;

            this.StopHighlighting();

            FileMetadata fmd = await this.metadataService.GetFileMetadataAsync(track.Path);

            await Task.Run(() =>
            {
                // If we're in editing mode, delay changing the lyrics.
                if (this.LyricsViewModel != null && this.LyricsViewModel.IsEditing)
                {
                    this.updateLyricsAfterEditingTimer.Start();
                    return;
                }

                // No FileMetadata available: clear the lyrics.
                if (fmd == null)
                {
                    this.ClearLyrics();
                    return;
                }
            });

            try
            {
                Lyrics lyrics             = null;
                bool   mustDownloadLyrics = false;

                await Task.Run(async() =>
                {
                    // Try to get lyrics from the audio file
                    lyrics            = new Lyrics(fmd != null && fmd.Lyrics.Value != null ? fmd.Lyrics.Value : String.Empty, string.Empty);
                    lyrics.SourceType = SourceTypeEnum.Audio;

                    // If the audio file has no lyrics, try to find lyrics in a local lyrics file.
                    if (!lyrics.HasText)
                    {
                        var lrcFile = Path.Combine(Path.GetDirectoryName(fmd.Path), Path.GetFileNameWithoutExtension(fmd.Path) + FileFormats.LRC);

                        if (File.Exists(lrcFile))
                        {
                            using (var fs = new FileStream(lrcFile, FileMode.Open, FileAccess.Read))
                            {
                                using (var sr = new StreamReader(fs, Encoding.Default))
                                {
                                    lyrics = new Lyrics(await sr.ReadToEndAsync(), String.Empty);
                                    if (lyrics.HasText)
                                    {
                                        lyrics.SourceType = SourceTypeEnum.Lrc;
                                        return;
                                    }
                                }
                            }
                        }

                        // If we still don't have lyrics and the user enabled automatic download of lyrics: try to download them online.
                        if (SettingsClient.Get <bool>("Lyrics", "DownloadLyrics"))
                        {
                            string artist = fmd.Artists != null && fmd.Artists.Values != null && fmd.Artists.Values.Length > 0 ? fmd.Artists.Values[0] : string.Empty;
                            string title  = fmd.Title != null && fmd.Title.Value != null ? fmd.Title.Value : string.Empty;

                            if (!string.IsNullOrWhiteSpace(artist) & !string.IsNullOrWhiteSpace(title))
                            {
                                mustDownloadLyrics = true;
                            }
                        }
                    }
                });

                // No lyrics were found in the file: try to download.
                if (mustDownloadLyrics)
                {
                    this.IsDownloadingLyrics = true;

                    try
                    {
                        var factory = new LyricsFactory();
                        lyrics = await factory.GetLyricsAsync(fmd.Artists.Values[0], fmd.Title.Value);

                        lyrics.SourceType = SourceTypeEnum.Online;
                    }
                    catch (Exception ex)
                    {
                        CoreLogger.Current.Error("Could not get lyrics online {0}. Exception: {1}", track.Path, ex.Message);
                    }

                    this.IsDownloadingLyrics = false;
                }

                await Task.Run(() =>
                {
                    this.LyricsViewModel = new LyricsViewModel(container, track);
                    this.LyricsViewModel.SetLyrics(lyrics);
                });
            }
            catch (Exception ex)
            {
                this.IsDownloadingLyrics = false;
                CoreLogger.Current.Error("Could not show lyrics for Track {0}. Exception: {1}", track.Path, ex.Message);
                this.ClearLyrics();
                return;
            }

            this.StartHighlighting();
        }
Exemple #3
0
 private void I18NService_LanguageChanged(object sender, EventArgs e)
 {
     this.lyricsFactory = new LyricsFactory(SettingsClient.Get <int>("Lyrics", "TimeoutSeconds"),
                                            SettingsClient.Get <string>("Lyrics", "Providers"), this.info);
 }