Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions vapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,22 @@ async function buildAudioPlayer(
return player;
}

function teardownAudioPlayer(player: HTMLAudioElement) {
player.srcObject = null;
player.remove();
}

function destroyAudioPlayer(participantId: string) {
const player = document.querySelector(
`audio[data-participant-id="${participantId}"]`,
);
player?.remove();
const player = document.querySelector<HTMLAudioElement>(`audio[data-participant-id="${participantId}"]`);
if (player) {
teardownAudioPlayer(player)
};
}

function destroyAllAudioPlayers() {
document
.querySelectorAll<HTMLAudioElement>('audio[data-participant-id]')
.forEach(teardownAudioPlayer);
}

function subscribeToTracks(
Expand Down Expand Up @@ -346,6 +357,7 @@ export default class Vapi extends VapiEventEmitter {
await this.call.destroy();
this.call = null;
}
destroyAllAudioPlayers();
this.speakingTimeout = null;
}

Expand Down Expand Up @@ -973,6 +985,7 @@ export default class Vapi extends VapiEventEmitter {
await this.call.destroy();
this.call = null;
}
destroyAllAudioPlayers();
this.speakingTimeout = null;
}

Expand Down