Factory
nmMPlayer(id?)
TypeScript
import nmMPlayer from '@nomercy-entertainment/nomercy-music-player';
nmMPlayer(id?: string | number): NMMusicPlayer<MusicPlaylistItem>
nmMPlayer<T extends MusicPlaylistItem>(id?: string | number): NMMusicPlayer<T>
Creates or retrieves a named player instance.
This is the only entry point, do not call new NMMusicPlayer() directly.
idis optional. When omitted, the core generates a unique id.- Two calls with the same
idreturn the same instance, no re-initialization. - Instances persist until
dispose()is called. After dispose, the sameidcreates a fresh instance.
TypeScript
const player = nmMPlayer('main'); // creates or retrieves 'main'
const same = nmMPlayer('main'); // same instance
player === same; // true
With a custom item type:
TypeScript
interface TrackItem extends MusicPlaylistItem {
catalogueId: number;
}
const player = nmMPlayer<TrackItem>('main');
// player.item() returns TrackItem | undefined
NMMusicPlayer<T>
The concrete player class.
Extends EventEmitter<MusicEventMap> and implements IPlayer<MusicEventMap> and IMusicPlayer<T>.
Constructor is not part of the public API, always use nmMPlayer().
IMusicPlayer<T>
TypeScript
import type { IMusicPlayer } from '@nomercy-entertainment/nomercy-music-player';
Recommended type for consumer-held player references. Use this when you accept a player parameter but only need music-specific methods.
TypeScript
interface IMusicPlayer<
T extends BasePlaylistItem = MusicPlaylistItem,
> extends IPlayer<MusicEventMap> {
backend(): IAudioBackend;
backend(kind: AudioBackendKind): Promise<void>;
crossfadeTo(track: T, opts?: CrossfadeOptions & ActionOptions): Promise<void>;
isTransitioning(): boolean;
}
TypeScript
function buildNowPlayingUI(player: IMusicPlayer): void {
player.on('current', ({ item }) => {
if (item) updateTitle(item.name);
});
player.on('crossfadeStart', ({ to }) => showTransition(to.name));
}
See also
- Configuration,
setup()options - Quick Start,
nmMPlayer('id').addPlugin(...).setup(...)pattern - Types Reference, all exported types