LyricsRepository handles fetching synchronized lyrics from the LrcLib API with automatic caching and retry logic with exponential backoff.
Lyrics are cached locally to minimize API calls and provide offline access to previously fetched lyrics.
app/src/main/java/com/crowstar/deeztrackermobile/features/lyrics/LyricsRepository.kt
Constructor
Android Context for accessing cache directory
- Base URL:
https://lrclib.net/ - 15-second connection timeout
- 30-second read/write timeouts
- Gson converter for JSON parsing
Primary Method
getLyrics()
Fetch synchronized lyrics for a track, with automatic caching.The track to fetch lyrics for (requires title, artist, album, and duration)
LRC-formatted lyrics string, or null if not found
- Cache Check: Returns cached lyrics if available
- Exact Match: Search by track name, artist, and album
- Fallback: Search by track name and artist only
- Duration Filter: Filters results by track duration (±2 seconds tolerance)
- Cache: Saves successful results to cache
Retry Logic
The repository implements exponential backoff for API requests:Maximum number of retry attempts
Initial delay before first retry (doubles for each subsequent retry)
- Initial attempt
- Retry after 500ms (if failed)
- Retry after 1000ms (if failed)
- Retry after 2000ms (if failed)
- Return null (after 3 failures)
Caching System
Cache Location
Lyrics are cached in the app’s internal cache directory:Cache File Naming
Cache files are named using normalized track information:normalize() replaces special characters and spaces with underscores.
Cache Behavior
- Hit: Returns cached lyrics immediately
- Miss: Fetches from API and caches result
- Invalid: Deletes and refetches if cache contains “NOT_FOUND” or is blank
API Integration
The repository uses the LrcLib API endpoints:Search by Track, Artist, and Album
Search by Track and Artist Only
Get by Exact Match
Duration Matching
The repository filters lyrics by duration with a ±2 second tolerance:Error Handling
Error Scenarios:- Network Failure: Retries with exponential backoff
- API Timeout: Retries up to 3 times
- No Results: Returns null (not cached)
- Invalid Response: Retries or returns null
- Cache Read Error: Deletes invalid cache and refetches
Response Format
Lyrics are returned in LRC format with timestamps:LrcParser for synchronized display:
features/lyrics/LrcParser.kt
Usage in Player
The lyrics are displayed with real-time synchronization in the music player:features/player/PlayerController.kt
Performance Considerations
Cache Management
To clear the lyrics cache:See Also
Lyrics Feature
How synchronized lyrics work in the app
LrcParser
LRC format parsing and timestamp handling
Music Player
Player integration with lyrics display