Skip to content

Commit d8805b5

Browse files
authored
refactor: optimize audio chat ui (#19)
1 parent 214b953 commit d8805b5

File tree

5 files changed

+23
-11
lines changed

5 files changed

+23
-11
lines changed

assets/controllers/audio_controller.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export default class extends Controller {
5959

6060
this.mediaRecorder.onstop = async () => {
6161
const audioBlob = new Blob(audioChunks, {type: 'audio/wav'});
62+
this.mediaRecorder.stream.getAudioTracks().forEach(track => track.stop());
6263

6364
const base64String = await this.blobToBase64(audioBlob);
6465
this.component.action('submit', { audio: base64String });
@@ -79,11 +80,4 @@ export default class extends Controller {
7980
reader.onloadend = () => resolve(reader.result.split(',')[1]);
8081
});
8182
}
82-
83-
playBase64Audio(base64String) {
84-
const audioSrc = "data:audio/wav;base64," + base64String;
85-
const audio = new Audio(audioSrc);
86-
87-
audio.play().catch(error => console.error("Playback error:", error));
88-
}
8983
}

assets/styles/audio.css

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
.user-message {
1313
background: #df662f;
1414
color: #ffffff;
15+
16+
#loading-message & {
17+
color: rgba(255, 255, 255, 0.7);
18+
}
1519
}
1620

1721
.bot-message {
@@ -58,4 +62,12 @@
5862
}
5963
}
6064
}
65+
66+
footer {
67+
color: #ffffff;
68+
69+
a {
70+
color: #ffffff;
71+
}
72+
}
6173
}

templates/_message.html.twig

+8-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,17 @@
3333
</div>
3434
{% endmacro %}
3535

36-
{% macro user(content) %}
36+
{% macro user(content, loading = false) %}
3737
<div class="d-flex align-items-baseline text-end justify-content-end mb-4">
3838
<div class="pe-2">
3939
{% for item in content %}
40-
<div class="user-message d-inline-block p-2 px-3 m-1 border border-light-subtle shadow-sm">{{ item.text }}</div>
40+
<div class="user-message d-inline-block p-2 px-3 m-1 border border-light-subtle shadow-sm">
41+
{% if loading %}
42+
<span class="spinner-border spinner-border-sm me-1"></span><i>{{ item.text }}</i>
43+
{% else %}
44+
{{ item.text }}
45+
{% endif %}
46+
</div>
4147
{% endfor %}
4248
</div>
4349
<div class="user avatar rounded-3 shadow-sm">

templates/components/audio.html.twig

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</div>
1818
{% endfor %}
1919
<div id="loading-message" class="d-none">
20-
{{ message.user([{text:''}]) }}
20+
{{ message.user([{text:'Converting your speech to text ...'}], true) }}
2121
{{ message.bot('The Bot is looking for an answer ...', true) }}
2222
</div>
2323
</div>

templates/index.html.twig

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
{% if 'dev' == app.environment %}
8585
<div class="card-footer">
8686
{{ ux_icon('solar:code-linear', { height: '20px', width: '20px' }) }}
87-
<a href="{{ path('_profiler_open_file', { file: 'src/Audio/Chat.php', line: 14 }) }}">See Implementation</a>
87+
<a href="{{ path('_profiler_open_file', { file: 'src/Audio/Chat.php', line: 18 }) }}">See Implementation</a>
8888
</div>
8989
{% endif %}
9090
</div>

0 commit comments

Comments
 (0)