Wie man eine Burbuja von Notas von Voz mit Formen von Onda Interaktive und Selector von Geschwindigkeit

Schnellübersicht

Eine burbuja von nota von voz tipo mensajería muestra ein arreglo von barras von amplitud SVG que se colorean progresivamente según der tiempo von reproducción actual (currentTime / duration). Die Nutzer pueden hacer clic in cualquier barra für saltar inmediatamente a jener segundo vom Audio, mientras que ein Button interaktiv cicla die Geschwindigkeit von reproducción zwischen 1x, 1.5x und 2x modificando die Eigenschaft native audio.playbackRate.

Interaktive Demo Live-Vorschau
0:42

Schritt 1: Estructuración vom Marcado Stil Mensajería Instantánea

Erstellt der contenedor von burbuja mit der Button von play, die barras von die Form von onda SVG und der conmutador von Geschwindigkeit.

voice-bubble.html
<div class="voice-bubble" id="voiceBubble">
  <button class="voice-play-btn" id="voicePlay" aria-label="Reproducir nota de voz">▶</button>
  <div class="waveform-container" id="waveformTrack" role="slider" aria-label="Barra de avance de audio">
    <!-- Barras de amplitud generadas o estáticas -->
    <div class="wf-bar" style="height: 40%;"></div>
    <div class="wf-bar" style="height: 70%;"></div>
    <div class="wf-bar" style="height: 100%;"></div>
    <div class="wf-bar" style="height: 60%;"></div>
    <div class="wf-bar" style="height: 85%;"></div>
    <div class="wf-bar" style="height: 35%;"></div>
    <div class="wf-bar" style="height: 50%;"></div>
    <div class="wf-bar" style="height: 90%;"></div>
  </div>
  <span class="voice-timer" id="voiceTimer">0:14</span>
  <button class="speed-toggle-btn" id="speedToggle" aria-label="Velocidad de reproducción">1x</button>
  <audio id="voiceAudio" src="media/memo.mp3" preload="metadata"></audio>
</div>

Schritt 2: Stile von Barras von Form von Onda mit Coloreo Dinámico

Nutzt eine Klasse .is-passed oder variables CSS für colorear die barras anteriores al cursor von reproducción in ein tono primario brillante.

voice-waveform.css
.voice-bubble {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  background: #1e1b4b;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 24px;
  padding: 8px 16px;
  max-width: 380px;
}

.waveform-container {
  display: flex;
  align-items: center;
  gap: 3px;
  height: 28px;
  cursor: pointer;
  flex: 1;
}

.wf-bar {
  width: 3px;
  background: rgba(255, 255, 255, 0.25);
  border-radius: 2px;
  transition: background-color 0.1s ease;
}

.wf-bar.is-passed {
  background: #6366f1;
}

.speed-toggle-btn {
  background: rgba(255, 255, 255, 0.1);
  border: none;
  border-radius: 12px;
  color: #a5b4fc;
  font-size: 11px;
  font-weight: 700;
  padding: 3px 7px;
  cursor: pointer;
}

Schritt 3: Sincronización von Barras und Salto al Punto Rascado

Berechnet qué barra debe iluminarse in cada evento timeupdate und ermöglicht saltar al instante exacto berechnet die posición vom clic relativo al contenedor.

waveform-engine.js
const audio = document.getElementById('voiceAudio');
const playBtn = document.getElementById('voicePlay');
const track = document.getElementById('waveformTrack');
const bars = track.querySelectorAll('.wf-bar');
const timer = document.getElementById('voiceTimer');

playBtn.addEventListener('click', () => {
  if (audio.paused) {
    audio.play();
    playBtn.textContent = '⏸';
  } else {
    audio.pause();
    playBtn.textContent = '▶';
  }
});

audio.addEventListener('timeupdate', () => {
  if (!audio.duration) return;
  const progress = audio.currentTime / audio.duration;
  const passedIndex = Math.floor(progress * bars.length);

  bars.forEach((bar, i) => {
    bar.classList.toggle('is-passed', i <= passedIndex);
  });

  const remaining = Math.max(0, audio.duration - audio.currentTime);
  const m = Math.floor(remaining / 60);
  const s = Math.floor(remaining % 60);
  timer.textContent = `${m}:${String(s).padStart(2, '0')}`;
});

