🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

apm-react-audio-player

Package Overview
Dependencies
Maintainers
4
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apm-react-audio-player

This is a light react audio player that is wrapped around a HTML5 audio tag, created for use on American Public Media and Minnesota Public Radio's websites.

latest
Source
npmnpm
Version
1.1.3
Version published
Weekly downloads
56
-15.15%
Maintainers
4
Weekly downloads
 
Created
Source

apm-react-audio-player

This is a light react audio player that is wrapped around a HTML5 audio tag, created for use on American Public Media and Minnesota Public Radio's websites.

The library was designed to add a audio player to a body of a story which will not trigger the static audio player.

Alt text

Table of Contents

Dependencies

Installation

  • NPM
  • YARN

Importing

Usage

Breaking Changes

HLS Support

Examples

Publishing

License

Dependencies

As of version 1.0.0, this library has no dependencies for usage.

Installation

There are several ways to install APM Player on your site.

NPM

npm install apm-react-audio-player

or to use yarn:

YARN

yarn add apm-react-audio-player

Importing

ES6 Import

The easiest way to include this in modern javascript, assuming you are using something like and Babel, is to use an import statement. The library uses named exports for all modules.

To import the player module:

import { ReactAudioPlayerInner, useAudioPlayer } from 'apm-react-audio-player';

Usage

Props

See the audio tag documentation for detailed explanations of these attributes.

PropTypeDefaultNotes
titleStringempty stringThe title of the audio track
audioSrcString or Arrayempty stringString: Single audio source URL
Array: Multiple source URLs for progressive enhancement (e.g., ['stream.m3u8', 'audio.aac', 'audio.mp3'])
descriptionStringempty stringThe description of the audio track
audioPlayerRefObjectempty objectA ref object for the audio player
progressBarRefObjectempty objectA ref object for the progress bar
onLoadedMetadataFunction---A function to handle the loadedmetadata event
togglePlayingFunction---A function to toggle the playing state
isPlayingBooleanfalseWhether the audio is currently playing
isMutedBooleanfalseWhether the audio is currently muted
toggleMuteFunction---A function to toggle the mute state
volumeCtrlBooleanfalseWhether to show volume controls
volumeControlFunction---A function to handle volume changes
currentTimeNumbernullThe current time of the audio track
durationNumbernullThe duration of the audio track
rewindControlFunction---A function to rewind the audio track
forwardControlFunction---A function to fast forward the audio track
changePlayerCurrentTimeFunction---A function to change the current time of the audio track
calculateTimeFunction---A function to calculate the time for the audio track
formatCalculateTimeFunction---A function to format the calculated time
playBtnClassStringempty stringThe CSS class for the play button
customStylesObject---Custom styles for the audio player
customHtmlJSX.Element<></>Custom HTML to be rendered inside the player

Example

import { ReactAudioPlayerInner, useAudioPlayer } from 'apm-react-audio-player';

const Example = () => {

  const audioPlayerRef = React.useRef();
  const progressBarRef = React.useRef();

  const {
    onLoadedMetadata,
    calculateTime,
    volumeControl,
    togglePlaying,
    toggleMute,
    isMuted,
    changePlayerCurrentTime,
    play,
    isPlaying,
    isFinishedPlaying,
    currentTime,
    duration,
    formatCalculateTime,
    rewindControl,
    forwardControl
  } = useAudioPlayer(audioPlayerRef, progressBarRef);

    return (
       <ReactAudioPlayerInner
        title={'MPR NEWS'}
        audioSrc={'https://play.publicradio.org/web/o/minnesota/podcasts/art_hounds/2024/06/26/arthounds_art-hounds-franconia_20240626_64.mp3'}
        description={'description'}
        playBtnClass="player-btn player-btn-playpause js-player-play"
        audioPlayerRef={audioPlayerRef}
        progressBarRef={progressBarRef}
           onLoadedMetadata={onLoadedMetadata}
        play={play}
        isPlaying={isPlaying}
        togglePlaying={togglePlaying}
        isMuted={isMuted}
        currentTime={currentTime}
        duration={duration}
        isAudioFinished={isFinishedPlaying}
        toggleMute={toggleMute}
        volumeCtrl={true}
        volumeControl={volumeControl}
        changePlayerCurrentTime={changePlayerCurrentTime}
        rewindControl={rewindControl}
        forwardControl={forwardControl}
        calculateTime={calculateTime}
        formatCalculateTime={formatCalculateTime}
        customHtml={<></>}
    />
  )
}

Breaking Changes

Version 1.0.29+

