TxtLyricProvider.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.Collections.Generic;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Lyrics;
  4. using MediaBrowser.Controller.Resolvers;
  5. namespace MediaBrowser.Providers.Lyric;
  6. /// <summary>
  7. /// TXT Lyric Provider.
  8. /// </summary>
  9. public class TxtLyricProvider : ILyricProvider
  10. {
  11. /// <inheritdoc />
  12. public string Name => "TxtLyricProvider";
  13. /// <summary>
  14. /// Gets the priority.
  15. /// </summary>
  16. /// <value>The priority.</value>
  17. public ResolverPriority Priority => ResolverPriority.Second;
  18. /// <inheritdoc />
  19. public IReadOnlyCollection<string> SupportedMediaTypes { get; } = new[] { "lrc", "txt" };
  20. /// <summary>
  21. /// Opens lyric file for the requested item, and processes it for API return.
  22. /// </summary>
  23. /// <param name="item">The item to to process.</param>
  24. /// <returns>If provider can determine lyrics, returns a <see cref="LyricResponse"/>; otherwise, null.</returns>
  25. public LyricResponse? GetLyrics(BaseItem item)
  26. {
  27. string? lyricFilePath = LyricInfo.GetLyricFilePath(this, item.Path);
  28. if (string.IsNullOrEmpty(lyricFilePath))
  29. {
  30. return null;
  31. }
  32. string[] lyricTextLines = System.IO.File.ReadAllLines(lyricFilePath);
  33. List<LyricLine> lyricList = new List<LyricLine>();
  34. if (lyricTextLines.Length == 0)
  35. {
  36. return null;
  37. }
  38. foreach (string lyricTextLine in lyricTextLines)
  39. {
  40. lyricList.Add(new LyricLine(lyricTextLine));
  41. }
  42. return new LyricResponse { Lyrics = lyricList };
  43. }
  44. }