• Stars
    star
    362
  • Rank 117,671 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A React.js wrapper for howler.js (audio player)

react-howler

A React.js wrapper for howler.js (audio player).

ReactHowler has no UI, you have to provide your own UI.

Props can be passed to control playback and react to events such as end, load, play, ...

Travis status Greenkeeper badge

LICENSE MIT js-standard-style npm dependencies

howler.js

howler.js is an audio library for the modern web. It defaults to Web Audio API and falls back to HTML5 Audio.

Usage

npm install --save react-howler

import React, { Component } from 'react'
import ReactHowler from 'react-howler'

class App extends Component {
  // This sound file may not work due to cross-origin setting
  render () {
    return (
      <ReactHowler
        src='http://goldfirestudios.com/proj/howlerjs/sound.ogg'
        playing={true}
      />
    )
  }
}

For a full working example please see it in App.js

Demo

http://khoanguyen.me/react-howler/ or

git clone http://github.com/thangngoc89/react-howler
npm install
npm run example:react
open http://localhost:3000

Props

Prop Default Description
src The sources to the track(s) to be loaded for the sound (URLs or base64 data URIs). These should be in order of preference, howler.js will automatically load the first one that is compatible with the current browser. If your files have no extensions, you will need to explicitly specify the extension using the format property.
Updating the src prop on the fly will destroy any currently playing howler instance and create a new one with the new src. Updating other props while keeping the src the same will maintain the current howler instance.
preload true Set to true to begin downloading the audio file if it is not loaded yet.
playing true Set to true or false to pause or play the media.
Setting to true on initial load will play the audio immediately after it is loaded
loop false Set to true or false to enable/disable loop
mute false Set to true or false to mute/unmute current audio
volume 1.0 The volume of the specific howl, from 0.0 to 1.0
rate 1 The initial playback rate (speed)
html5 false Set to true to force HTML5 Audio. This should be used for large audio files so that you don't have to wait for the full file to be downloaded and decoded before playing.
format [] howler.js automatically detects your file format from the extension, but you may also specify a format in situations where extraction won't work (such as with a SoundCloud stream).
xhr {} When using Web Audio, howler.js uses an XHR request to load the audio files. If you need to send custom headers, set the HTTP method or enable withCredentials (see reference), include them with this parameter. Each is optional (method defaults to GET, headers default to null and withCredentials defaults to false). For example:
<ReactHowler src="sound.webm" xhr={{ method: 'POST', headers: { Authorization: 'Bearer:' + token, }, withCredentials: true, }} />
onPlay noop Called when audio starts or resumes playing
onPause noop Called when audio is paused
onVolume noop Called when volume is changed
onStop noop Called when audio is stopped
onLoad noop Called when audio is loaded (buffered)
onLoadError noop Called when an error occurs whilst attempting to load media
onEnd noop Called when media finishes playing
onSeek noop Called when the sound has been seeked. The first parameter is the ID of the sound. onSeek(id)
onPlayError noop Called when the sound is unable to play. The first parameter is the ID of the sound and the second is the error message/code. onPlayError(id, errorCode)

Methods

duration([id])

Get the duration of the audio source. Will return 0 until after the load event fires.

  • id: Number optional The sound ID to check. Passing an ID will return the duration of the sprite being played on this instance; otherwise, the full source duration is returned.

load()

When preload is true this is automatically called. When setting preload to false, you can call load() manually before playing sounds (useful for lazy loading large files on slow networks). If you attempt to play a sound that's not loading or loaded with preload react-howler will automatically call load().

Tip: If you're calling load() manually, check the load status with howlerState()

seek([seek])

Get/set the position of playback for a sound.

  • seek: Number optional The position to move current playback to (in seconds).

howlerState()

Check the load status of the Howl, returns a string unloaded, loading or loaded.

This is an alias for Howler's state() function

stop([id])

Stops playback of sound, resetting seek to 0.

  • id: Number optional The sound ID. If none is passed, all sounds in group are stopped.

rate([value], [id])

Speeds up/down an audio playback. Calling with no arguments will reset all howls to default rate.

  • value: Number optional The playback rate. If empty, will default to 1.
  • id: Number optional The sound ID. If empty, all sounds in group get updated.

