1.3.0-rc.8 • Published 1 year ago

@vocadb/nostalgic-diva v1.3.0-rc.8

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Nostalgic Diva

React function components for imperatively controlling embedded players (audio, Niconico, SoundCloud, Vimeo and YouTube) using refs.

This was originally developed in VocaDB/vocadb#1101 as a part of VocaDB.

Live demo

See VocaDB and its playlist page.

Installation

yarn add @vocadb/nostalgic-diva or npm i @vocadb/nostalgic-diva

Usage

For more information, see VdbPlayer.tsx and PlaylistIndex.tsx.

import {
    NostalgicDiva,
    NostalgicDivaProvider,
    PlayerOptions,
} from '@vocadb/nostalgic-diva';

// Callbacks
const handleError = React.useCallback(() => {}, []);
const handlePlay = React.useCallback(() => {}, []);
const handlePause = React.useCallback(() => {}, []);
const handleEnded = React.useCallback(() => {}, []);
const handleTimeUpdate = React.useCallback(() => {}, []);

// Options
const options = React.useMemo(
    (): PlayerOptions => ({
        onError: handleError,
        onPlay: handlePlay,
        onPause: handlePause,
        onEnded: handleEnded,
        onTimeUpdate: handleTimeUpdate,
    }),
    [
        handleError,
        handlePlay,
        handlePause,
        handleEnded,
        handleTimeUpdate,
    ],
);

<NostalgicDivaProvider>
    <NostalgicDiva
        // Supported media types:
        // - "Audio"
        // - "Niconico"
        // - "SoundCloud"
        // - "Vimeo"
        // - "YouTube"
        type="Audio"
        videoId={videoId}
        options={options}
    />;
</NostalgicDivaProvider>
import {
    useNostalgicDiva,
} from '@vocadb/nostalgic-diva';

const diva = useNostalgicDiva();

// Play
await diva.play();

// Pause
await diva.pause();

// Mute
await diva.setMuted(true);

// Unmute
await diva.setMuted(false);

// Seek
await diva.setCurrentTime(seconds);

Imperative functions

FunctionDescription
loadVideo(id: string): Promise<void>Loads a new video into an existing player.
play(): Promise<void>Plays a video.
pause(): Promise<void>Pauses the playback of a video.
setCurrentTime(seconds: number): Promise<void>Sets the current playback position in seconds.
setVolume(volume: number): Promise<void>Sets the volume level of the player on a scale from 0 to 1.
setMuted(muted: boolean): Promise<void>Sets the muted state of the player.
getDuration(): Promise<number \| undefined>Gets the duration of the video in seconds.
getCurrentTime(): Promise<number \| undefined>Gets the current playback position of a video, measured in seconds.

Events

EventDescription
onError(event: any): voidFired when the player experiences some sort of error.
onPlay(): voidFired when the video plays.
onPause(): voidFired when the video is paused.
onEnded(): voidFired when playback reaches the end of a video.
onTimeUpdate(event: TimeEvent): voidFired when the playback position of the video changes.

Lifecycle

  1. PlayerApi.attach
  2. IPlayerApi.loadVideo
  3. PlayerOptions.onLoaded
  4. IPlayerApi.play
  5. PlayerOptions.onPlay
  6. PlayerOptions.onTimeUpdate
  7. IPlayerApi.pause
  8. PlayerOptions.onPause
  9. PlayerOptions.onEnded
  10. PlayerApi.detach

The attach function is called when switching from another player (Audio, Niconico, SoundCloud and YouTube), and the detach function is called when switching to another player. After the detach function is called, you cannot use any imperative functions like loadVideo, play, pause and etc.

References

1.3.0-rc.8

1 year ago

1.3.0-rc.3

2 years ago

1.3.0-rc.6

1 year ago

1.3.0-rc.7

1 year ago

1.3.0-rc.4

2 years ago

1.3.0-rc.5

1 year ago

1.3.0-rc.2

2 years ago

1.3.0-rc.1

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.2.0-rc.1

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago

1.0.0-rc.7

2 years ago

1.0.0-rc.6

2 years ago

1.0.0-rc.5

2 years ago

1.0.0-rc.4

2 years ago

1.0.0-rc.3

2 years ago

1.0.0-rc.2

2 years ago

1.0.0-rc.1

2 years ago