useLocalAudio

useLocalAudio

The useLocalAudio hook exposes primitives to interact with your own audio stream - coming from your microphone device.

NameDescriptionReturn TypeParams
streamYour microphone audio stream. Null if not enabled yet.MediaStream | null
trackYour microphone audio stream track. Null if not enabled yet.MediaStreamTrack | null
isAudioOnYour microphone audio stream is on or off yet.boolean
enableAudioEnable your microphone audio stream and start producing it with other peers in the room.Promise<void>
disableAudioDisable your microphone audio stream and stop producing it with other peers in the room.Promise<void>
replaceAudioStreamReplaces the mediastream.Promise<void>
changeAudioSourceChanges the audio sources of the devices.Promise<void>deviceId: string

Example Usage

import { useLocalAudio } from "@huddle01/react/hooks";
 
const {
    stream,
    track,
    isAudioOn,
    enableAudio,
    disableAudio,
    replaceAudioStream,
    changeAudioSource,
  } = useLocalAudio({
    onProduceStart(producer) {},
    onProduceClose(label:string) {},
    onProduceError() {},
  });
 
// enable and disable audio
const handleAudio = async () => {
  if (isAudioOn) {
    await disableAudio();
  } else {
    await enableAudio();
  }
};

Props

The useLocalAudio hook accepts an object with the following fields as props.

onProduceStart

OptionalAdvanced
DescriptionReturn Type
This function will be called when you start producing your microphone's audio stream i.e sharing it with other peers in the room.void
Parameter NameTypeDescriptionRequired
producerProducerThe mediasoup producer object.Yes

Example Usage

const localAudio = useLocalAudio({ onProduceStart: (producer) => {
	console.log("Started producing your audio stream!");
	console.log(producer);
    // your code here
}});

onProduceClose

OptionalAdvanced
DescriptionReturn Type
This function will be called when you stop producing your microphone's audio stream.void

Example Usage

const localAudio = useLocalAudio({ onProduceClose: () => {
	console.log("Stopped producing your audio stream!");
    // your code here
}});

onProduceError

OptionalAdvanced
DescriptionReturn Type
This function will be called when there was an error encountered while producing your microphone's audio stream.void

Example Usage

const localAudio = useLocalAudio({ onProduceError: () => {
	console.log("There was an error in producing your audio stream!");
    // your code here
}});
Audio/Video Infrastructure designed for developers to empower them to ship simple yet powerful Audio/Video Apps.
support
company
Copyright © 2024 Graphene 01, Inc. All Rights Reserved.