diff --git a/MediaBrowser.Controller/Lyrics/LyricInfo.cs b/MediaBrowser.Controller/Lyrics/LyricInfo.cs index a063a4cc55..57964eee51 100644 --- a/MediaBrowser.Controller/Lyrics/LyricInfo.cs +++ b/MediaBrowser.Controller/Lyrics/LyricInfo.cs @@ -18,18 +18,29 @@ public static class LyricInfo /// Lyric file path if passed lyric provider's supported media type is found; otherwise, null. public static string? GetLyricFilePath(this ILyricProvider lyricProvider, string itemPath) { + // Ensure we have a provider if (lyricProvider is null) { return null; } - if (!Directory.Exists(Path.GetDirectoryName(itemPath))) + // Ensure the path to the item is not null + string? itemDirectoryPath = Path.GetDirectoryName(itemPath); + if (itemDirectoryPath is null) { return null; } - foreach (var lyricFilePath in Directory.GetFiles(Path.GetDirectoryName(itemPath), $"{Path.GetFileNameWithoutExtension(itemPath)}.*")) + // Ensure the directory path exists + if (!Directory.Exists(itemDirectoryPath)) { + return null; + } + + foreach (var lyricFilePath in Directory.GetFiles(itemDirectoryPath, $"{Path.GetFileNameWithoutExtension(itemPath)}.*")) + { + if (lyricFilePath is null) { continue; } + if (lyricProvider.SupportedMediaTypes.Contains(Path.GetExtension(lyricFilePath)[1..])) { return lyricFilePath;