useLocalAudio
The useLocalAudio hook exposes primitives to interact with your own audio stream - coming from your microphone device.
Name | Description | Return Type | Params |
---|---|---|---|
stream | Your microphone audio stream. Null if not enabled yet. | MediaStream | null | |
track | Your microphone audio stream track. Null if not enabled yet. | MediaStreamTrack | null | |
isAudioOn | Your microphone audio stream is on or off yet. | boolean | |
enableAudio | Enable your microphone audio stream and start producing it with other peers in the room. | Promise<void> | |
disableAudio | Disable your microphone audio stream and stop producing it with other peers in the room. | Promise<void> | |
replaceAudioStream | Replaces the mediastream. | Promise<void> | |
changeAudioSource | Changes 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
Description | Return 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 Name | Type | Description | Required |
---|---|---|---|
producer | Producer | The 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
Description | Return 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
Description | Return 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
}});