
Security News
/Research
Wallet-Draining npm Package Impersonates Nodemailer to Hijack Crypto Transactions
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
audio-hooks
Advanced tools
A React hooks library for managing audio playback with advanced controls and features.
A React hooks library for managing audio playback with advanced controls and features.
npm install audio-hooks
import { useAudioList, AudioProvider } from 'audio-hooks/react'
function AudioPlayer() {
const { state, controls } = useAudioList([
'audio1.mp3',
'audio2.mp3',
'audio3.mp3'
])
return (
<AudioProvider>
<div>
<h3>Now Playing: Track {state.playingIndex + 1}</h3>
<p>{Math.floor(state.currentTime)}s / {Math.floor(state.duration)}s</p>
<div>
<button onClick={controls.prev}>⏮️</button>
<button onClick={controls.togglePlay}>
{state.playing ? '⏸️' : '▶️'}
</button>
<button onClick={controls.next}>⏭️</button>
</div>
<div>
<button onClick={() => controls.rewind()}>⏪ 5s</button>
<button onClick={() => controls.fastForward()}>⏩ 5s</button>
</div>
<input
type="range"
min="0"
max="1"
step="0.01"
value={state.volume}
onChange={(e) => controls.setVolume(Number(e.target.value))}
/>
</div>
</AudioProvider>
)
}
// 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), {
onEnded: () => {
console.log('Track ended')
}
})
return {
state,
controls,
playingTrack: tracks[state.playingIndex],
totalTracks: tracks.length,
}
}
// components/AudioPlayer.tsx
import { usePlayList } from 'hooks/usePlayList'
const playList = {
name: 'My Playlist',
tracks: [
{ name: 'Song 1', artist: 'Artist 1', album: 'Album 1', url: 'song1.mp3' },
{ name: 'Song 2', artist: 'Artist 2', album: 'Album 2', url: 'song2.mp3' },
],
}
const AudioPlayer = () => {
const {
state,
controls,
playingTrack,
totalTracks,
} = usePlayList(playList)
return (
<AudioProvider>
<div className="audio-player">
<div className="track-info">
<h3>{playingTrack?.name || 'No track selected'}</h3>
<p>{playingTrack?.artist} - {playingTrack?.album}</p>
<span>Track {state.playingIndex + 1} of {totalTracks}</span>
</div>
<div className="playback-controls">
<button onClick={controls.prev}>⏮️</button>
<button onClick={controls.togglePlay}>
{state.playing ? '⏸️' : '▶️'}
</button>
<button onClick={controls.next}>⏭️</button>
<button onClick={() => controls.nextPlayMode()}>
Mode: {state.playMode}
</button>
</div>
<div className="seek-controls">
<button onClick={() => controls.rewind(10000)}>⏪ 10s</button>
<input
type="range"
min="0"
max={state.duration}
value={state.currentTime}
onChange={(e) => controls.seek(Number(e.target.value))}
/>
<button onClick={() => controls.fastForward(10000)}>⏩ 10s</button>
</div>
<div className="audio-settings">
<label>
Volume: {Math.round(state.volume * 100)}%
<input
type="range"
min="0"
max="1"
step="0.01"
value={state.volume}
onChange={(e) => controls.setVolume(Number(e.target.value))}
/>
</label>
<label>
Speed: {state.playbackRate}x
<input
type="range"
min="0.5"
max="3"
step="0.1"
value={state.playbackRate}
onChange={(e) => controls.setPlaybackRate(Number(e.target.value))}
/>
</label>
</div>
</div>
</AudioProvider>
)
}
useAudioList(urls, options)
Parameters:
urls: string[]
- Array of audio URLsoptions?: AudioOptions
- Optional configuration object
onEnded?: () => void
- Callback fired when a track endsReturns: UseAudioListReturn
state: AudioState
- Current playback statecontrols: AudioControls
- Playback control methodsProperty | Type | Description |
---|---|---|
audios | string[] | Array of audio URLs |
playing | boolean | Whether audio is currently playing |
playingIndex | number | Index of currently playing track (-1 if none) |
currentTime | number | Current playback position in seconds |
duration | number | Total duration of current track in seconds |
volume | number | Current volume level (0-1) |
playbackRate | number | Current playback speed (0.5-3.0) |
playMode | PlayMode | Current playback mode |
Method | Parameters | Description |
---|---|---|
play() | - | Start playing the current audio track |
pause() | - | Pause the current audio track |
togglePlay() | - | Toggle between play and pause |
next() | - | Skip to next track based on current play mode |
prev() | - | Go to previous track based on current play mode |
seek(time) | time: number | Seek to specific time in seconds |
fastForward(ms?) | ms?: number | Fast forward by milliseconds (default: 5000) |
rewind(ms?) | ms?: number | Rewind by milliseconds (default: 5000) |
setVolume(volume) | volume: number | Set volume (0-1) |
nextPlayMode(mode?) | mode?: PlayMode | Cycle through or set specific play mode |
playTrack(index) | index: number | Play specific track by index |
setPlaybackRate(rate) | rate: number | Set playback speed (0.5-3.0) |
setAudioList(urls) | urls: string[] | Update the audio URL list |
switchAudio(index) | index: number | Switch to track without playing |
type PlayMode =
| 'Shuffle' // Random track order
| 'SingleOnce' // Play current track once, then stop
| 'SingleLoop' // Repeat current track infinitely
| 'SequentialOnce' // Play all tracks once, then stop
| 'SequentialLoop' // Repeat entire playlist infinitely
The hook automatically manages audio resources and cleans up when components unmount. Audio elements are pooled for efficient reuse.
const { state, controls } = useAudioList(urls, {
onEnded: () => console.log('Track finished'),
})
// Handle play errors
const handlePlay = async () => {
try {
await controls.play()
} catch (error) {
console.error('Playback failed:', error)
}
}
MIT © Lee
Contributions are welcome! Please feel free to submit a Pull Request.
git clone https://github.com/hey-lee/audio-hooks
cd audio-hooks
npm install
npm run dev
FAQs
A React hooks library for managing audio playback with advanced controls and features.
We found that audio-hooks demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.