Removed isLive prop: The isLive prop has been removed from ReactAudioPlayerInner. Live stream detection is now automatic based on the HTML5 audio duration property.

  • Old behavior: You had to manually pass isLive={true} for live streams
  • New behavior: The player automatically detects live streams when duration === Infinity

Migration:

// ❌ Old - Remove the isLive prop
<ReactAudioPlayerInner
  audioSrc="https://example.com/live.m3u8"
  isLive={true}  // <-- Remove this
  // ... other props
/>

// âś… New - Detection is automatic
<ReactAudioPlayerInner
  audioSrc="https://example.com/live.m3u8"
  // ... other props
/>

The UI will automatically adapt based on the audio metadata - no manual configuration needed.

HLS Support

The audio player now supports HLS (HTTP Live Streaming) for both live streams and pre-recorded content using native browser support. The player uses HTML5 <source> tags for progressive enhancement, allowing browsers to fall back to alternate formats if HLS is not supported.

Browser Compatibility

BrowserHLS SupportFallback Behavior
Safari 11+Native ✓N/A
iOS SafariNative ✓N/A
Chrome 142+Native ✓N/A
Edge 142+Native ✓N/A
Mobile Chrome/AndroidNative ✓N/A
Chrome <142NoneAutomatically uses AAC/MP3
FirefoxNoneAutomatically uses AAC/MP3
Edge <142NoneAutomatically uses AAC/MP3
Safari <11NoneAutomatically uses AAC/MP3

Note: Chrome and Edge added native HLS support in version 142 (December 2024). Older versions will automatically skip HLS sources and use alternative formats.

HLS Usage Examples

See examples/hls-example.jsx for a complete working example with multiple HLS scenarios.

Basic HLS with Progressive Enhancement

Provide multiple source formats to ensure compatibility across all browsers:

<ReactAudioPlayerInner
  title="Podcast Episode"
  audioSrc={[
    'https://example.com/episode.m3u8',
    'https://example.com/episode.aac',
    'https://example.com/episode.mp3'
  ]}
  // ... other props
/>

Browsers will try sources in order:

  • Modern browsers (Safari, Chrome 142+, Edge 142+) will play the HLS stream
  • Older browsers will skip the .m3u8 and fall back to AAC or MP3

Live Radio Stream

<ReactAudioPlayerInner
  title="Live Radio"
  audioSrc={['https://stream.example.com/live.m3u8']}
  // ... other props
/>

Single Source (Backward Compatible)

The player maintains backward compatibility with the original single-source API:

<ReactAudioPlayerInner
  title="Audio Track"
  audioSrc="https://example.com/audio.mp3"
  // ... other props
/>

Live Stream Detection

The player automatically detects live streams using the HTML5 audio duration property:

  • Live streams: duration === Infinity

    • Timeline/seek controls are hidden
    • Rewind/forward buttons are hidden
    • "On Air" label is displayed
  • Pre-recorded audio: duration is a finite number

    • Timeline/seek controls are shown
    • Rewind/forward buttons are shown
    • Duration is displayed

No additional props are needed - the player automatically adapts its UI based on whether the content is live or pre-recorded.

Supported Audio Formats

The player automatically detects MIME types from file extensions:

ExtensionMIME Type
.m3u8application/x-mpegURL
.mp3audio/mpeg
.aacaudio/aac
.oggaudio/ogg
.wavaudio/wav

Examples

The examples/ directory contains working demonstrations of the audio player with HLS support.

Running the Examples

Option 1: Using the serve script (recommended)

# Start local server and open examples in browser
yarn serve:examples

# Or using npm
npm run serve:examples

This will start a local server on port 8000 and automatically open the examples page in your browser.

Option 2: Open HTML file directly

  • Open examples/index.html in a web browser
  • The page includes three examples:
    • HLS with progressive enhancement (fallback sources)
    • Live HLS stream (MPR Current)
    • Regular MP3 (backward compatible)

Building the Examples

The examples use a pre-built bundle (examples/bundle.js). If you modify the source code and want to rebuild the examples:

# Rebuild the examples bundle
yarn build:examples

# Or using npm
npm run build:examples

Files:

  • examples/index.html - Main example page with CSS and layout
  • examples/hls-example.jsx - React components demonstrating HLS features
  • examples/bundle.js - Compiled bundle (auto-generated, don't edit directly)
  • examples/hls-test.html - Additional test page for development

Publishing

  • Ensure every merge request and/or change to apm-react-audio-player should always come with an updated version (ex. 1.0.17 to 1.0.18) in the package.json.
  • Once the changes is on Main branch, locally run:
    • git pull main
    • yarn run build or npm run build
    • yarn publish or npm publish

License

MIT © Phanx091

FAQs

Package last updated on 29 Apr 2026

Did you know?

Socket

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