Voice
@irtio/client 5.0BrowsersThe Voice interface is a room connection’s voice: who is on the voice server and speaking, and the player’s own microphone and settings. joinVoice() from @irtio/client/voice returns one.
Instance properties
All are read only.
Voice.participants- The other players on the voice server, whether or not this player hears them.
Voice.speaking- The players speaking now, this one included.
Voice.mutedPlayers- The other players on the voice server sending no audio.
Voice.muted,Voice.micOn,Voice.pushToTalkandVoice.level- This player’s microphone: muted, on, push-to-talk and its input level from 0 to 1.
Voice.blocked,Voice.input,Voice.outputandVoice.canSetOutput- The players this player blocked, the chosen microphone and speaker and whether the browser can choose the speaker.
Voice.projectandVoice.livekit- The project URL the settings are kept by, and the voice server’s connection.
Instance methods
Voice.enableMic(),Voice.setMuted(),Voice.setPushToTalk()andVoice.talk()- Turn the microphone on, mute it and send it only while a key is held.
Voice.volume(),Voice.setVolume(),Voice.setBlocked(),Voice.devices(),Voice.setInput()andVoice.setOutput()- This player’s volume for each other player, local blocks and the choice of microphone and speaker.
Voice.disconnect()- Leaves the voice server for good.
Events
Listen to these events with addEventListener().
participantsevent- A player joined or left the voice server.
speakingandmutedPlayersspeakingormutedPlayerschanged.micError- The microphone failed to start and was turned off.
Description
The voice follows the room connection through reconnects and ends when it closes for good. Its three sets empty while voice is down and fill again after it rejoins, so listeners added once keep working.
Event listeners
Voice is an EventTarget. participants, speaking and mutedPlayers each fire a CustomEvent when the set of the same name changes, with the new set, a ReadonlySet<string> of player IDs, in its detail. micError fires a CustomEvent with the Error in its detail.
Microphone
enableMic() publishes the microphone, asking the browser for permission the first time; call it from a click. Called before voice has joined, it turns the microphone on when it does, and while the room mutes this player it publishes once the mute ends. A refusal or other failure turns the microphone off again, rejects and fires micError, as does a failure on a later join. An error named NotAllowedError is a refusal.
setMuted(muted) mutes or unmutes it, and muted is that mute. micOn is true once enableMic() was called and setMuted() hasn’t muted it. setPushToTalk(true) keeps the microphone muted except between talk(true) and talk(false), which the page calls from its own key handlers, and pushToTalk says whether it’s on. level is the microphone’s input level from 0 to 1, 0 while it isn’t sending; read it on animation frames for a meter.
Player settings
setVolume(player, volume, keep) sets how loud this player hears another, from 0 to 1, multiplied into the distance attenuation; volume(player) reads it, 1 by default. With keep set to false, the value isn’t saved, for a slider being dragged. setBlocked(player, blocked) is a local mute: the client never subscribes to a blocked player, whatever the room picks, and blocked is the set of blocked players. Volumes and blocks are kept in localStorage per project, or for the page where storage is blocked.
devices() resolves to the microphones and speakers the browser offers, as { inputs, outputs } of { id, label }; labels are empty until the page has microphone permission. setInput(id) and setOutput(id) choose one, and input and output are the chosen IDs, undefined for the browser’s default. Only a browser with setSinkId can choose the speaker, which canSetOutput says; elsewhere devices() lists no speakers and setOutput() rejects.
Connection
project is the room connection’s project URL, by which settings are kept. livekit is the voice server’s LiveKit room while voice is joined, for features Voice doesn’t cover; it’s replaced on each reconnect, so listeners on it need adding again. disconnect() leaves the voice server for good.
Examples
Muting with a key
A team shooter turns the microphone on from a button and mutes it with the M key.
// team-voice.ts
import { connect } from "@irtio/client";
import { joinVoice } from "@irtio/client/voice";
const room = await connect("squad");
const voice = joinVoice(room);
voice.addEventListener("micError", ({ detail: e }) => console.warn(e.name === "NotAllowedError" ? "microphone refused" : e.message));
document.querySelector("#mic")?.addEventListener("click", () => void voice.enableMic().catch(() => {}));
addEventListener("keydown", (e) => {
if (e.code === "KeyM") void voice.setMuted(!voice.muted);
});
micError reports the refusal, so the click handler’s rejection is caught and dropped.
See also
joinVoice()RoomClient.peers- Proximity voice
- UI kit, for ready-made voice controls
Runtime availability
| Browsers | |
|---|---|
Voice | 5.0 |