diff --git a/examples/client_browser.html b/examples/client_browser.html
index 49f41c6..ae66a11 100644
--- a/examples/client_browser.html
+++ b/examples/client_browser.html
@@ -76,17 +76,17 @@
const nextSeq = () => ++seq;
- const ensureAudioContext = () => {
+ const ensureAudioContext = async () => {
if (!audioCtx) {
audioCtx = new (window.AudioContext || window.webkitAudioContext)({ sampleRate: 24000 });
}
if (audioCtx.state === 'suspended') {
- audioCtx.resume();
+ await audioCtx.resume();
}
};
- const playPcm16 = (base64Data) => {
- ensureAudioContext();
+ const playPcm16 = async (base64Data) => {
+ await ensureAudioContext();
const raw = atob(base64Data);
const samples = new Int16Array(raw.length / 2);
const view = new DataView(samples.buffer);
@@ -120,7 +120,8 @@
log(`Connecting to ${uri} ...`);
try {
ws = new WebSocket(uri);
- ws.onopen = () => {
+ ws.onopen = async () => {
+ await ensureAudioContext();
log('Connected');
$('connect').disabled = true;
$('speak').disabled = false;
@@ -144,8 +145,14 @@
ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
if (msg.type === 'audio') {
- playPcm16(msg.data);
- log(`audio seq=${msg.seq} len=${(msg.data.length * 3 / 4 / 2 / 24000).toFixed(2)}s`);
+ (async () => {
+ try {
+ await playPcm16(msg.data);
+ log(`audio seq=${msg.seq} len=${(msg.data.length * 3 / 4 / 2 / 24000).toFixed(2)}s`);
+ } catch (err) {
+ log(`audio playback error: ${err.message}`);
+ }
+ })();
} else if (msg.type === 'status') {
log(`status ${msg.event} seq=${msg.seq}`);
} else if (msg.type === 'error') {
@@ -172,7 +179,7 @@
log('Not connected');
return;
}
- ensureAudioContext();
+ await ensureAudioContext();
nextStartTime = 0;
const text = $('text').value.trim();