A chat voice note
A voice note from the microphone to a message bubble: a round play button, a slim wave, the length under it, a speed button, and the long pauses skipped. A chat can hold hundreds of them.
The bubble
import { createPlayer } from 'sonicframe/player';
createPlayer({
container: bubble,
src: note.url,
preload: 'none',
peaks: note.peaks, // saved with the message when it was recorded
theme: 'bubble',
a11y: { label: `Voice note from ${note.author}, ${note.length}` },
});
The bubble theme already puts the time and
the speed under the wave, like a messenger. On a phone,
any player turns into this arrangement on its own; see
the voice note on phones.
Recording the note
The recorder gives the note with its length and its peaks. Fold them to 60 or so and send them with the message, so the bubble draws at once and the audio loads only on play.
import { resamplePeaks, silencesFromPeaks } from 'sonicframe';
import { createRecorder } from 'sonicframe/record';
createRecorder({
container: composer,
theme: 'bubble',
async onSend(recording) {
const peaks = resamplePeaks(recording.peaks, 60);
const silences = silencesFromPeaks(peaks, recording.duration, { minDuration: 1.5 });
await chat.sendVoiceNote({ audio: recording.blob, duration: recording.duration, peaks, silences });
},
});
Skip the long pauses
import { skipSilences } from 'sonicframe';
createPlayer({
container: bubble,
src: note.url,
peaks: note.peaks,
theme: 'bubble',
plugins: [skipSilences({ silences: note.silences, dim: true })],
});
Hundreds in one chat
- Each player keeps its pixels only near the screen and loads its peaks there.
- Every frame shares one resize observer, one visibility observer and one animation loop.
- Play buttons are exclusive: starting one note pauses the one playing.
-
With
preload: 'none', nothing is downloaded until a note is played.
See Many players on a page for a live test.