Other howler.js methods

If you need to use other howler.js methods that are not included in this wrapper you can access the howler instance directly via howler

import React, { Component } from 'react'
import ReactHowler from 'react-howler'

class App extends Component {
  getHowler () {
    this.player.howler
  }

  getDuration () {
    this.player.duration()
  }

  getSeek () {
    this.player.seek()
  }

  setSeek () {
    this.player.seek(0.5)
  }
  // This sound file may not work due to cross-origin setting
  render () {
    return(
      <ReactHowler
        src='http://goldfirestudios.com/proj/howlerjs/sound.ogg'
        playing={true}
        ref={(ref) => (this.player = ref)}
      />
    );
  }
}

Howler global core methods

Howler global methods are avaiable in window scope. Please refer to howler's documentation

Usage:

window.Howler.mute(true) // Mute all sounds

Linting

This project uses standard code style.

js-standard-style

npm run lint

Audio files in examples

sound.ogg

Taken from howler.js demo page Sound file direct link: sound.ogg

sound2.ogg

Fingerstyle Bass line over an Am chord progression By Serolillo (Own work) [CC BY 2.5 (http://creativecommons.org/licenses/by/2.5)], via Wikimedia Commons

License

MIT

More Repositories

1

bs-ant-design

Bucklescript + ReasonReact binding Ant Design
OCaml
62
star
2

react-code-playground

A playground component built for React.js
JavaScript
20
star
3

denu

A toy OCaml builder
OCaml
17
star
4

ocaml-slug

Url safe slug generator for OCaml
OCaml
17
star
5

reason-react-boilerplate

JavaScript
13
star
6

markdown-it-react-renderer

React renderer for Markdown-it
JavaScript
11
star
7

blog

My personal blog
JavaScript
11
star
8

openit.io

Open source code of a package on npm/rubygems/crates and more
JavaScript
10
star
9

sketch-v2

OCaml
9
star
10

statinamic-theme-lumen

Statinamic theme with typography, colorful suitable for blog
JavaScript
9
star
11

discourse-vietnamese-slug

Add Vietnamese Slug to Discourse
Ruby
7
star
12

awesome-reasonreact

A list of resources for building a website in ReasonReact
6
star
13

pushbullet-laravel-socialite

Add Pushbullet as Oauth2 provider to Laravel Socialite
PHP
5
star
14

electron-react-app

JavaScript
5
star
15

dnh-cpp

Website cho khóa học C++ của Dạy Nhau Học
JavaScript
5
star
16

redex-phenomic

OCaml
4
star
17

vietnamese-unicode-toolkit

A toolkits to convert all charaters into unicode in Vietnamese
JavaScript
4
star
18

thangngoc89.github.io

Generated version of my blog. Address: http://khoanguyen.me. Code:
HTML
3
star
19

reason-react-router

OCaml
2
star
20

dxd-file-format

Necessary data for reverse-engineering DXD file format
2
star
21

nyc-mock-fs

JavaScript
2
star
22

context

OCaml
2
star
23

reason-react-shade

CSS
1
star
24

graphql_ppx_bin

Install graphql_ppx via npm
Makefile
1
star
25

yhoc.co

A custom google search template
CSS
1
star
26

wp-tree-shaking

OCaml
1
star
27

nhakhoaphuocbinh

JavaScript
1
star
28

left-pad

High performance left-pad using binary operator
JavaScript
1
star
29

ng-admin-for-laravel

This help you can create a quick AngularJS backend with ng-admin for Laravel
PHP
1
star
30

eloquent-N2N-benchmark

Benchmark Eloquent N2N relationship
PHP
1
star
31

quick_tools

My automation tools collection
PHP
1
star
32

maxperf

Javascript max perf FTW
JavaScript
1
star
33

hoidapyhoc

Hỏi Đáp Y Học Website
Java
1
star
34

bsb-theme-react

OCaml
1
star
35

medsouls

JavaScript
1
star
36

reason-react-starter

ReasonReact starter project
JavaScript
1
star
37

seaade

SEAADE 2016 official website
JavaScript
1
star
38

esy-argon2

C
1
star
39

bs-bindings

An umbrella repository for all of my bindings
C++
1
star
40

hei-sb

JavaScript
1
star