JLBA202505130001_关于吉林银行新建AI智能培训系统的需求_增加语音通话和视频通话20分钟没有声音自动关闭

This commit is contained in:
田岩
2025-12-20 14:54:58 +08:00
parent 263e028bef
commit e97439a26c
2 changed files with 42 additions and 49 deletions
-1
View File
@@ -25,7 +25,6 @@ const DEFAULT_AUTH_PARAMS = {
// device_id: uni.getSystemInfoSync().deviceId || '', // 设备ID(补充)
// timestamp: Date.now() // 时间戳(防篡改)
};
// const wsUrl = 'ws://127.0.0.1:8000/ws/audio'
const url = '/trapractice/voiceCallSocket/voiceCall'
const _baseUrl = get_base_url(url).split('http').join('ws')
export default function useWebSocket(aaas = null, options = {}) {
+42 -48
View File
@@ -26,27 +26,9 @@
</view>
</template>
<script setup>
import {
ref,
reactive,
onMounted,
onUnmounted,
computed,
getCurrentInstance,
nextTick,
toRaw
} from 'vue';
import {
onShow,
onLoad,
onUnload,
onBackPress,
onReady
} from '@dcloudio/uni-app';
import {
ProtocolCodec,
MessageType
} from './js/ProtocolCodec';
import { ref, reactive, onMounted, onUnmounted, computed, getCurrentInstance, nextTick, toRaw } from 'vue';
import { onShow, onLoad, onUnload, onBackPress, onReady } from '@dcloudio/uni-app';
import { ProtocolCodec, MessageType } from './js/ProtocolCodec';
import common from '@/common/common';
import talkMicrophone from './components/talk-microphone.vue';
import talkSpeaker from './components/talk-speaker.vue';
@@ -56,12 +38,7 @@
const talkSpeakerRef = ref(null);
const talkCameraRef = ref(null);
const {
initConnection,
close,
send,
isRecording
} = useWebSocket();
const { initConnection, close, send, isRecording } = useWebSocket();
const isVideo = ref(false);
const isVideoStyle = computed(() => dataInfo.traInterTyp === '03');
@@ -94,8 +71,15 @@
const test = () => {
isVideo.value && talkCameraRef.value.switchCamera();
};
const clearAutomaticEndTimeTimer = () => {
if (automaticEndTimeTimer){
clearTimeout(automaticEndTimeTimer)
automaticEndTimeTimer = null
}
}
// 关闭销毁流程,逐层清理
const destroy = (type = 0) => {
clearAutomaticEndTimeTimer()
if (type <= 0) {
isVideo.value && talkCameraRef.value.stopPreview();
talkMicrophoneRef.value.stop();
@@ -110,16 +94,16 @@
if (dataInfo.execId) {
common.redirectTo(
'/pages/sparring/result?isOver=1&traId=' +
dataInfo.traId +
'&execId=' +
dataInfo.execId +
'&type=' +
dataInfo.traType +
'&isPreview=' +
(dataInfo.preview ? '1' : '')
dataInfo.traId +
'&execId=' +
dataInfo.execId +
'&type=' +
dataInfo.traType +
'&isPreview=' +
(dataInfo.preview ? '1' : '')
);
} else {
common.navigateBack()
common.navigateBack();
}
}, 300);
});
@@ -137,15 +121,15 @@
const onCurrentDecibels = (res) => {
console.log('当前分贝', res);
};
// 麦克风录音结束
const microphoneNoPermission= () =>{
const microphoneNoPermission = () => {
common.show('', '该功能需要授予麦克风权限', false).then((res) => {
if (res) {
destroy(); // 走关闭销毁流程
}
});
}
};
// 定时器实例
let timer = null;
@@ -160,6 +144,8 @@
};
const targetTimestamp = ref(0);
let automaticEndTime = 1200000; // 不说话就中断的时间
let automaticEndTimeTimer = null; // 不说话就中断的定时器
// 计算属性:实时格式化剩余时间
const formattedTime = ref('00:00');
// 3. 更新剩余时间的核心方法
@@ -171,6 +157,7 @@
};
const onStartRecord = (body) => {
automaticEndTime = body.automaticEndTime ?? 1200000;
dataInfo.execId = body.execId;
targetTimestamp.value = Date.now();
// 当ws连接成功时候
@@ -186,10 +173,7 @@
frameSize: 1024,
gain: 1.0,
// 音频数据的帧接口回调,直接转码后接入到websocket的发送函数
onFrameRecorded: ({
isLastFrame,
frameBuffer
}) => send(ProtocolCodec.pack(MessageType.AUDIO_DATA, frameBuffer))
onFrameRecorded: ({ isLastFrame, frameBuffer }) => send(ProtocolCodec.pack(MessageType.AUDIO_DATA, frameBuffer))
});
};
const playOver = (id) => {
@@ -202,6 +186,10 @@
lock: id
})
);
// 打开一个计时器,时间到了就中断
automaticEndTimeTimer = setTimeout(() => {
destroy();
}, automaticEndTime);
}, 300);
}
};
@@ -268,7 +256,11 @@
},
onStartRecord: onStartRecord,
playPcmCallback: (res) => talkSpeakerRef.value.playPCM(res),
setPlayNumber: (id) => talkSpeakerRef.value.setPlayNumber(id),
setPlayNumber: (id) => {
clearAutomaticEndTimeTimer()
console.log('新一句话开始');
talkSpeakerRef.value.setPlayNumber(id)
},
onError,
onClose
});
@@ -285,6 +277,7 @@
onUnmounted(() => {
console.log('组件卸载');
if (timer) clearInterval(timer);
clearAutomaticEndTimeTimer()
});
onBackPress((res) => {
if (res.from === 'backbutton') {
@@ -296,17 +289,18 @@
<style scoped lang="scss">
.isVideo {
// background-color: red;
.role-info {
display: none;
}
&.show-box {
background: linear-gradient(120deg,
rgba(51, 51, 51, 0.2) 0%,
rgba(102, 102, 102, 0.2) 50%,
rgba(51, 51, 51, 0.2) 100%);
background: linear-gradient(
120deg,
rgba(51, 51, 51, 0.2) 0%,
rgba(102, 102, 102, 0.2) 50%,
rgba(51, 51, 51, 0.2) 100%
);
backdrop-filter: blur(0px);
}
}