// Salto al hacer clic en cualquier punto de la forma de onda
track.addEventListener('click', (e) => {
  const rect = track.getBoundingClientRect();
  const ratio = (e.clientX - rect.left) / rect.width;
  if (audio.duration) {
    audio.currentTime = ratio * audio.duration;
  }
});

Schritt 4: Selector Cíclico von Geschwindigkeit von Audio mit Audio.playbackRate

Alterna suavemente die Geschwindigkeit von reproducción zwischen 1x, 1.5x und 2x ohne distorsionar der tono vocal vom hablante.

speed-controller.js
const speeds = [1, 1.5, 2];
let speedIndex = 0;
const speedBtn = document.getElementById('speedToggle');

speedBtn.addEventListener('click', () => {
  speedIndex = (speedIndex + 1) % speeds.length;
  const rate = speeds[speedIndex];
  audio.playbackRate = rate;
  speedBtn.textContent = `${rate}x`;
});

Erweiterte Best Practices & Vertiefung

Erweitere diese Implementierung durch die Kombination mit ergänzenden Werkzeugen und Design-Tokens:

1. Rastreador von Form von Onda von Amplitud SVG

Im Burbuja von Notas von Voz mit Form von Onda öffnen →

Visualización fiel vom Muster vocal vom archivo sonoro mit barras von Höhe precalculada.

1.5x SPEED

2. Multiplicadores von Geschwindigkeit 1x, 1.5x und 2x

Im Burbuja von Notas von Voz mit Form von Onda öffnen →

Aceleración sonora instantánea mediante die Eigenschaft native Audio.playbackRate conservando der tono vocal.

01:24 / 03:45

3. Insignias von Duración und Conteo Regresivo

Im Burbuja von Notas von Voz mit Form von Onda öffnen →

Muestra die duración total oder der tiempo restante in formato MM:SS mit alineación numérica monospace.

Chaam
Chaams Profi-Tipp

Eine wichtige Lektion aus Web Audio Visualizern: Prüfe immer audioCtx.state === 'suspended' und reaktiviere ihn innerhalb eines direkten Klick-Events. Mobile Browser blockieren unaufgeforderte Audio-Wiedergabe strikt.

Listo für añadir notas von voz interaktive a tu Anwendung?

Generiert reproductores von notas von voz Stil chat mit Audio-Wellenformen clicables, duraciones dinámicas und multiplicadores von Geschwindigkeit.

Im Burbuja von Notas von Voz mit Form von Onda öffnen ↗

Häufig gestellte Fragen (FAQ)

Praxisnahe Antworten zu Implementierung, Browser-Besonderheiten und Performance-Tipps.

Cambiar playbackRate altera der tono von die voz haciéndola aguda?

No. Die Browser moderne activan durch defecto die Eigenschaft preservesPitch = true in der Element von Audio, das que conserva der timbre natural von die voz ohne der Effekt von "voz von ardilla".

Wie extraer die datos von amplitud für erstellen die barras von onda von ein Audio?

Se decodifica der Audio mediante audioCtx.decodeAudioData(arrayBuffer), se divide der canal PCM in N bloques und se berechnet die media cuadrática (RMS) oder pico maximal von cada segmento für normalizar alturas zwischen 0 und 100%.

Durch qué die burbuja debe mostrar der tiempo restante in lugar vom transcurrido?

Al igual que in WhatsApp und Telegram, mostrar der tiempo restante indica al oyente exactamente cuánto tiempo le falta für terminar von escuchar die nota antes von decidir reproducirla.

Wie hacer que die pista von ondas sea barrierefrei für lectores von Bildschirm?

Der contenedor debe tener role="slider", aria-valuemin="0", aria-valuemax="100" und actualizar aria-valuenow dinámicamente mit der porcentaje von reproducción.

Qué ocurre al terminar die reproducción?

Al dispararse der evento ended, se reinicia Audio.currentTime = 0, se restablecen todas die barras und der icono vom Button regresa al estado von reproducción ▶.

← Zurück zu allen Blog-Artikeln Generator-Studio starten →