This documentation covers two main classes: TrackManager
and PlayerManager
, which are used for managing audio tracks and playback.
Represents the loading state of a track.
enum LoadState {
Before = "before",
Loading = "loading",
Loaded = "loaded",
Error = "error"
}
Defines the strategy for loading tracks.
enum LoadStrategy {
Eager = "eager",
Lazy = "lazy"
}
The TrackManager
class is responsible for managing individual audio tracks.
constructor({ id, src, strategy = LoadStrategy.Eager }: TrackManagerParams)
id
: Unique identifier for the tracksrc
: Source URL of the audio filestrategy
: Loading strategy (default:LoadStrategy.Eager
)
id
: String - Unique identifier for the tracksrc
: String - Source URL of the audio fileaudio
: HTMLAudioElement - The underlying audio elementstate
: LoadState - Current loading state of the trackstrategy
: LoadStrategy - Loading strategy for the trackduration
: number | undefined - Duration of the track in secondscurrentTime
: number - Current playback position in secondsprogress
: number - Playback progress as a fraction (0 to 1)playing
: boolean - Whether the track is currently playing
load()
: Promise - Loads the audio trackplay({ startAt?: number })
: Promise - Plays the track, optionally starting at a specific timepause()
: Promise - Pauses the tracksetCurrentTime(val: number)
: void - Sets the current playback positionsetProgress(val: number)
: void - Sets the playback progress as a fraction (0 to 1)
The PlayerManager
class manages the currently active track and provides an interface for playback control.
constructor()
activeTrack
: TrackManager | null - The currently active trackplaying
: boolean - Whether the active track is currently playingcurrentTime
: number - Current playback position of the active trackprogress
: number - Playback progress of the active track as a fraction (0 to 1)duration
: number | undefined - Duration of the active track in seconds
isActive(track: TrackManager | null | string)
: boolean - Checks if the given track is currently activesetTrack(track: TrackManager | null)
: void - Sets the active trackplay({ track?: TrackManager | null, startAt?: number })
: Promise - Plays the active track or a specified trackpause()
: Promise - Pauses the active tracksetCurrentTime(val: number)
: void - Sets the current playback position of the active tracksetProgress(val: number)
: void - Sets the playback progress of the active track as a fraction (0 to 1)
// Create a TrackManager instance
const track = new TrackManager({
id: "track1",
src: "https://example.com/audio.mp3",
strategy: LoadStrategy.Lazy
});
// Create a PlayerManager instance
const player = new PlayerManager();
// Load and play the track
await player.play({ track });
// Pause the track
await player.pause();
// Set playback position to 30 seconds
player.currentTime = 30;
// Set playback progress to 50%
player.setProgress(0.5);
This documentation provides an overview of the TrackManager
and PlayerManager
classes, their properties, and methods. For more detailed information on each method and property, refer to the inline comments in the source code.