给 realtime 语音对话增加音效

This commit is contained in:
RockYang 2024-10-18 06:26:05 +08:00
parent d5eeeea764
commit 662d7b099e
4 changed files with 49 additions and 11 deletions

Binary file not shown.

Binary file not shown.

View File

@ -9,6 +9,14 @@
<div class="phone"></div> <div class="phone"></div>
</div> </div>
<div class="status-text">{{ connectingText }}</div> <div class="status-text">{{ connectingText }}</div>
<audio ref="backgroundAudio" loop>
<source src="/medias/calling.mp3" type="audio/mp3" />
您的浏览器不支持音频元素
</audio>
<audio ref="hangUpAudio">
<source src="/medias/hang-up.mp3" type="audio/mp3" />
您的浏览器不支持音频元素
</audio>
</el-container> </el-container>
<!-- conversation body --> <!-- conversation body -->
@ -126,12 +134,23 @@ const clientCanvasRef = ref(null);
const serverCanvasRef = ref(null); const serverCanvasRef = ref(null);
const isConnected = ref(false); const isConnected = ref(false);
const isRecording = ref(false); const isRecording = ref(false);
const backgroundAudio = ref(null);
const hangUpAudio = ref(null);
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const connect = async () => { const connect = async () => {
if (isConnected.value) { if (isConnected.value) {
return return
} }
//
if (backgroundAudio.value) {
backgroundAudio.value.play().catch(error => {
console.error('播放失败,可能是浏览器的自动播放策略导致的:', error);
});
}
//
await sleep(3000)
try { try {
await client.value.connect(); await client.value.connect();
await wavRecorder.value.begin(); await wavRecorder.value.begin();
@ -142,10 +161,12 @@ const connect = async () => {
} }
isConnected.value = true; isConnected.value = true;
backgroundAudio.value?.pause()
backgroundAudio.value.currentTime = 0
client.value.sendUserMessageContent([ client.value.sendUserMessageContent([
{ {
type: 'input_text', type: 'input_text',
text: '你好,我是老阳!', text: '你好,我是极客学长!',
}, },
]); ]);
if (client.value.getTurnDetectionType() === 'server_vad') { if (client.value.getTurnDetectionType() === 'server_vad') {
@ -283,10 +304,17 @@ onUnmounted(() => {
// //
const hangUp = async () => { const hangUp = async () => {
try { try {
isConnected.value = false; isConnected.value = false
client.value.disconnect(); //
await wavRecorder.value.end(); client.value.disconnect()
await wavStreamPlayer.value.interrupt(); //
await wavRecorder.value.end()
await wavStreamPlayer.value.interrupt()
//
backgroundAudio.value?.pause()
backgroundAudio.value.currentTime = 0
//
hangUpAudio.value?.play()
emits('close') emits('close')
} catch (e) { } catch (e) {
console.error(e) console.error(e)

View File

@ -145,13 +145,19 @@
<el-dialog <el-dialog
v-model="showContentDialog" v-model="showContentDialog"
title="消息详情" title="消息详情"
class="chat-dialog"
style="--el-dialog-width:60%"
> >
<div class="chat-line" v-html="dialogContent"></div> <div class="chat-detail">
<div class="chat-line" v-html="dialogContent"></div>
</div>
</el-dialog> </el-dialog>
<el-dialog <el-dialog
v-model="showChatItemDialog" v-model="showChatItemDialog"
title="对话详情" title="对话详情"
class="chat-dialog"
style="--el-dialog-width:60%"
> >
<div class="chat-box chat-page"> <div class="chat-box chat-page">
<div v-for="item in messages" :key="item.id"> <div v-for="item in messages" :key="item.id">
@ -354,15 +360,19 @@ const showMessages = (row) => {
justify-content right justify-content right
} }
.chat-detail {
max-height 90vh
overflow auto
}
.chat-box { .chat-box {
overflow hidden overflow auto
// //
--content-font-size: 16px; --content-font-size: 16px;
--content-color: #c1c1c1; --content-color: #c1c1c1;
font-family: 'Microsoft YaHei', '微软雅黑', Arial, sans-serif; font-family: 'Microsoft YaHei', '微软雅黑', Arial, sans-serif;
height 90% max-height 90vh
.chat-line { .chat-line {
// //