useRemoteAudio

useRemoteAudio

The useRemoteAudio hook exposes primitives to interact with audio streams coming from other peers in the room.

NameDescriptionReturn Type
streamRemote peer's audio stream. Null if not enabled yet.MediaStream | null
trackRemote peer's audio stream track. Null if not enabled yet.MediaStreamTrack | null
stateState of the remote peer's audio stream."playable" | "unavailable" | "stopped" | "paused" | "available"
isAudioOnState of the remote peer's audio stream.boolean

Example Usage

import { useRemoteAudio } from "@huddle01/react/hooks";
import { useEffect, useRef } from 'react';
 
const {
    state,
    track,
    stream,
    isAudioOn
  } = useRemoteAudio({
    peerId: "remote-peer-id",
     onPlayable(data:{
      stream:Mediastream,
      track:Mediatrack,
      label:"audio"
    }) {},
    onClose(reason:{
    code: number;
    tag: string;
    message: string;
    }) {},
  });
 
const audioRef = useRef<HTMLAudioElement>(null);
 
useEffect(() => {
  if (audio.current && stream) {
    audio.current.srcObject = stream;
  }
}, [stream]);
 
return (
  <audio ref={audioRef} autoplay>
)

Props

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

peerId

Required
DescriptionType
The peerId of the peer whose audio stream you want to consume.string

Example Usage

const remoteAudio = useRemoteAudio({ peerId: "remote-peer-id" });

onPlayable

OptionalAdvanced
DescriptionReturn Type
This function will be called when the other peer has enabled their audio stream and it can now be played on your end.void
Parameter NameTypeDescriptionRequired
data{track: MediaStreamTrack; stream: MediaStream; label: 'audio';}The data object containing the audio stream and audio stream track that can be played.Yes

Example Usage

const remoteAudio = useRemoteAudio({ peerId: "remote-peer-id", onPlayable: (data) => {
	console.log("Ready to play remote peer's audio stream!");
    // your code here
}});

onClose

OptionalAdvanced
DescriptionReturn Type
This function will be called when the other peer disabled their microphone's audio stream.void

Example Usage

const remoteAudio = useRemoteAudio({ peerId: "remote-peer-id", onClose: (reason) => {
	console.log("Remote audio stream has been closed!");
    // 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.