-
Notifications
You must be signed in to change notification settings - Fork 9
/
RemoteAudioTrack.tsx
78 lines (67 loc) · 2.11 KB
/
RemoteAudioTrack.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import type { IRemoteAudioTrack } from "agora-rtc-sdk-ng";
import type { ReactNode } from "react";
import { useEffect } from "react";
import type { Nullable } from "../misc/utils";
import { useAutoPlayAudioTrack } from "./TrackBoundary";
export interface RemoteAudioTrackProps {
/**
* The remote audio track to be played.
*/
readonly track?: Nullable<IRemoteAudioTrack>;
/**
* `true`: Play the track.`false`: Stop playing the track.
*/
readonly play?: boolean;
/**
* The ID of the playback device, such as a speaker. The device ID can be obtained using [`IAgoraRTC.getPlaybackDevices`](https://api-ref.agora.io/en/video-sdk/web/4.x/interfaces/iagorartc.html#getplaybackdevices). This property is only supported in the desktop version of Chrome browser. Modifying the value of this property in other browsers throws a `NOT_SUPPORTED` error.
*/
readonly playbackDeviceId?: string;
/**
* The volume. The value ranges from 0 (mute) to 100 (the original volume).
*/
readonly volume?: number;
/**
* The React nodes to be rendered.
*/
readonly children?: ReactNode;
}
/**
* This component plays the audio track of a remote user with the playback device you specify.
* @example
* ```jsx
* import { RemoteAudioTrack, useJoin, useRemoteAudioTracks, useRemoteUsers } from "agora-rtc-react";
*
* function App() {
* const remoteUsers = useRemoteUsers();
* const audioTracks = useRemoteAudioTracks(remoteUsers);
*
* return (
* <>
* {audioTracks.map(track => (
* <RemoteAudioTrack key={track.getUserId()} play track={track} />
* ))}
* </>
* );
* }
* ```
*/
export function RemoteAudioTrack({
track,
play = false,
playbackDeviceId,
volume,
children,
}: RemoteAudioTrackProps) {
useAutoPlayAudioTrack(track, play);
useEffect(() => {
if (track && playbackDeviceId != null) {
track.setPlaybackDevice(playbackDeviceId).catch(console.warn);
}
}, [track, playbackDeviceId]);
useEffect(() => {
if (track && volume != null) {
track.setVolume(volume);
}
}, [track, volume]);
return children ? <>{children}</> : null;
}