andrymamboro commited on
Commit
aaa0b08
·
verified ·
1 Parent(s): 16b3ef8

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +39 -16
templates/index.html CHANGED
@@ -1,33 +1,56 @@
1
  <!DOCTYPE html>
2
- <html>
3
  <head>
4
  <script src="https://cdn.jsdelivr.net/npm/livekit-client/dist/livekit-client.umd.min.js"></script>
 
 
 
 
 
 
5
  </head>
6
  <body>
7
- <div style="text-align: center; margin-top: 50px; font-family: sans-serif;">
8
  <h1>Voice Agent</h1>
9
- <button id="btn">Hubungkan</button>
10
  <p id="status">Status: Terputus</p>
11
  </div>
12
 
13
  <script>
14
- const btn = document.getElementById('btn');
15
  const status = document.getElementById('status');
16
  let room;
17
 
18
  btn.onclick = async () => {
19
- try {
20
- status.innerText = "Menghubungkan...";
21
- const res = await fetch('/token');
22
- const data = await res.json();
23
-
24
- room = new LivekitClient.Room();
25
- await room.connect(data.server_url, data.token);
26
-
27
- status.innerText = "Status: Terhubung";
28
- btn.disabled = true;
29
- } catch (e) {
30
- status.innerText = "Error: " + e.message;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  }
32
  };
33
  </script>
 
1
  <!DOCTYPE html>
2
+ <html lang="id">
3
  <head>
4
  <script src="https://cdn.jsdelivr.net/npm/livekit-client/dist/livekit-client.umd.min.js"></script>
5
+ <style>
6
+ body { font-family: sans-serif; display: flex; justify-content: center; padding-top: 50px; background: #f4f4f9; }
7
+ .card { background: white; padding: 30px; border-radius: 20px; box-shadow: 0 10px 25px rgba(0,0,0,0.1); width: 300px; text-align: center; }
8
+ button { padding: 12px 25px; border: none; border-radius: 50px; cursor: pointer; font-weight: bold; margin-top: 15px; }
9
+ #toggle-btn { background: #007bff; color: white; }
10
+ </style>
11
  </head>
12
  <body>
13
+ <div class="card">
14
  <h1>Voice Agent</h1>
15
+ <button id="toggle-btn">Hubungkan</button>
16
  <p id="status">Status: Terputus</p>
17
  </div>
18
 
19
  <script>
20
+ const btn = document.getElementById('toggle-btn');
21
  const status = document.getElementById('status');
22
  let room;
23
 
24
  btn.onclick = async () => {
25
+ if (room && room.state === 'connected') {
26
+ room.disconnect();
27
+ room = null;
28
+ btn.innerText = "Hubungkan";
29
+ status.innerText = "Status: Terputus";
30
+ } else {
31
+ try {
32
+ status.innerText = "Menghubungkan...";
33
+ const res = await fetch('/token');
34
+ const data = await res.json();
35
+
36
+ room = new LivekitClient.Room();
37
+
38
+ // --- BAGIAN PENTING UNTUK AUDIO ---
39
+ room.on(LivekitClient.RoomEvent.TrackSubscribed, (track) => {
40
+ if (track.kind === 'audio') {
41
+ track.attach(); // Memaksa audio dimainkan di speaker
42
+ }
43
+ });
44
+ // ----------------------------------
45
+
46
+ await room.connect(data.server_url, data.token);
47
+ await room.localParticipant.setMicrophoneEnabled(true);
48
+
49
+ btn.innerText = "Berhenti";
50
+ status.innerText = "Status: Terhubung";
51
+ } catch (e) {
52
+ status.innerText = "Error: " + e.message;
53
+ }
54
  }
55
  };
56
  </script>