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-nowplaying

v1.5.1

Published

A cross-browser cross-device friendly React context to auto-play audio in a browser.

Downloads

997

Readme

React NowPlaying

Don’t Be the Grinch! Or Cross-Browser Compatibility Problems — Mike Mackrory

NPM Version Discord

A cross-browser cross-device friendly React context to auto-play audio in a browser.

Demo

See it in action here: https://react-nowplaying.vercel.app

Getting Started

Installation

npm i react-nowplaying

Add to your project

Import the React context provider;

import { NowPlayingContextProvider } from "react-nowplaying";

And, just as any custom context provider, wrap the part of your app you need to be context aware in the custom provider's tags.

// layout.tsx

//...

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html lang="en">
      <body className={inter.className}>
        <NowPlayingContextProvider>{children}</NowPlayingContextProvider> // e.g. custom provider tags wrap entire body of app
      </body>
    </html>
  );
}

Play Blob data

Now, you can provide your data Blob to the play function.

export default function MyComponent() {
  const { play } = useNowPlaying();

  const playAudio = () => {
    // get your audio blob
    play(blob, "audio/mp3");
  };

  return (
    <button type="button" onClick={() => playAudio()}>
      Play Audio
    </button>
  );
}

Play a URL string

Now, you can provide your url string to the play function.

export default function MyComponent() {
  const { play } = useNowPlaying();

  const playAudio = () => {
    // get your audio blob
    play("https://your-file.com/audio.mp3", "audio/mp3");
  };

  return (
    <button type="button" onClick={() => playAudio()}>
      Play Audio
    </button>
  );
}

Audio Controls

Using our context's hook, we return native controls from the <audio> native, with the exception of play(). If you pause() your audio, you can now use resume().

The player property is also available from the hook, which is a native reference to our <audio> tag, giving you the ability to add your own event listeners and control it as you need to.

export default function MyComponent() {
  const { pause, resume } = useNowPlaying();

  //... build your controls
}

How It Works

In our recent project, we faced challenges with automatically playing audio across different browsers and devices, including Safari, Firefox, and all iOS devices. Our solution involved complex adjustments to enable automatic playback of audio files (in Blob format) received from our API. Instead of utilizing the Web Audio API, we opted for a workaround that involves a hidden audio element on the webpage. Here’s how it works:

  • When audio is ready to play, the webpage updates a hidden audio element with the audio source and type, allowing the audio to play automatically.
  • We created a custom hook that exposes an instance of the audio element, making it easier to manage playback. The hook extends the interface of an audio element.

This approach allows for seamless audio playback across a wide range of browsers and devices, ensuring users have a consistent experience.

Getting Help

We love to hear from you so if you have questions, comments or find a bug in the project, let us know! You can either:

Author

Deepgram

Known Issues

For a list of known issues and potential bugs, please visit our Issues page. We regularly update this section as new issues are discovered and resolved.

Collaborating

We welcome contributions from the community. For instructions on how to get involved, please read our Collaborating Guide.

Code of Conduct

To ensure a welcoming and safe environment for all contributors, we adhere to a Code of Conduct. All participants in our project are expected to read and follow our Code of Conduct. We are committed to making participation in this project a harassment-free experience for everyone.

Changelog

Stay updated with the changes and improvements made to our project by checking out our Changelog. This document includes a detailed list of changes, including new features, bug fixes, and other important updates.

License

This project is licensed under the MIT License. See the LICENSE file for details.