webamp
Version:
Winamp 2 implemented in HTML5 and JavaScript
212 lines (211 loc) • 7.86 kB
TypeScript
import type JSZip from "jszip";
import ReactDOM from "react-dom/client";
import { Store, Track, LoadedURLTrack, Middleware, ButterchurnOptions, PartialState, Options, MediaStatus, PlaylistTrack, PlayerMediaStatus, IMetadataApi, Preset } from "./types";
import { IMedia, IMediaClass } from "./media";
import Emitter from "./emitter";
import Disposable from "./Disposable";
export interface PrivateOptions {
__initialState?: PartialState;
__customMiddlewares?: Middleware[];
__butterchurnOptions?: ButterchurnOptions;
__customMediaClass?: IMediaClass;
}
export interface InjectableDependencies {
requireJSZip: () => Promise<JSZip>;
requireMusicMetadata: () => Promise<IMetadataApi>;
requireButterchurnPresets?: () => Promise<Preset[]>;
}
declare class Webamp {
static VERSION: string;
_actionEmitter: Emitter;
_root: ReactDOM.Root | null;
_disposable: Disposable;
options: Options & PrivateOptions & InjectableDependencies;
media: IMedia;
store: Store;
/**
* Returns a true if the current browser supports the features that Webamp depends upon.
*
* It is recommended to check this before you attempt to instantiate an instance of Winamp.
*/
static browserIsSupported(): boolean;
constructor(options: Options & PrivateOptions & InjectableDependencies);
/**
* Play the current track.
*/
play(): void;
/**
* Pause the current track.
*/
pause(): void;
/**
* Stop the currently playing audio. Equivalent to pressing the "stop" button.
*/
stop(): void;
/**
* Set volume from 0 - 100.
*/
setVolume(volume: number): void;
/**
* Seek backward n seconds in the current track.
*/
seekBackward(seconds: number): void;
/**
* Seek forward n seconds in the current track.
*/
seekForward(seconds: number): void;
/**
* Seek to a given time within the current track.
*/
seekToTime(seconds: number): void;
/**
* Check if shuffle is enabled.
*/
isShuffleEnabled(): boolean;
/**
* Toggle shuffle mode between enabled and disabled.
*/
toggleShuffle(): void;
/**
* Check if repeat is enabled.
*/
isRepeatEnabled(): boolean;
/**
* Toggle repeat mode between enabled and disabled.
*/
toggleRepeat(): void;
/**
* Play the next track.
*/
nextTrack(): void;
/**
* Play the previous track.
*/
previousTrack(): void;
/**
* Set the current track to a specific track in the playlist by zero-based index.
*
* Note: If Webamp is currently playing, the track will begin playing. If
* Webamp is not playing, the track will not start playing. You can use
* `webamp.pause()` before calling this method or `webamp.play()` after
* calling this method to control whether the track starts playing.
*/
setCurrentTrack(index: number): void;
/**
* Add an array of `Track`s to the end of the playlist.
*/
appendTracks(tracks: Track[]): void;
/**
* Replace the playlist with an array of `Track`s and begin playing the first track.
*/
setTracksToPlay(tracks: Track[]): void;
/**
* Get the current playlist in order.
*/
getPlaylistTracks(): PlaylistTrack[];
/**
* Get the current "playing" status.
*/
getMediaStatus(): MediaStatus;
/**
* Get the current "playing" status of the player. Similar to
* `getMediaStatus()`, but can differentiate between different reasons why the
* player might not be playing, such as "ENDED" when the end of the playlist
* has been reached or "CLOSED" when the player has been closed.
*/
getPlayerMediaStatus(): PlayerMediaStatus;
/**
* A callback which will be called when Webamp is _about to_ close. Returns an
* "unsubscribe" function. The callback will be passed a `cancel` function
* which you can use to conditionally prevent Webamp from being closed.
*
* @returns An "unsubscribe" function. Useful if at some point in the future you want to stop listening to these events.
*/
onWillClose(cb: (cancel: () => void) => void): () => void;
/**
* A callback which will be called when Webamp is closed.
*
* @returns An "unsubscribe" function. Useful if at some point in the future you want to stop listening to these events.
*/
onClose(cb: () => void): () => void;
/**
* Equivalent to selecting "Close" from Webamp's options menu. Once closed,
* you can open it again with `.reopen()`.
*/
close(): void;
/**
* After `.close()`ing this instance, you can reopen it by calling this method.
*/
reopen(): void;
/**
* A callback which will be called when a new track starts loading.
*
* This can happen on startup when the first track starts buffering, or when a
* subsequent track starts playing. The callback will be called with an
* object `({url: 'https://example.com/track.mp3'})` containing the URL of the
* track.
*
* Note: If the user drags in a track, the URL may be an ObjectURL.
*
* @returns An "unsubscribe" function. Useful if at some point in the future
* you want to stop listening to these events.
*/
onTrackDidChange(cb: (trackInfo: LoadedURLTrack | null) => void): () => void;
/**
* A callback which will be called when Webamp is minimized.
*
* @returns An "unsubscribe" function. Useful if at some point in the future you want to stop listening to these events.
*/
onMinimize(cb: () => void): () => void;
/**
* Set the skin to use. This can be a URL or a base64 encoded string. The skin
* will be loaded asynchronously.
*
* NOTE: If the URL is not on the same domain as the page, you will need to consider CORS.
* https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
*/
setSkinFromUrl(url: string): void;
/**
* Returns a promise that resolves when the skin is done loading.
*/
skinIsLoaded(): Promise<void>;
/**
* Webamp will wait until it has fetched the skin and fully parsed it and then render itself.
*
* Webamp is rendered into a new DOM node at the end of the <body> tag with the id `#webamp`.
*
* Webamp will position itself on top of the center of the given DOM node.
*
* @returns A promise is returned which will resolve after the render is complete.
*/
renderWhenReady(node: HTMLElement): Promise<void>;
/**
* Webamp will wait until it has fetched the skin and fully parsed it and then render itself.
*
* Webamp will render itself as a child of the given DOM node and position
* itself in the center of that node.
*
* @returns A promise is returned which will resolve after the render is complete.
*/
renderInto(node: HTMLElement): Promise<void>;
_render(node: HTMLElement, contained: boolean): Promise<void>;
/**
* **Note:** _This method is not fully functional. It is currently impossible to
* clean up a Winamp instance. This method makes an effort, but it still leaks
* the whole instance. In the future the behavior of this method will improve,
* so you might as well call it._
*
* When you are done with a Webamp instance, call this method and Webamp will
* attempt to clean itself up to avoid memory leaks.
*/
dispose(): void;
__onStateChange(cb: () => void): () => void;
/**
* Wait for the store to match a predicate condition.
* Returns a promise that resolves when the condition is met.
* If the instance is disposed, the promise will be rejected.
*/
private storeHas;
_bufferTracks(tracks: Track[]): void;
}
export default Webamp;