-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathvoice-assistant.tsx
83 lines (75 loc) · 2.27 KB
/
voice-assistant.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
79
80
81
82
83
'use client';
import {
LiveKitRoom,
useToken,
useVoiceAssistant,
BarVisualizer,
RoomAudioRenderer,
VoiceAssistantControlBar,
} from '@livekit/components-react';
import type { NextPage } from 'next';
import { useMemo, useState } from 'react';
import { MediaDeviceFailure } from 'livekit-client';
import styles from '../styles/VoiceAssistant.module.scss';
import { generateRandomUserId } from '../lib/helper';
function SimpleVoiceAssistant() {
const { state, audioTrack } = useVoiceAssistant();
return (
<BarVisualizer
state={state}
barCount={7}
trackRef={audioTrack}
style={{ width: '75vw', height: '300px' }}
/>
);
}
const VoiceAssistantExample: NextPage = () => {
const params = typeof window !== 'undefined' ? new URLSearchParams(location.search) : null;
const roomName = useMemo(
() => params?.get('room') ?? 'test-room-' + Math.random().toFixed(5),
[],
);
const [shouldConnect, setShouldConnect] = useState(false);
const tokenOptions = useMemo(() => {
const userId = params?.get('user') ?? generateRandomUserId();
return {
userInfo: {
identity: userId,
name: userId,
},
};
}, []);
const token = useToken(process.env.NEXT_PUBLIC_LK_TOKEN_ENDPOINT, roomName, tokenOptions);
const onDeviceFailure = (e?: MediaDeviceFailure) => {
console.error(e);
alert(
'Error acquiring camera or microphone permissions. Please make sure you grant the necessary permissions in your browser and reload the tab',
);
};
return (
<main data-lk-theme="default" className={styles.main}>
<LiveKitRoom
audio={true}
token={token}
connect={shouldConnect}
serverUrl={process.env.NEXT_PUBLIC_LK_SERVER_URL}
onMediaDeviceFailure={onDeviceFailure}
onDisconnected={() => setShouldConnect(false)}
className={styles.room}
>
<div className={styles.inner}>
{shouldConnect ? (
<SimpleVoiceAssistant />
) : (
<button className="lk-button" onClick={() => setShouldConnect(true)}>
Connect
</button>
)}
</div>
<VoiceAssistantControlBar />
<RoomAudioRenderer />
</LiveKitRoom>
</main>
);
};
export default VoiceAssistantExample;