-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvolume-control-react.jsx
More file actions
193 lines (165 loc) · 5.03 KB
/
volume-control-react.jsx
File metadata and controls
193 lines (165 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* React Volume Control Component
* A React wrapper for the VolumeControl class
*/
import React, { useRef, useEffect, useState } from 'react';
const VolumeControlReact = ({
min = 0,
max = 100,
step = 1,
defaultValue = 70,
storageKey = 'volume-level',
audioElement = null,
gainNode = null,
onVolumeChange = null,
onMuteToggle = null,
className = '',
style = {}
}) => {
const containerRef = useRef(null);
const volumeControlRef = useRef(null);
const [volume, setVolume] = useState(defaultValue);
const [muted, setMuted] = useState(false);
useEffect(() => {
// Initialize volume control
volumeControlRef.current = new window.VolumeControl({
min,
max,
step,
defaultValue,
storageKey
});
// Connect audio elements if provided
if (audioElement) {
volumeControlRef.current.connectAudioElement(audioElement);
}
if (gainNode) {
volumeControlRef.current.connectGainNode(gainNode);
}
// Add event listeners
if (onVolumeChange) {
volumeControlRef.current.on('onVolumeChange', (newVolume, isMuted) => {
setVolume(newVolume);
setMuted(isMuted);
onVolumeChange(newVolume, isMuted);
});
}
if (onMuteToggle) {
volumeControlRef.current.on('onMuteToggle', (newVolume, isMuted) => {
setVolume(newVolume);
setMuted(isMuted);
onMuteToggle(newVolume, isMuted);
});
}
// Add default volume change listener to update state
volumeControlRef.current.on('onVolumeChange', (newVolume, isMuted) => {
setVolume(newVolume);
setMuted(isMuted);
});
// Append the volume control to the container
if (containerRef.current) {
containerRef.current.appendChild(volumeControlRef.current.getElement());
}
return () => {
// Cleanup
if (volumeControlRef.current) {
volumeControlRef.current.destroy();
}
};
}, [min, max, step, defaultValue, storageKey]);
// Effect to update audio elements when they change
useEffect(() => {
if (volumeControlRef.current && audioElement) {
volumeControlRef.current.connectAudioElement(audioElement);
}
}, [audioElement]);
useEffect(() => {
if (volumeControlRef.current && gainNode) {
volumeControlRef.current.connectGainNode(gainNode);
}
}, [gainNode]);
return React.createElement('div', {
ref: containerRef,
className: `volume-control-react ${className}`,
style: style
});
};
// PropTypes for better development experience
VolumeControlReact.propTypes = {
min: PropTypes.number,
max: PropTypes.number,
step: PropTypes.number,
defaultValue: PropTypes.number,
storageKey: PropTypes.string,
audioElement: PropTypes.instanceOf(HTMLAudioElement),
gainNode: PropTypes.object, // GainNode from Web Audio API
onVolumeChange: PropTypes.func,
onMuteToggle: PropTypes.func,
className: PropTypes.string,
style: PropTypes.object
};
export default VolumeControlReact;
// Usage example component
export const VolumeControlExample = () => {
const [audioElement] = useState(() => {
// Create a dummy audio element for demonstration
const audio = new Audio();
audio.src = 'https://example.com/audio.mp3'; // Replace with actual audio URL
return audio;
});
const handleVolumeChange = (volume, muted) => {
console.log('Volume changed:', volume, muted);
};
const handleMuteToggle = (volume, muted) => {
console.log('Mute toggled:', muted);
};
return React.createElement(VolumeControlReact, {
audioElement: audioElement,
onVolumeChange: handleVolumeChange,
onMuteToggle: handleMuteToggle,
className: 'custom-volume-control',
style: { margin: '20px' }
});
};
// Hook for using volume control state
export const useVolumeControl = (initialVolume = 70) => {
const [volume, setVolume] = useState(initialVolume);
const [muted, setMuted] = useState(false);
const volumeControlRef = useRef(null);
const initialize = (element, options = {}) => {
if (element && typeof window !== 'undefined' && window.VolumeControl) {
volumeControlRef.current = new window.VolumeControl(options);
if (element instanceof HTMLAudioElement) {
volumeControlRef.current.connectAudioElement(element);
}
volumeControlRef.current.on('onVolumeChange', (newVolume, isMuted) => {
setVolume(newVolume);
setMuted(isMuted);
});
volumeControlRef.current.on('onMuteToggle', (newVolume, isMuted) => {
setVolume(newVolume);
setMuted(isMuted);
});
return volumeControlRef.current.getElement();
}
return null;
};
const setVolumeExternal = (newVolume) => {
if (volumeControlRef.current) {
volumeControlRef.current.setVolume(newVolume);
}
};
const toggleMuteExternal = () => {
if (volumeControlRef.current) {
volumeControlRef.current.toggleMute();
}
};
return {
volume,
muted,
initialize,
setVolume: setVolumeExternal,
toggleMute: toggleMuteExternal,
volumeControl: volumeControlRef.current
};
};