TxtLyricProvider.cs 1.5 KB

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