Socket
Book a DemoInstallSign in
Socket

audio-hooks

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

audio-hooks

A React hooks library for managing audio playback with advanced controls and features.

0.0.4
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

A React hooks library for managing audio playback with advanced controls and features.

License: MIT

Installation

npm install audio-hooks

Features

  • Multiple audio playback modes (RepeatOne, RepeatAll, Shuffle)
  • Volume control
  • Playback rate control (0.5x to 3.0x)
  • Track seeking
  • Previous/Next track navigation
  • Built on Web Audio API
  • TypeScript support

Usage

// hooks/usePlayList.ts
import { useAudioList, AudioProvider } from 'audio-hooks/react'

export interface PlayList {
  id: string
  name: string
  owner?: string
  cover?: string
  description?: string
  tracks: Track[]
  createdAt?: string
  updatedAt?: string
  duration?: number
  isPublic?: boolean
  tags?: string[]
}

type Track = {
  name: string
  artist: string
  album: string
  cover?: string
  url: string
}


export const usePlayList = ({ tracks = [] }: PlayList) => {
  const {
    state,
    controls,
  } = useAudioList(tracks.map(({ url }) => url))

  return {
    state,
    controls,
    playingTrack: tracks[state.playingIndex],
  }
}

// components/AudioPlayer.tsx
import { usePlayList } from 'hooks/usePlayList'

const playList = {
  name: 'My Playlist',
  tracks: [],
  // ...
}

const AudioPlayer = () => {
  const {
    state,
    controls,
    playingTrack,
  } = usePlayList(playList)

  return (
    <AudioProvider>
      <div>
        Playing: {state.playing ? 'Yes' : 'No'}
        Current Track: {state.playingIndex + 1} of {playList.tracks.length}
        Time: {state.currentTime} / {state.duration}
      </div>
      
      <div>
        <button onClick={controls.play}>Play</button>
        <button onClick={controls.pause}>Pause</button>
        <button onClick={controls.prev}>Previous</button>
        <button onClick={controls.next}>Next</button>
      </div>
    </AudioProvider>
  )
}

API

useAudioList(urls: string[])

Returns an object containing the current state and controls for audio playback.

state

  • volume: number - Current volume level between 0 and 1.
  • playing: boolean - Whether audio is currently playing.
  • duration: number - Total duration of current track in seconds.
  • currentTime: number - Current playback position in seconds.
  • playbackRate: number - Current playback speed between 0.5 and 3.0.
  • playingIndex: number - Index of currently playing track in playlist.

controls

  • play(): Start playing the current audio track.
  • pause(): Pause the current audio track.
  • prev(): Go to previous track based on current play mode.
  • next(): Skip to next track based on current play mode.
  • setVolume(volume: number): Set audio volume between 0 and 1.
  • seek(time: number): Seeks to the specified time in seconds.
  • playTrack(index: number): Play specific track by index.
  • setPlaybackRate(rate: number): Set playback rate between 0.5 and 3.0.
  • nextPlayMode(mode?: PlayMode): Change the play mode to the next mode in the list.
    type PlayMode = 'RepeatAll' | 'RepeatOne' | 'Shuffle'
    

setList(urls: string[])

Update the list of audio URLs.

License

MIT © Lee

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Keywords

web

FAQs

Package last updated on 15 Jul 2025

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

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.