Socket
Socket
Sign inDemoInstall

react-h5-audio-player

Package Overview
Dependencies
9
Maintainers
1
Versions
79
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-h5-audio-player

A customizable React audio player. Written in TypeScript. Mobile compatible. Keyboard friendly


Version published
Weekly downloads
25K
decreased by-1.09%
Maintainers
1
Install size
768 kB
Created
Weekly downloads
 

Readme

Source

logo

React H5 Audio Player

MIT License Monthly download Latest version
  • Audio player component that provides consistent UI/UX on different browsers.
  • Super customizable layout
  • Flexbox css with SVG icons. Mobile friendly.
  • I18n and a11y supported, keyboards events supported.
  • Support Media Source Extensions (MSE) and Encrypted Media Extensions (EME)
  • Written in TypeScript.

screenshot

Live Demo: Storybook

Try it on CodePen: Basic, Playlist

Supported browsers: Chrome, Firefox, Safari, Edge

Migrate from v2.x to v3

Installation

$ npm i react-h5-audio-player

Or

$ yarn add react-h5-audio-player

Usage

import AudioPlayer from 'react-h5-audio-player';
import 'react-h5-audio-player/lib/styles.css';
// import 'react-h5-audio-player/lib/styles.less' Use LESS
// import 'react-h5-audio-player/src/styles.scss' Use SASS

const Player = () => (
  <AudioPlayer
    autoPlay
    src="http://example.com/audio.mp3"
    onPlay={e => console.log("onPlay")}
    // other props here
  />
);
Keyboard shortcuts (When audio player focused)

They can be turned off by setting hasDefaultKeyBindings prop to false

Key bindingAction
SpacePlay/Pause
Rewind
Forward
Volume up
Volume down
LToggle loop
MToggle mute

Props

HTML Audio Tag Native Attributes

PropsTypeDefaultNote
srcstring''
preload'auto' | 'metadata' | 'none''auto'
autoPlaybooleanfalseWon't work on most mobile devices
loopbooleanfalse
mutedbooleanfalse
volumenumber1.0Won't work on most mobile devices
crossOriginstringundefined
mediaGroupstringundefined

More native attributes detail: MDN Audio element

The controls attribute defaults to false and should never be changed to true because this library is already providing UI.

UI/UX Props

PropsTypeDefaultNote
showSkipControlsbooleanfalseShow Previous/Next buttons
showJumpControlsbooleantrueShow Rewind/Forward buttons
showDownloadProgressbooleantrueShow download progress over progress bar
showFilledProgressbooleantrueShow filled (already played) area on progress bar
showFilledVolumebooleanfalseShow filled volume area on volume bar
hasDefaultKeyBindingsbooleantrueWhether has default keyboard shortcuts
autoPlayAfterSrcChangebooleantruePlay audio after src is changed, no matter autoPlay is true or false
volumeJumpStepnumber0.1Indicates the volume jump step when pressing up/down arrow key, volume range is 0 to 1
progressJumpStepnumber5000Deprecated, use progressJumpSteps. Indicates the progress jump step (ms) when clicking rewind/forward button or left/right arrow key
progressJumpStepsobject{ backward: 5000, forward: 5000 }Indicates the progress jump step (ms) when clicking rewind/forward button or left/right arrow key
progressUpdateIntervalnumber20Indicates the interval (ms) that the progress bar UI updates,
listenIntervalnumber1000Indicates the interval (ms) to call the onListened prop during playback
defaultCurrentTimeReactNode'--:--'Default display for audio's current time before src's meta data is loaded
defaultDurationReactNode'--:--'Default display for audio's duration before src's meta data is loaded
timeFormat'auto' | 'mm:ss'
| 'hh:mm:ss'
'auto'Time format for both current time and duration. 'auto' means when duration is greater than one hour, time format is hh:mm:ss, otherwise it's mm:ss
headerReactNodenullHeader of the audio player
footerReactNodenullFooter of the audio player
layout'stacked' | 'horizontal' |
'stacked-reverse' |
'horizontal-reverse'
'stacked'Overall layout of the audio player
customIconsCustomIcons{}Custom icons to replace the default ones
customProgressBarSectionArray<string |
ReactElement>
[CURRENT_TIME,
PROGRESS_BAR,
DURATION]
Custom layout of progress bar section
customControlsSectionArray<string |
ReactElement>
[ADDITIONAL_CONTROLS,
MAIN_CONTROLS,
VOLUME_CONTROLS]
Custom layout of controls section
customAdditionalControlsArray<string |
ReactElement>
[LOOP]Custom layout of additional controls
customVolumeControlsArray<string |
ReactElement>
[VOLUME]Custom layout of volume controls
i18nAriaLabelsI18nAriaLabelsI18nAriaLabelsA configuration object to overwrite the default aria-label on the action buttons
mseObjectnullA configuration object so the player can play audio chunks, MSE streams and encrypted audio (See section about Media Source Extensions in this Readme)
mse.srcDurationnumber-The complete duration of the MSE audio chunks together (this is a key of the mse prop)
mse.onSeekFunction (Event)-The callback to be used when seek happens (this is a key of the mse prop)
mse.srcDurationnumber-The callback to be used when encrypted audio is detected and needs to be decrypted (this is a key of the mse prop)

