npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-web-track-player

v1.4.3

Published

### Inspired by [react-native-track-player](https://github.com/react-native-kit/react-native-track-player)

Downloads

6

Readme

React Web Track Player

Inspired by react-native-track-player

Summary

Examples

Playing Music Example

import React, { useEffect } from 'react';

import player, { useTrackPlayerProgress, usePlaybackTrackChanged, usePlaybackState } from '../index';

function App() {
    const { position, bufferedPosition, duration } = useTrackPlayerProgress();

    const currentTrack = usePlaybackTrackChanged();

    const playbackState = usePlaybackState();

    useEffect(() => {
        <!-- The track id has to be unique -->
        const tracks = [
            {
                id: 1,
                url: '',
                title: 'l',
                artist: '',
                album: '',
                artwork: [
                    { 
                        src: '',
                        sizes: '512x512',
                        type: 'image/png'
                    },
                ],
            },
            {
                id: 2,
                url:'',
                title: '',
                artist: '',
                album: '',
                artwork: [
                    { 
                        src: '',
                        sizes: '512x512',
                        type: 'image/png'
                    },
                ],
            }
        ];

        <!-- Start player and set MediaSessionAPI Capabilities -->
        player.setupPlayer({
            capabilities: [
                [
                    'play',
                    async () => {
                    await player.play();
                    },
                ],
                [
                    'pause',
                    () => {
                    player.pause();
                    },
                ],
                [
                    'previoustrack',
                    async () => {
                    await player.skipToPrevious();
                    },
                ],
                [
                    'nexttrack',
                    async () => {
                    await player.skipToNext();
                    },
                ],
                [
                    'stop',
                    () => {
                    player.reset();
                    },
                ],
            ],
        });
        
        <!-- Add tracks to the queue -->
        player.add(tracks);
    }, []);

    useEffect(() => {
        <!-- Will be updated every time a track change -->
        console.log(currentTrack);
    }, [currentTrack]);

    useEffect(() => {
        <!-- Will be updated every time a playback state change -->
        console.log(playbackState);
    }, [playbackState]);

    useEffect(() => {
        <!-- Will be updated every time track position state change -->
        console.log(position, bufferedPosition, duration);
    }, [position, bufferedPosition, duration]);

    async function handlePlay() {
        <!-- After adding the tracks to the queue call this function with the track position -->
        await player.play(0);
    }

    function handlePause() {
        player.pause();
    }

    async function handleSkip() {
        await player.skipToNext();
    }

    async function handlePrev() {
        await player.skipToPrevious();
    }

    function handleSeek() {
        <!-- Seek to 10 seconds -->
        player.seekTo(10); 
    }

    return (
        <div>
            <div>
                <h1>{currentTrack && currentTrack.title}</h1>
                <div>
                    {position}

                    |

                    {duration}
                </div>
            </div>

            <button type="button" onClick={handlePlay}>Play</button>

            <button type="button" onClick={handlePause}>Pause</button>

            <button type="button" onClick={handleSkip}>Skip to Next</button>

            <button type="button" onClick={handlePrev}>Skip to Prev</button>

            <button type="button" onClick={handleSeek}>Seek To</button>

        </div>
    );
}

export default App;

Set MediaSessionAPI Actions

  <!-- Start player and set MediaSessionAPI Capabilities -->
  player.setupPlayer({
      capabilities: [
          [
              'play',
              async () => {
              await player.play();
              },
          ],
          [
              'pause',
              () => {
              player.pause();
              },
          ],
          [
              'previoustrack',
              async () => {
              await player.skipToPrevious();
              },
          ],
          [
              'nexttrack',
              async () => {
              await player.skipToNext();
              },
          ],
          [
              'stop',
              () => {
              player.reset();
              },
          ],
      ],
  });

Functions

Player Functions

setupPlayer(options)

Initializes the player with the specified options.

You should always call this function (even without any options set) before using the player to make sure everything is initialized.

| Param | Type | Description | | -------------------- | -------- | ------------- | | options | object | The options | | options.capabilities | array | The MediaSessionAPI Actions

destroy()

Destroys the player, cleaning up its resources. After executing this function, you won't be able to use the player anymore, unless you call setupPlayer() again.

Queue Functions

add(tracks)

Adds one or more tracks to the queue.

| Param | Type | Description | | -------------- | -------- | ------------- | | tracks | array of | The tracks that will be added |

remove(tracks)

Removes one or more tracks from the queue.

| Param | Type | Description | | ------ | -------- | ------------- | | tracks | array of track ids | The tracks that will be removed |

skipToIndex(index)

Skips to a track in the queue based on the index.

Returns: Promise

| Param | Type | Description | | ------ | -------- | ------------- | | index | number | The track index |

skipToNext()

Skips to the next track in the queue.

Returns: Promise

skipToPrevious()

Skips to the previous track in the queue.

Returns: Promise

reset()

Resets the player stopping the current track and clearing the queue.

getTrack(id)

Gets a track object from the queue.

Returns: object

getCurrentTrack()

Gets the current track object from the queue.

Returns: object

getQueue()

Gets the whole queue

Returns: Array

play()

Plays or resumes the current track.

Returns: Promise

pause()

Pauses the current track.

stop()

Stops the current track.

seekTo(seconds)

Seeks to a specified time position in the current track.

| Param | Type | Description | | ------- | -------- | ----------------------- | | seconds | number | The position in seconds |

setVolume(volume)

Sets the volume of the player.

| Param | Type | Description | | ------ | -------- | --------------------------------- | | volume | number | The volume in a range from 0 to 1 |

getVolume()

Gets the volume of the player (a number between 0 and 1).

Returns: number

getDuration()

Gets the duration of the current track in seconds.

Note: Is recommended to add the track duration on the track object

Returns: number

getPosition()

Gets the position of the player in seconds.

Returns: number

getBufferedPosition()

Gets the buffered position of the player in seconds.

Returns: number

getPlaybackState()

Gets the state of the player. "STATE_PLAYING | STATE_PAUSED | STATE_STOPPED | STATE_NONE"

Returns: string