Overview
LocalAudiobookService manages the import, storage, and organization of local audiobook files on Android devices using the Storage Access Framework (SAF). It provides automatic metadata extraction, cover art detection, and smart caching for efficient file scanning.
File: lib/resources/services/local/local_audiobook_service.dart
Storage Configuration
getRootFolderPath()
Gets the configured root folder path for local audiobooks. Returns:Future<String?>
setRootFolderPath()
Sets the root folder path where local audiobooks are stored.The SAF URI path to the audiobooks folder
Audiobook Management
getAllAudiobooks()
Retrieves all locally imported audiobooks from Hive storage. Returns:Future<List<LocalAudiobook>>
saveAudiobook()
Saves a local audiobook to Hive storage.The audiobook to save
updateAudiobook()
Updates an existing local audiobook in storage.The audiobook to update
deleteAudiobook()
Removes a local audiobook from storage.The audiobook to delete
Scanning & Caching
scanForAudiobooks()
Performs a full scan of the root folder to discover audiobooks. This method:- Scans all audio files in the root folder
- Organizes files into 3 levels (standalone files, folders, author/book structure)
- Extracts metadata from ID3 tags
- Searches for cover art (file match, folder search, embedded art)
Future<List<LocalAudiobook>>
The scanner supports three folder organization levels:
- Level 0: Standalone audio files in the root
- Level 1: One folder per audiobook
- Level 2: Author folders containing book folders
refreshAudiobooks()
Performs a full rescan and replaces all cached audiobooks. Returns:Future<List<LocalAudiobook>>
smartRefreshAudiobooks()
Intelligently refreshes audiobooks by only processing changed files. Returns:Future<List<LocalAudiobook>>
How it works:
- Compares current files with cached file list
- Identifies new, deleted, and modified files
- Only processes affected audiobooks
- Returns cached audiobooks if no changes detected
Cache Management
getLastScannedFiles()
Gets the list of file paths from the last scan. Returns:Future<List<String>?>
saveScannedFiles()
Saves the list of scanned file paths to cache.List of file paths to cache
clearFileCache()
Clears the cached file list. Useful when changing the root folder.clearAllCaches()
Clears both the file cache and all audiobook data.Metadata Extraction
The service automatically extracts metadata from audio files: ID3 Tags:- Title
- Artist/Author
- Album
- Genre
- Track number
- Duration
- File Match: Looks for
cover.jpg,folder.jpg,artwork.jpgin the same folder - Folder Search: Scans all images in the folder
- Embedded Art: Extracts embedded album art from ID3 tags
File Organization Levels
Level 0: Standalone Files
Level 1: Folder per Book
Level 2: Author/Book Structure
Usage Example
Storage Backend
The service uses Hive for local storage with two boxes:local_audiobooks: Stores audiobook metadatalocal_audiobooks_file_cache: Caches file paths for change detection
Related
- LocalAudiobook Model - Data structure for local audiobooks
- AudiobookFile - Audio file representation
- Local Audiobooks Feature - User guide for importing local files