LocalMusicRepository provides access to all music files on the device using Android’s MediaStore API. It handles querying, filtering, and organizing local music by tracks, albums, and artists.
This repository requires READ_EXTERNAL_STORAGE or READ_MEDIA_AUDIO permissions depending on the Android version.
app/src/main/java/com/crowstar/deeztrackermobile/features/localmusic/LocalMusicRepository.kt
Constructor
Android ContentResolver for querying MediaStore
Optional playlist repository for favorite track integration
Storage Methods
getTotalStorageSpace()
Get the total storage space available on the device.Total storage space in bytes
Track Query Methods
getAllTracks()
Get all music tracks from device storage, sorted alphabetically by title.List of all music tracks on the device
- Filters out non-music files (notifications, ringtones, alarms)
- Sorts by title (case-insensitive)
- Includes full metadata (title, artist, album, duration, file path, size, MIME type, dates)
- Generates album art URIs
getTracksForAlbum()
Get all tracks for a specific album.MediaStore album ID
List of tracks in the album, sorted by track number
getTracksForArtist()
Get all tracks by a specific artist.Artist name to filter by
List of tracks by the artist
searchTracks()
Search tracks by query string (searches title, artist, and album fields).Search query string
List of tracks matching the search query
getDownloadedTracks()
Get tracks from specific file paths (used to find downloaded tracks).List of file paths to query
List of tracks at the specified paths
getTrackIdByPath()
Get the MediaStore ID for a track at a specific file path.File path of the track
MediaStore track ID, or null if not found
Album Methods
getAllAlbums()
Get all albums from device storage.List of all albums with metadata (title, artist, track count, first year)
Artist Methods
getAllArtists()
Get all artists from device storage.List of all artists with track and album counts
Deletion Methods
requestDeleteTrack()
Request permission to delete a track (Android 11+).MediaStore track ID to delete
IntentSender for requesting deletion permission, or null on Android 10 and below
onTrackDeleted()
Update internal state after a track has been deleted.MediaStore track ID that was deleted
This method removes the track from any playlists via LocalPlaylistRepository.
MediaStore Integration
The repository queries the following MediaStore columns:_ID- Unique track identifierTITLE- Track titleARTIST- Artist nameALBUM- Album nameALBUM_ID- Album identifier (for album art)DURATION- Track duration in millisecondsDATA- File pathSIZE- File size in bytesMIME_TYPE- Audio format (audio/mpeg, audio/flac, etc.)DATE_ADDED- Unix timestamp when addedDATE_MODIFIED- Unix timestamp of last modificationTRACK- Track number in albumYEAR- Release year
Album Art Handling
Album art is retrieved using the album ID:Usage Example
LocalMusicScreen.kt:45-68
See Also
Local Library Feature
How local music browsing works in the app
LocalTrack Model
LocalTrack data structure
LocalPlaylistRepository
Playlist management