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-spotify-web-playback-sdk-headless

v1.4.2

Published

A headless react library for the Spotify web playback SDK. Quite a name

Downloads

39

Readme

react-spotify-web-playback-sdk-headless

A headless react library for the Spotify web playback SDK. Quite a name

Why

This library was build for the Kokopelli project, since the regular Spotify SDK is not very user friendly. It does not support playing music directly and it cannot refresh access tokens. This library adds simple functions to do exactly that.

How to use

Usage is very simple, just add this to your project, preferably as high in the tree as possible (this is since re-renders can and will break the SDK)

import SpotifyWebPlayback from 'react-spotify-web-playback-sdk-headless'

const ACCESS_TOKEN="<access_token>"

const Index = () => {
  const player = useRef<SpotifyWebPlayback>(null)

  const playSong = () => {
    player.current?.play('spotify:track:4k1OADTXVmuABulPYY9IIu')
  }

  return <div>
    <button onClick={playSong}>
      Play a song
    </button>

    <SpotifyWebPlayback accessToken={ACCESS_TOKEN} name="cool player" volume={50}>
  </div>
}

The code above will initialize the player (if the token is valid) and pressing the button will play a song from the browser!

To access methods use the ref as seen above.

Props

No props are required, but the player will not play when some combinations are not present.

| Prop | Value | Description | |------|-------|-------------| | accessToken | string | The spotify access token to use, required the streaming scope. | | refreshToken| string | The spotify refresh token to use, this is only used when refreshTokenAutomatically is present | | name | string | Name for the player (as seen from Spotify) | | volume | number | The volume of the player, from 1 to 100. This can be changed at all times | | logging | boolean | To enable logging | | debug | boolean | To enable debug logging, this is quite spammy | | refreshTokenAutomatically | boolean | To automatically refresh the accessToken, this requires refreshTokenUrl and refreshToken| | refreshTokenUrl | string | The URL running the token server, see below | | getOAuthToken | (cb: (token: string) => void) => void | This function is called by the SDK when it needs a fresh token, this can be used to implement token refreshing manually |

Listeners

The SDK has a lot of event you can listen to, so this is also exposed by this library. You can add a listener by passing it as a prop to the SpotifyWebPlayback object. For more information see the Spotify Developer Reference

| Listener | Description | |----------|-------------| | onReady | Is called when the player is ready | | onNotReady | Is called when something went horribly wrong with initialization | | onPlayerStateChanged | The most interesting event, is called when a song is started, paused, finished etc. | | onInitializationError | Speaks for itself | | onAuthenticationError | Speaks for itself | | onAccountError | When a Spotify account doesnt have a premium subscription | | onPlaybackError | Speaks for itself | | onAutoplayFailed | Happens when the browser denies autoplay, this can be solved by adding a button that starts playback, or enabling autoplay | | songFinished | New listener, is called when a song is finished with playing | | onTokenRefresh | New listener, is called when a token is refreshed |

Class methods

Most functions are from the SDK and docs for that can be found on the Spotify Developer Reference. But there are some new methods:

| Method | Type | Description | | setVolume | (volume: number) => void | Change the volume of the player | | play | (spotifyId: string) => void | Play a song on the player, takes a spotify id in the spotify:track:[id] format, or with just the id | | queueSongs | (spotifyIds: string[]) => void | Adds a list of songs to the current playback queue, same format as SpotifyWebPlayback#play |

Automatic Token Refreshing

Token refreshing has to be done by a server, which can be found here To use this you need to add some props to your object. If you use this then you cannot use getOAuthToken

<SpotifyWebPlayback accessToken="..." refreshToken="..." refreshTokenAutomatically refreshTokenUrl="<url_to_server>/spotify/auth/refresh" />

For an example of this check the 'test' story in the repo