Overview
TheAudiobookFile class represents a single audio file or chapter in an audiobook. It can represent files from Archive.org, YouTube, or local storage. It also supports chapter slicing for books with embedded chapter metadata.
Properties
The audiobook identifier this file belongs to
The title of this specific file or chapter
The filename of the audio file
The URL or local path to the audio file
The duration of the audio file in seconds
The track number or chapter position
The file size in bytes
URL or path to the high-quality cover image for this file
Chapter start time in milliseconds (from file start). Used for chapter slicing
Chapter duration in milliseconds. If null, plays to end of file. Used for chapter slicing
Constructors
AudiobookFile.fromJson(Map json)
Creates anAudiobookFile from Archive.org JSON data.
Parameters:
A map containing file data from the Archive.org API
- File URL:
https://archive.org/download/{identifier}/{name} - Cover image:
https://archive.org/download/{identifier}/{highQCoverImage}
AudiobookFile.fromYoutubeJson(Map json)
Creates anAudiobookFile from YouTube JSON data. The URL is taken directly from the JSON rather than being constructed.
Parameters:
A map containing file data from YouTube
AudiobookFile.fromLocalJson(Map json, String location)
Creates anAudiobookFile from local storage JSON data.
Parameters:
A map containing file data
The local directory path where the audiobook is stored
- File URL:
{location}/{url} - Cover image:
{location}/cover.jpg
AudiobookFile.fromMap
Creates anAudiobookFile from a map.
Parameters:
A map containing all audiobook file properties (Map<dynamic, dynamic>)
Methods
toMap()
Converts the audiobook file to a map. Returns:Map<dynamic, dynamic>
toJson()
Converts the audiobook file to a JSON-serializable map. Returns:Map<String, dynamic>
Static Methods
chapterSlice
Creates a chapter slice from a larger audio file using time offsets. Useful for audiobooks with embedded chapter metadata. Parameters:The audiobook identifier
The URL or path to the parent audio file
The title of the parent file
The chapter/track number
The title of the chapter. If empty, defaults to ” — Chapter ”
The start time of the chapter in milliseconds
The duration of the chapter in milliseconds. If null, plays to end of file
URL or path to the cover image
AudiobookFile
fromJsonArray(List jsonFiles)
Converts a list of JSON objects into a list ofAudiobookFile instances. Handles parsing errors gracefully and logs them.
Parameters:
A list of JSON maps from the Archive.org API response
List<AudiobookFile>
fromLocalJsonArray(List jsonFiles, String location)
Converts a list of local JSON objects intoAudiobookFile instances.
Parameters:
A list of JSON maps
The local directory path
List<AudiobookFile>
fromYoutubeJsonArray(List jsonFiles)
Converts a list of YouTube JSON objects intoAudiobookFile instances.
Parameters:
A list of JSON maps from YouTube
List<AudiobookFile>
fromDownloadedFiles(String audiobookId)
Scans the downloads directory for MP3 files and createsAudiobookFile instances from them. Extracts metadata using flutter_media_metadata.
Parameters:
The audiobook identifier
Future<Either<String, List<AudiobookFile>>>
Returns a Right with the list of files on success, or a Left with an error message on failure.
fromLocalFiles(String audiobookId)
LoadsAudiobookFile instances from a files.txt JSON file in the local audiobook directory.
Parameters:
The audiobook identifier
Future<Either<String, List<AudiobookFile>>>
fromYoutubeFiles(String audiobookId)
LoadsAudiobookFile instances from a files.txt JSON file in the YouTube audiobook directory.
Parameters:
The audiobook identifier
Future<Either<String, List<AudiobookFile>>>
Helper Methods
The class includes private static helper methods for safe type parsing:_parseTrack(dynamic value)- Safely parses track numbers, handling strings like “1/20”_parseIntSafely(dynamic value)- Safely parses integers from various types_parseDoubleSafely(dynamic value)- Safely parses doubles from various types