change .cn to .com
This commit is contained in:
@@ -4,7 +4,7 @@ let system_message_idx = 0
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
data = window.appData;
|
||||
console.log(data);
|
||||
socket = io('wss://hsamooc.cn/agent', {
|
||||
socket = io('wss://hsamooc.com/agent', {
|
||||
query: {
|
||||
user_uuid: data.user_uuid,
|
||||
folder: data.folder
|
||||
@@ -207,15 +207,133 @@ function sendMessage() {
|
||||
document.getElementById('chatbox').scrollTop = document.getElementById('chatbox').scrollHeight;
|
||||
}
|
||||
|
||||
let voice_stream = null;
|
||||
let audio_chunks = [];
|
||||
// let voice_stream = null;
|
||||
// let audio_chunks = [];
|
||||
// let isRecording = false;
|
||||
// let recordingInterval = null;
|
||||
// //鼠标点击终止事件
|
||||
// function handleMouseClick(event) {
|
||||
// event.stopPropagation();
|
||||
// stopRecording();
|
||||
// }
|
||||
// //发送语音
|
||||
// const startAudioRecording = async () => {
|
||||
// try {
|
||||
// const stream = await navigator.mediaDevices.getUserMedia({
|
||||
// audio: {
|
||||
// sampleRate: 16000,
|
||||
// channelCount: 1,
|
||||
// echoCancellation: true,
|
||||
// noiseSuppression: true
|
||||
// }
|
||||
// });
|
||||
// const mediaRecorder = new MediaRecorder(stream);
|
||||
// const audioContext = new AudioContext({
|
||||
// sampleRate: 16000
|
||||
// });
|
||||
|
||||
// // Load the AudioWorklet
|
||||
// await audioContext.audioWorklet.addModule('/static/js/audio-processor.js');
|
||||
|
||||
// const source = audioContext.createMediaStreamSource(stream);
|
||||
// const workletNode = new AudioWorkletNode(audioContext, 'audio-processor');
|
||||
// let audioChunks = [];
|
||||
|
||||
// workletNode.port.onmessage = (e) => {
|
||||
// audioChunks.push(new Float32Array(e.data));
|
||||
|
||||
// if (audioChunks.length >= 16) {
|
||||
// // 合并所有音频块
|
||||
// const totalLength = audioChunks.reduce((acc, chunk) => acc + chunk.length, 0);
|
||||
// const mergedData = new Float32Array(totalLength);
|
||||
// let offset = 0;
|
||||
// for (const chunk of audioChunks) {
|
||||
// mergedData.set(chunk, offset);
|
||||
// offset += chunk.length;
|
||||
// }
|
||||
|
||||
// const audioMessage = {
|
||||
// id: window.data.user_uuid,
|
||||
// type: 'audio',
|
||||
// data: mergedData,
|
||||
// sampleRate: 16000,
|
||||
// timestamp: Date.now()
|
||||
// };
|
||||
// socket.emit('voice', audioMessage);
|
||||
// console.log("voice sended")
|
||||
// audioChunks = [];
|
||||
// }
|
||||
// };
|
||||
|
||||
// source.connect(workletNode);
|
||||
// workletNode.connect(audioContext.destination);
|
||||
|
||||
// mediaRecorder.value = {
|
||||
// stop: () => {
|
||||
// source.disconnect();
|
||||
// workletNode.disconnect();
|
||||
// stream.getTracks().forEach(track => track.stop());
|
||||
// audioContext.close();
|
||||
// }
|
||||
// }
|
||||
|
||||
// isRecording.value = true;
|
||||
// } catch (error) {
|
||||
// console.error('Error starting audio recording:', error);
|
||||
// }
|
||||
// };
|
||||
// function sendVoiceMessage() {
|
||||
// startAudioRecording()
|
||||
// //变换录音中图标
|
||||
// document.getElementById('recordIcon').className = 'bi bi-record-circle';
|
||||
// document.addEventListener('click', handleMouseClick);
|
||||
// }
|
||||
|
||||
// //停止录音
|
||||
// function stopRecording() {
|
||||
// if (!isRecording) return;
|
||||
// isRecording = false;
|
||||
// //取消监听
|
||||
// document.removeEventListener('click', handleMouseClick);
|
||||
// //取消定时器
|
||||
// if (recordingInterval) {
|
||||
// clearInterval(recordingInterval);
|
||||
// recordingInterval = null;
|
||||
// }
|
||||
// //变换图标
|
||||
// document.getElementById('recordIcon').className = 'bi bi-mic';
|
||||
// //发送剩下的数据
|
||||
// if (audio_chunks.length > 0) {
|
||||
// const merged_data = new Uint8Array(audio_chunks.reduce((acc, chunk) => acc + chunk.length, 0));
|
||||
// let offset = 0;
|
||||
// for (const chunk of audio_chunks) {
|
||||
// merged_data.set(new Uint8Array(chunk), offset);
|
||||
// offset += chunk.length;
|
||||
// }
|
||||
// socket.emit('VoiceMessage', merged_data, { binary: true });
|
||||
// audio_chunks = [];
|
||||
// }
|
||||
// overlay.style.display = 'none';
|
||||
// //关闭音频流
|
||||
// if (voice_stream) {
|
||||
// voice_stream.stop_stream();
|
||||
// voice_stream.close();
|
||||
// voice_stream = null;
|
||||
// }
|
||||
// if (p) {
|
||||
// p.terminate();
|
||||
// p = null;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
//语音这一块//
|
||||
let mediaRecorderInstance = null;
|
||||
let audioContextInstance = null;
|
||||
let workletNodeInstance = null;
|
||||
let sourceInstance = null;
|
||||
let streamInstance = null;
|
||||
let isRecording = false;
|
||||
let recordingInterval = null;
|
||||
//鼠标点击终止事件
|
||||
function handleMouseClick(event) {
|
||||
event.stopPropagation();
|
||||
stopRecording();
|
||||
}
|
||||
//发送语音
|
||||
const startAudioRecording = async () => {
|
||||
try {
|
||||
@@ -227,19 +345,31 @@ const startAudioRecording = async () => {
|
||||
noiseSuppression: true
|
||||
}
|
||||
});
|
||||
|
||||
// 保存实例到全局变量
|
||||
streamInstance = stream;
|
||||
const mediaRecorder = new MediaRecorder(stream);
|
||||
mediaRecorderInstance = mediaRecorder;
|
||||
|
||||
const audioContext = new AudioContext({
|
||||
sampleRate: 16000
|
||||
});
|
||||
audioContextInstance = audioContext;
|
||||
|
||||
// Load the AudioWorklet
|
||||
await audioContext.audioWorklet.addModule('/static/js/audio-processor.js');
|
||||
|
||||
const source = audioContext.createMediaStreamSource(stream);
|
||||
sourceInstance = source;
|
||||
|
||||
const workletNode = new AudioWorkletNode(audioContext, 'audio-processor');
|
||||
let audioChunks = [];
|
||||
workletNodeInstance = workletNode;
|
||||
|
||||
audioChunks = [];
|
||||
|
||||
workletNode.port.onmessage = (e) => {
|
||||
if (!isRecording) return;
|
||||
|
||||
audioChunks.push(new Float32Array(e.data));
|
||||
|
||||
if (audioChunks.length >= 16) {
|
||||
@@ -260,7 +390,7 @@ const startAudioRecording = async () => {
|
||||
timestamp: Date.now()
|
||||
};
|
||||
socket.emit('voice', audioMessage);
|
||||
console.log("voice sended")
|
||||
console.log("voice sended");
|
||||
audioChunks = [];
|
||||
}
|
||||
};
|
||||
@@ -268,63 +398,75 @@ const startAudioRecording = async () => {
|
||||
source.connect(workletNode);
|
||||
workletNode.connect(audioContext.destination);
|
||||
|
||||
mediaRecorder.value = {
|
||||
stop: () => {
|
||||
source.disconnect();
|
||||
workletNode.disconnect();
|
||||
stream.getTracks().forEach(track => track.stop());
|
||||
audioContext.close();
|
||||
}
|
||||
}
|
||||
|
||||
isRecording.value = true;
|
||||
isRecording = true;
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error starting audio recording:', error);
|
||||
// 发生错误时清理资源
|
||||
stopRecording();
|
||||
}
|
||||
};
|
||||
function sendVoiceMessage() {
|
||||
startAudioRecording()
|
||||
//变换录音中图标
|
||||
document.getElementById('recordIcon').className = 'bi bi-record-circle';
|
||||
document.addEventListener('click', handleMouseClick);
|
||||
}
|
||||
|
||||
//停止录音
|
||||
function stopRecording() {
|
||||
if (!isRecording) return;
|
||||
if (!isRecording) return;
|
||||
// 停止所有音频处理
|
||||
if (workletNodeInstance) {
|
||||
workletNodeInstance.disconnect();
|
||||
workletNodeInstance = null;
|
||||
}
|
||||
if (sourceInstance) {
|
||||
sourceInstance.disconnect();
|
||||
sourceInstance = null;
|
||||
}
|
||||
if (audioContextInstance && audioContextInstance.state !== 'closed') {
|
||||
audioContextInstance.close();
|
||||
audioContextInstance = null;
|
||||
}
|
||||
// 停止媒体流
|
||||
if (streamInstance) {
|
||||
streamInstance.getTracks().forEach(track => {
|
||||
track.stop();
|
||||
});
|
||||
streamInstance = null;
|
||||
}
|
||||
// 重置录音状态
|
||||
isRecording = false;
|
||||
//取消监听
|
||||
mediaRecorderInstance = null;
|
||||
audioChunks = [];
|
||||
// 恢复麦克风图标
|
||||
const recordIcon = document.getElementById('recordIcon');
|
||||
if (recordIcon) {
|
||||
recordIcon.className = 'bi bi-mic';
|
||||
}
|
||||
// 移除点击事件监听器
|
||||
document.removeEventListener('click', handleMouseClick);
|
||||
//取消定时器
|
||||
if (recordingInterval) {
|
||||
clearInterval(recordingInterval);
|
||||
recordingInterval = null;
|
||||
}
|
||||
//变换图标
|
||||
document.getElementById('recordIcon').className = 'bi bi-mic';
|
||||
//发送剩下的数据
|
||||
if (audio_chunks.length > 0) {
|
||||
const merged_data = new Uint8Array(audio_chunks.reduce((acc, chunk) => acc + chunk.length, 0));
|
||||
let offset = 0;
|
||||
for (const chunk of audio_chunks) {
|
||||
merged_data.set(new Uint8Array(chunk), offset);
|
||||
offset += chunk.length;
|
||||
}
|
||||
socket.emit('VoiceMessage', merged_data, { binary: true });
|
||||
audio_chunks = [];
|
||||
}
|
||||
overlay.style.display = 'none';
|
||||
//关闭音频流
|
||||
if (voice_stream) {
|
||||
voice_stream.stop_stream();
|
||||
voice_stream.close();
|
||||
voice_stream = null;
|
||||
}
|
||||
if (p) {
|
||||
p.terminate();
|
||||
p = null;
|
||||
}
|
||||
}
|
||||
//鼠标点击终止事件
|
||||
function handleMouseClick(event) {
|
||||
event.stopPropagation();
|
||||
stopRecording();
|
||||
}
|
||||
function sendVoiceMessage() {
|
||||
if (isRecording) {
|
||||
stopRecording();
|
||||
return;
|
||||
}
|
||||
startAudioRecording();
|
||||
// 变换录音中图标
|
||||
const recordIcon = document.getElementById('recordIcon');
|
||||
if (recordIcon) {
|
||||
recordIcon.className = 'bi bi-record-circle';
|
||||
}
|
||||
// 添加点击事件监听器
|
||||
document.addEventListener('click', handleMouseClick);
|
||||
}
|
||||
//页面卸载时停止录音
|
||||
window.addEventListener('beforeunload', () => {
|
||||
if (isRecording) {
|
||||
stopRecording();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
let currentChapterIndex = -1;
|
||||
function next_chapter(data) {
|
||||
|
||||
Reference in New Issue
Block a user