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-track-player

v1.0.16

Published

A fully fledged audio module created for music apps

Downloads

98

Readme

react-track-player 🎧

npm version

PRs Welcome

Cross Platform audio streaming Module for React native. Provides audio playback, external media controls, background mode and more!

Features

  • [x] Lightweight - Complete audio library without managing queue, the code is optimized to use least resources
  • [x] Background play - Audio can be playing in the background and media can be controlled externally as well
  • [x] Local or network, files or streams - Support for online streaming and offline files
  • [x] Multi-platform - Supports Android, iOS and Web
  • [x] Supports React Hooks 🎣 - Includes React Hooks for common use-cases so you don’t have to write them

Example

import { TrackPlayer } from "react-track-player";

load = () => {
  TrackPlayer.load({
    title: "Awesome song",
    artist: "Mr. Awesome",
    album: "Awesome songs only",
    cover: "https://source.unsplash.com/random",
    path: "https://dl.dropboxusercontent.com/s/8avcnxmjtdujytz/Sher%20Aaya%20Sher.mp3?dl=0",
  }).then(() => {
    console.log("audio loaded");
  });
};

play = () => {
  TrackPlayer.play();
};

pause = () => {
  TrackPlayer.pause();
};

Install

Install the module using yarn or npm

Using npm

npm install react-track-player --save

Using Yarn

yarn add react-track-player

Troubleshooting

Issues while installing might be listed in troubleshooting page.

Getting started

First of all, you need to set up the player. This usually takes less than a second:

import TrackPlayer from 'react-track-player';

await TrackPlayer.setup({})
// The player is ready to be used

Player Information

Event handler

import { addEventListener } from "react-track-player";

subscription = addListener("media", function (event) {
  // handle event
  console.log("from event listener", event);
  if (event == "skip_to_next") {
    skipToNext();
  } else if (event == "skip_to_previous") {
    skipToPrevious();
  } else if (event == "completed") {
    skipToNext();
  } else {
    updateStatus();
  }
});

subscription.remove();

API

Load Audio into track player

TrackPlayer.load({
    title: "Awesome song",
    artist: "Mr. Awesome",
    album: "Awesome songs only",
    cover: "https://source.unsplash.com/random",
    path: "https://dl.dropboxusercontent.com/s/8avcnxmjtdujytz/Sher%20Aaya%20Sher.mp3?dl=0",
})

Load a track into track player. Audio wont be played untill play() function is called.

Returns: Promise - The promise resolves if it is sucess

| Param | Type | Description | | ------ | --------- | --------- | | title | string | Track title | | artist | string | Name of the artist | | album | string | Name of the album | | cover | string | Audio cover image path | | path | string | Audio url or path for native audio files |

State

TrackPlayer.getState()

This method can be used to get the state of the player

Returns: Promise

Get Position

TrackPlayer.getPosition();

Get track player progress position.

Returns: Promise

Get Duration

TrackPlayer.getDuration();

Gets the duration of the current track in seconds.

Returns: Promise

Destroy

TrackPlayer.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 setup() again. Get track duration.

Returns: Promise

Hooks

usePlaybackState

usePlaybackState gives the state of the player

useProgress

useProgress accepts an interval to set the rate (in miliseconds) to poll the track player’s progress. The default value is 1000 or every second.

import React from 'react';
import { Text, View } from 'react-native';
import { TrackPlayer, useProgress } from 'react-track-player';

const MyComponent = () => {
  const { position, duration } = useProgress()

  return (
    <View>
    <Slider
          style={{ width: '100%', height: 40 }}
          minimumValue={0}
          maximumValue={duration}
          value={position}
          onSlidingComplete={value => {
            TrackPlayer.pause();
            TrackPlayer.seekTo(value)
            TrackPlayer.play();
          }}
        />
      <Text>Track progress: {position} seconds out of {duration} total</Text>
    </View>
  )
}