Broadcast and Play Audio-Only Streams
Publish from a microphone with no camera, and play a stream that has no video track.
To build a voice room, a call-in show, or any other audio-first product, you publish a broadcast that carries a microphone track and no video track, and you give viewers something other than a video frame to look at. This page covers both halves in the Native Frame browser SDK: how to capture and broadcast from the microphone alone, and what the player does and reports when the stream it plays has no video.
Prerequisites
- A working camera broadcast, set up as in Set Up a Livestream Video. This page reuses its call and authentication setup and changes only how the media is captured.
- A player for viewers, set up as in View a Stream.
How a broadcast decides what to publish
A broadcast publishes whatever tracks its controller holds. call.broadcast(ctrl, options) watches the controller's combined source stream and, each time its tracks change, creates, replaces, or closes one producer per kind. A live audio track gets an audio producer. A live video track gets a video producer. If a kind has no track, it has no producer.
So an audio-only broadcast is not a mode you switch on. It is a broadcast whose controller never captured a camera. There is no audioOnly field on BroadcastOptions, and you do not need one.
Broadcast from the microphone only
Two things keep the camera off: telling mediaController.init() not to ask for camera permission, and never starting the camera on the controller.
init() asks for camera and microphone permission by default. Pass requestVideoPermission: false and it asks for the microphone only, so the browser shows no camera prompt.
The controller that requestController() returns starts each device when you give it one. Setting audioDeviceId starts the microphone. Setting videoDeviceId, or setting videoDisabled = false, starts the camera. For an audio-only broadcast, set audioDeviceId and never touch videoDeviceId.
defaultConstraints: { video: false } does not keep the camera off. The controller turns false into an empty constraint set, which asks the browser for any camera. The camera opens as soon as anything starts video on that controller. Never start video instead.
For the same reason, do not use the React usePreviewPlayer hook for an audio-only broadcast. By default it sets both videoDeviceId and audioDeviceId, which opens the camera. Supplying mscOptions.defaultConstraints, whose type requires both audio and video, stops it setting either, so the hook starts no device at all. Build the controller yourself, as below.
import { useEffect, useMemo, useState } from "react";
import { context, hooks, mediaController, types } from "@video/video-client-react";
const { MediaStreamControllerAPIProvider } = context;
const { useBroadcast, useEvent } = hooks;
/**
* Captures the microphone only and broadcasts it on an existing call.
* Create the call as in "Set Up a Livestream Video" and pass it in.
*/
export function AudioOnlyBroadcaster({ call }: { call: types.CallAPI | null }) {
const [msc, setMsc] = useState<types.MediaStreamControllerAPI | null>(null);
useEffect(() => {
let ctrl: types.MediaStreamControllerAPI | null = null;
let cancelled = false;
(async () => {
// Ask for microphone permission only. No camera prompt is shown.
await mediaController.init({ requestAudioPermission: true, requestVideoPermission: false });
ctrl = await mediaController.requestController();
// Setting audioDeviceId starts the microphone. "" means the default microphone.
// videoDeviceId is never set, so the camera never starts.
ctrl.audioDeviceId = mediaController.audioDevices()[0]?.deviceId ?? "";
if (cancelled) {
ctrl.dispose("unmounted before ready");
return;
}
setMsc(ctrl);
})();
return () => {
cancelled = true;
ctrl?.dispose("audio-only broadcaster unmounted");
};
}, []);
// Memoise the options: useBroadcast pins them at first render.
const broadcastOptions = useMemo<types.BroadcastOptions>(() => ({ streamName: "default" }), []);
const { status, error } = useBroadcast(call, msc, broadcastOptions);
if (msc == null) {
return <p>Starting microphone…</p>;
}
return (
<MediaStreamControllerAPIProvider mediaStreamControllerAPI={msc}>
<MicMuteButton />
<p>Broadcast: {status}</p>
{error != null && <p role="alert">{String(error)}</p>}
</MediaStreamControllerAPIProvider>
);
}
/** Mutes the microphone without stopping it, so the audio producer stays up. */
function MicMuteButton() {
const msc = context.useMediaStreamControllerAPI();
useEvent(msc, "audioMuted");
return (
<button type="button" onClick={() => { msc.audioMuted = !msc.audioMuted; }}>
{msc.audioMuted ? "Unmute microphone" : "Mute microphone"}
</button>
);
}
useBroadcast stays idle until both the call and the controller are non-null, then starts the broadcast. There is no preview player here: an audio-only broadcaster has no picture to preview, and playing your own microphone back to yourself causes feedback.
import { mediaController } from '@video/video-client-core';
/**
* Captures the microphone only.
* Returns a controller that call.broadcast() accepts.
*/
export async function requestMicrophoneOnly() {
// Ask for microphone permission only. No camera prompt is shown.
await mediaController.init({ requestAudioPermission: true, requestVideoPermission: false });
const mediaStreamController = await mediaController.requestController();
// Setting audioDeviceId starts the microphone. "" means the default microphone.
// videoDeviceId is never set, so the camera never starts.
const [microphone] = mediaController.audioDevices();
mediaStreamController.audioDeviceId = microphone?.deviceId ?? "";
return mediaStreamController;
}
// With a call created as in "Set Up a Livestream Video":
const mediaStreamController = await requestMicrophoneOnly();
const broadcast = await call.broadcast(mediaStreamController, { streamName: "default" });
If you do not need the device-selection surface of mediaController, you can build the same thing from a microphone controller and a composition, as Broadcast Multiple Sources describes. A composition that contains only a microphone has no video source at all:
import {
createMediaContext,
createMicrophoneController,
createCompositionStreamController,
} from '@video/video-client-core';
const ctx = createMediaContext();
const microphone = createMicrophoneController(ctx);
const composition = createCompositionStreamController(ctx, [microphone]);
// Requests the microphone only.
await microphone.start();
const broadcast = await call.broadcast(composition, { streamName: "default" });
Broadcast options and the tracks you capture
These are the options that mention audio-only or video-only, and what each one does to what is published.
| Option | Where | What it does |
|---|---|---|
videoOnly | BroadcastOptions | When true, the broadcast never opens an audio producer, whatever audio tracks the controller holds. pause() and resume() also leave audio alone. It is the only track-selection switch at the broadcast level. |
audioOnly | BroadcastOptions | Does not exist. Capture no video track instead, as above. |
audioOnly / videoOnly | PeerProducerOptions, passed as audioProducerOptions or videoProducerOptions | Sent to the server as metadata on the producer (appData.audioOnly, appData.videoOnly). They do not choose which tracks the SDK publishes, with one partial exception below. |
The exception: videoProducerOptions: { audioOnly: true } makes the broadcast skip creating a video producer when the controller's tracks change. broadcast.resume() and broadcast.videoHidden = false do not check it, so they can still start video. Do not use it to keep video off; capture no camera instead.
When a flag and the tracks disagree, the tracks win, except for videoOnly. A broadcast with videoOnly: true and only a microphone publishes nothing. A broadcast without it, on a controller with only a microphone, publishes audio.
Mute, pause, and stop the microphone
Three controls affect the microphone. For an audio-only broadcast the difference matters, because the microphone is the whole broadcast.
| Control | What happens |
|---|---|
mediaStreamController.audioMuted = true | Mutes the microphone by gain. The track stays live and the audio producer keeps running, so viewers keep receiving a silent track. |
broadcast.audioMuted = true | Pauses the audio producer. Setting it back to false resumes it. |
mediaStreamController.audioDisabled = true | Stops the microphone. The track ends, so the broadcast closes its audio producer and, with no video either, publishes nothing until the microphone starts again. |
The React ToggleMicButton component toggles audioDisabled, so on an audio-only broadcast it stops the microphone rather than muting it. Use a button over audioMuted, like MicMuteButton above, if you want a mute that leaves the broadcast up.
Detect a failed audio publish
When the broadcast cannot create its audio producer, it emits error with an EnableAudioError, whose code is "enable-audio" (ErrorCode.EnableAudio). The error's inner property holds the underlying failure. The broadcast also keeps the most recent error on broadcast.error, so a listener that attaches late can still read one that fired during call.broadcast().
broadcast.on("error", (err) => {
if (err.code === "enable-audio") {
// The audio producer could not be created. With no video track,
// nothing is being published. err.inner holds the underlying cause.
showPublishFailed(err.message);
}
});
// An error raised while call.broadcast() was still starting is latched here.
if (broadcast.error != null) {
showPublishFailed(broadcast.error.message);
}
In React, useBroadcast subscribes to the same event and reports it as error, with status set to "error".
The SDK has no error code of its own for a stream being refused because it has no video. Any failure to create the audio producer arrives as "enable-audio", so branch on that code and read inner for the detail.
Play an audio-only stream
On the viewer side you create the player and attach it exactly as for a video stream. What changes is what the player reports, and what you show.
What the player reports
player.hasAudio and player.hasVideo say whether the current source is delivering each kind right now. How far you can rely on them depends on the driver, which player.format names.
player.format | hasAudio / hasVideo |
|---|---|
"webrtc" | Reflect the tracks the stream delivers. An audio-only stream reads hasAudio: true, hasVideo: false. A track counts only while it is present and not paused, so hasVideo also reads false while a camera broadcaster has paused video. |
"media-stream", playing a call participant's stream (the stream from a streamAdded event) | Same as WebRTC: computed from the participant's tracks, and a paused or muted track counts as absent. |
"media-stream", playing any other MediaStream | Both always read true. This includes a local preview of your own controller, even if it has no camera. |
Any other driver (hlsjs, native-hls, flvhttp, mp4-http) | Not populated. Both read false whatever the stream carries. Do not use them to detect an audio-only stream on these drivers. |
On WebRTC, an audio-only stream plays normally. The driver treats a stream with no video track as healthy: it does not report an error and does not fail over to another format. player.qualities is an empty list, because the quality ladder comes from the video track.
The hasVideo and hasAudio events fire only when the value changes. A stream that never had video never fires hasVideo, so read the getter as well as listening for the event, for example when playing fires.
On WebRTC the player can also emit videoCodecUnreceivable, a diagnostic for a browser that could not negotiate the stream's video codec and received audio only. It fires when the server offers a video codec this browser cannot receive and the stream arrives with no video track. A browser like that, playing a stream that is audio-only on purpose, looks the same, so on an audio-only product treat the event as a hint, not as proof that video is missing.
Attach a video element
Attach a <video> element, as for any other stream. The player plays the audio through it, and it is the element every guide and component in the SDK attaches to. Cover it with your own panel rather than replacing it with a different element.
import { components, context, hooks } from "@video/video-client-react";
const { Video, QualitySelect } = components;
const { usePlayerAPI } = context;
const { useEvent } = hooks;
/**
* Render inside the PlayerAPIProvider from "View a Stream".
* Shows an audio panel in place of the picture when the stream has no video.
*/
export function StreamStage() {
const player = usePlayerAPI();
// Re-render when the driver, the track set, or the quality list changes.
useEvent(player, "format", "hasAudio", "hasVideo", "playing", "qualities");
const tracksReported = player.format === "webrtc" || player.format === "media-stream";
const audioOnly = tracksReported && player.hasAudio && !player.hasVideo;
return (
<div style={{ position: "relative" }}>
<Video />
{audioOnly && (
<div className="audio-only-panel" style={{ position: "absolute", inset: 0 }}>
<p>Audio only</p>
</div>
)}
{player.qualities.length > 0 && <QualitySelect />}
</div>
);
}
import { requestPlayer } from '@video/video-client-core';
const video = document.getElementById("player-video");
const audioPanel = document.getElementById("audio-only-panel");
const qualitySelect = document.getElementById("quality-select");
const player = await requestPlayer(manifestUrl);
await player.attachTo(video);
function renderStage() {
// hasAudio/hasVideo are only reported by the WebRTC and media-stream drivers.
const tracksReported = player.format === "webrtc" || player.format === "media-stream";
const audioOnly = tracksReported && player.hasAudio && !player.hasVideo;
audioPanel.hidden = !audioOnly;
// An audio-only WebRTC stream has no quality ladder.
qualitySelect.hidden = player.qualities.length === 0;
}
for (const event of ["format", "hasAudio", "hasVideo", "playing", "qualities"]) {
player.on(event, renderStage);
}
renderStage();
What to show instead of a video frame
With no video track there is no picture to render, so decide what the viewer sees:
- Show a panel in place of the picture: the broadcaster's name or avatar, and a clear "audio only" label, so a still frame does not read as a frozen stream.
- Hide the quality selector when
player.qualitiesis empty. On an audio-only WebRTC stream there is nothing to choose between. - Keep a visible unmute control. Browsers commonly refuse unmuted autoplay, and in an audio product the forced mute is the whole experience. Forced mute explains the
"forced"state and whyunmute()belongs in a click handler. - Do not tell the viewer the stream is audio-only from
hasVideoalone. On WebRTC it also readsfalsewhile a camera broadcaster has paused video, and on other drivers it is not reported at all. If your product knows a stream is audio-only, carry that in your own stream metadata and usehasVideoonly to decide what to show right now.
Limits
defaultConstraints.video: falsedoes not stop camera capture. Never start the camera instead.hasAudioandhasVideoare reported only on the WebRTC driver and when playing a call participant. Elsewhere they readfalse, ortruefor a plainMediaStream.- On WebRTC,
hasVideo: falsedoes not distinguish an audio-only stream from a camera stream whose video is paused. videoCodecUnreceivablecan fire on a stream that is audio-only on purpose.- There is no dedicated error code for an audio-only publish being refused. It arrives as
"enable-audio". PeerProducerOptions.audioOnlydoes not reliably keep video off; only capturing no video track does.