A comprehensive, framework-agnostic volume control component for web applications. Provides seamless integration with HTML5 audio elements and Web Audio API, featuring accessibility support, localStorage persistence, and event-driven architecture.
- Framework Agnostic: Works with vanilla JavaScript, React, and other frameworks
- HTML5 Audio Integration: Direct control of
<audio>elements - Web Audio API Support: Connect to GainNodes for advanced audio processing
- Accessibility: Full ARIA support and keyboard navigation
- Persistence: Automatically saves volume preferences to localStorage
- Event System:
onVolumeChangeandonMuteTogglecallbacks - Customizable: Configurable range, steps, and default values
- Responsive: Works on desktop and mobile devices
No installation required! Just include the JavaScript and CSS files:
<link rel="stylesheet" href="volume-control.css">
<script src="volume-control.js"></script>// Create volume control
const volumeControl = new VolumeControl();
// Add to DOM
document.body.appendChild(volumeControl.getElement());
// Connect to audio element
const audio = new Audio('audio.mp3');
volumeControl.connectAudioElement(audio);
// Listen for events
volumeControl.on('onVolumeChange', (volume, muted) => {
console.log('Volume:', volume, 'Muted:', muted);
});import VolumeControlReact from './volume-control-react';
function AudioPlayer() {
const audioRef = useRef(null);
return (
<div>
<audio ref={audioRef} src="audio.mp3" />
<VolumeControlReact
audioElement={audioRef.current}
onVolumeChange={(volume, muted) => console.log(volume, muted)}
/>
</div>
);
}new VolumeControl({
min: 0, // Minimum volume (0-100)
max: 100, // Maximum volume (0-100)
step: 1, // Step increment
defaultValue: 70, // Initial volume
storageKey: 'volume-level' // localStorage key
});getElement(): Returns the DOM elementconnectAudioElement(audioElement): Connect to HTML5 audioconnectGainNode(gainNode): Connect to Web Audio API GainNodesetVolume(volume, silent): Set volume programmaticallytoggleMute(): Toggle mute stateon(event, callback): Add event listeneroff(event, callback): Remove event listenerdestroy(): Clean up
onVolumeChange(volume, muted): Volume changedonMuteToggle(volume, muted): Mute state changed
<VolumeControlReact
min={0}
max={100}
step={1}
defaultValue={70}
storageKey="volume-level"
audioElement={audioElement}
gainNode={gainNode}
onVolumeChange={(volume, muted) => {}}
onMuteToggle={(volume, muted) => {}}
className="custom-class"
style={{ margin: '10px' }}
/>- Arrow Keys: Adjust volume (←/↓ decrease, →/↑ increase)
- M Key: Toggle mute (when slider focused)
- Enter/Space: Toggle mute (when mute button focused)
- Full ARIA attributes support
- Screen reader compatible
- Keyboard navigation
- High contrast mode support
- Reduced motion support
- Chrome 60+
- Firefox 55+
- Safari 12+
- Edge 79+
See the example.html file for a complete demo with:
- Vanilla JavaScript usage
- HTML5 audio integration
- Event logging
- Programmatic control examples
No build process required! The component works as standalone JavaScript.
MIT License - feel free to use in your projects.
- Initial release
- Framework-agnostic volume control
- HTML5 audio integration
- Web Audio API support
- Accessibility features
- localStorage persistence
- Event system
- React wrapper component
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
For issues and questions, please open an issue on GitHub.