AudioPlayer
AudioPlayer
is a class that manages audio playback and editing functionalities. It handles loading audio tracks, controlling playback, and managing audio effects.
Properties
name: string
The name of the audio player. The default value is 'AudioPlayer'
.
tracks: Map<string, Map<string, Audio>>
A map that stores audio tracks, organized by track IDs and audio IDs.
_duration: number
The total duration of all loaded tracks.
audioContext: AudioContext | null
The audio context used for audio processing.
Constructor
constructor();
Initializes a new instance of the AudioPlayer
.
Methods
createContext ()
Creates and initializes the audio context.
- Return:
AudioContext
loadAudioBuffer (src)
Loads an audio buffer from the specified source URL.
Name | Type | Description |
---|---|---|
src | string | The URL of the audio source. |
- Return:
Promise<AudioBuffer>
prepareTrackAll (waves)
Prepare an audio source from multiple wave objects.
Name | Type | Description |
---|---|---|
waves | Wave[] | Wave components. |
prepareWave (waves)
Prepare an audio source from a wave object.
Name | Type | Description |
---|---|---|
waves | Wave | Wave components. |
play (startTime)
Starts playback of the audio tracks.
Name | Type | Description |
---|---|---|
startTime | number | The time to start playback. Default value is 0. |
pause ()
Pauses the currently playing tracks.
stop ()
Stops all currently playing tracks.
mute (trackId, isMuted)
Mutes or unmutes the specified track.
Name | Type | Description |
---|---|---|
trackId | string | The ID of the track to mute. |
isMuted | boolean undefined | The mute state to set. |
isMuted (trackId)
Checks if the specified track is muted.
Name | Type | Description |
---|---|---|
trackId | string | The ID of the track to check. |
- Return:
boolean
getAudioBuffer (trackId)
Retrieves the audio buffer for the specified track.
Name | Type | Description |
---|---|---|
trackId | string | The ID of the track. |
- Return:
AudioBuffer | undefined
getDuration ()
Returns the total duration of all loaded tracks.
- Return:
number
async toBlob ()
Converts the currently loaded audio tracks into a single Blob. This function merges the audio buffers of all tracks and transforms the merged audio buffer into a Blob.
- Return:
Promise<Blob>
async downloadBlob (filename)
Downloads the Blob with the specified filename.
This function calls the toBlob
function to create the Blob and then uses the generated Blob to perform the file download.
Name | Type | Description |
---|---|---|
filename | string | The name of the file to download. |
setTrackGroup(trackgroup)
Specifies the trackgroup the AudioPlayer will track.
Manages audio based on specified track groups.
Usage Example
const audioPlayer = new AudioPlayer();
const songData: SongDataType = {};
await Promise.all([audioPlayer.prepareTrack(songData, 'track1')]);
audioPlayer.play();