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

audiofile

v1.1.1

Published

Dead easy audio

Downloads

16

Readme

audiofile

Dead easy audio

Factsheet

  • Permissive MIT license
  • Designed both for app UI and game usecases
  • Automatic caching and preloading
  • Audiofiles are replayable
  • Rapid fire sound support
  • Play multiple audiofiles simultaneously
  • Maintains the user's background music on mobile (a killer feature if you want to allow the user to play their own music or podcasts while playing your app/game)
  • Promise support
  • npm/browserify/webpack and global browser module support
  • Desktop and mobile browsers support
  • Chaining support
  • Demo included

Prerequisites

Installation

Install with npm:

npm install audiofile

Alternatively, you can just download audiofile.js and use <script> tags.

Usage

Initialisation

The library exposes the Audiofile object, from which you can create instances:

var af = new Audiofile({ /* options... */ });

The use of the new keyword is optional, so you could instead do:

var audiofile = require('audiofile');
var af = audiofile({ /* options... */ });

The options object can contain these fields:

  • url : The URL of the audio (mandatory if using fetch).
  • fetch : A fetch init options object, allowing you to customise the request headers et cetera (fetch defaults are default here, i.e. plain HTTP GET).
  • load : An alternative to fetch. When specified, fetch is ignored the load promise is used to get the file. The promise is expected to resolve to a Blob.
  • preload : Whether or not to load the audio on object creation (default true).
  • loop : Whether to infinitely loop the audio (default false).
  • volume : The volume of the audio in percent (0 to 1 inclusive; default 1). Values greater than 1 boost the volume.
  • output : Redirects the output of the audiofile to the specified AudioNode, allowing for postprocessing of the audio (system audio is the default).

Audiofile.killUserAudio()

If you want to disable the user's own background audio (e.g. music, podcasts), then you must call this function. This is useful in games with their own background music. By default, the user's background audio is respected.

Audiofile.killUserAudio();

You may alternatively call this:

Audiofile.resetUserAudio();

af.clone()

Calling af.clone() returns a new audiofile that has the same cached audio as the calling audiofile. The cloned audiofile can be played independently of the calling audiofile. This is especially useful for rapid fire usecases.

var af2 = af.clone();

af.load()

Calling af.load() returns a promise that is fulfilled when loading is completed. It's useful if you want to load the audio at a particular time, and it's generally useful for detecting when you can play.

af.load().then(function(){
  console.log('audiofile loaded');
});

If you want a quick synchronous check if the audiofile is loaded or not, just read af.loaded:

if( af.loaded ){
  console.log('do something');
} else {
  console.log('do something else');
}

af.play()

Play the audiofile at its current progress level.

af.play();

af.pause()

Pause the audiofile at its current progress level. It can be resumed via af.play().

af.pause();

af.stop()

Stop playback of the audiofile and resets its progress to the beginning.

af.stop();

af.progress()

Get or set the progress of the audiofile in milliseconds.

var time = af.progress(); // get progress

af.progress( 2*time ); // progress twice as far

af.progressDelta()

Set the progress of the audiofile relatively in milliseconds. Positive values fastforward the audiofile, and negative values rewind the audiofile.

af.progressDelta( 1000 ); // fastforward 1 second

There are also aliases:

af.rewind( 1000 ); // same as af.progressDelta( -1000 )
af.fastforward( 1000 ); // same as af.progressDelta( 1000 )

af.volume()

Get or set the volume level as a percent (0 to 1 inclusive). Values greater than 1 boost the volume.

var vol = af.volume(); // current level

af.volume(0.5); // set level to 50%

af.loop()

Get or set whether looping is enabled. This is useful if you want to reconfigure an audiofile.

var looping = af.loop();

af.loop( false ); // turn off looping