Event Props

Supported media events: onPlay, onPause, onEnded, onSeeking, onSeeked, onAbort, onCanPlay, onCanPlayThrough, onEmptied, onError, onLoadStart, onLoadedMetaData, onLoadedData, onPlaying, onSuspend, onWaiting, onVolumeChange

Docs: Media Events | MDN

Note: onTimeUpdate is not supported. Please use onListen with listenInterval for better performance.

Other events
PropsTypeDefaultNote
onClickPreviousFunction (Event)nullCalled when click Previous button
onClickNextFunction (Event)nullCalled when click Next button
onListenFunction (Event)nullCalled every listenInterval milliseconds during playback
onPlayErrorFunction (Error)nullCalled when there's error invoking audio.play(), it captures error that onError won't catch
onChangeCurrentTimeErrorFunction ()nullCalled when dragging progress bar or press rewind/forward while the audio hasn't loaded yet

UI Overwrites

Besides using props to change UI, React H5 Audio Player provides built-in class names and SASS/LESS variables for developers to overwrite.

SASS variables

$rhap_theme-color: #868686 !default;   // Color of all buttons and volume/progress indicators
$rhap_background-color: #fff !default; // Color of the player background
$rhap_bar-color: #e4e4e4 !default;     // Color of volume and progress bar
$rhap_time-color: #333 !default;       // Font color of current time and duration
$rhap_font-family: inherit !default;   // Font family of current time and duration

For LESS variables, just replace $ with @. This library supports both.

Status class names

There are some status class names on the audio player's wrapper div. They can be used for overwriting styles.

classNameDescription
rhap_loop--onLoop is on
rhap_loop--offLoop is off
rhap_play-status--pausedPaused status
rhap_play-status--playingPlaying status

For example:

.rhap_play-status--paused .rhap_progress-bar {
  // Overwrite the progress bar style while the audio is paused
}

Advanced Usage

Access to the audio element

You can get direct access to the underlying audio element. First get a ref to ReactAudioPlayer:

this.player = createRef()

<ReactAudioPlayer ref={this.player} />

Then you can access the audio element like this:

this.player.current.audio.current

Media Source Extensions and Encrypted Media Extensions

You can use Media Source Extensions and Encrypted Media Extensions with this player. You need to provide the complete duration, and also a onSeek and onEncrypted callbacks. The logic for feeding the audio buffer and providing the decryption keys (if using encryption) must be set in the consumer side. The player does not provide that logic. Check the StoryBook example to understand better how to use.

Release Notes

https://github.com/lhz516/react-h5-audio-player/releases

How to contribute

Issues and PR's are welcome.

Credits

Keywords

FAQs

Last updated on 28 Dec 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc