IOS开发2
This commit is contained in:
@@ -282,6 +282,7 @@
|
||||
tempText.value = '';
|
||||
allText.value = '';
|
||||
ossKey.value = '';
|
||||
|
||||
pressTimer = setTimeout(() => {
|
||||
if (!isRecording.value) {
|
||||
console.log('定时器被取消还是进入了');
|
||||
@@ -298,6 +299,100 @@
|
||||
// 记录开始时间戳(毫秒)
|
||||
recordStartTime.value = Date.now();
|
||||
popupRef.value.open(); // 弹出录音界面
|
||||
initWebSocket(wsUrl + '?summary=' + getToken())
|
||||
.then((socketTask) => {
|
||||
ws.value = socketTask;
|
||||
|
||||
ws.value.onMessage((res: { data: Uint8Array | ArrayBuffer }) => {
|
||||
try {
|
||||
const { msgType, body } = ProtocolCodec.unpack(res.data);
|
||||
if (msgType === MessageType.TEXT_MESSAGE) {
|
||||
tempText.value = '';
|
||||
allText.value = allText.value + body;
|
||||
} else if (msgType === MessageType.IDENTITY) {
|
||||
console.log("错误消息", body);
|
||||
const _errorBody = body as {code:number,msg:string };
|
||||
const recordDurationMs = Date.now() - recordStartTime.value;
|
||||
if(_errorBody?.code === 2) { // 认证错误,实际是登录失效
|
||||
closeWS();
|
||||
goto_login_fun();
|
||||
stopAudioRecord(); // 关掉录音
|
||||
setTimeout(() => closePopup, Math.max(700 - recordDurationMs, 0));
|
||||
return;
|
||||
} else if(_errorBody?.code === 1) {
|
||||
if (!isRecording.value) {
|
||||
ws.value = null;
|
||||
wsStatus.value = false;
|
||||
return;
|
||||
}
|
||||
console.log('开启语音');
|
||||
wsStatus.value = true;
|
||||
startRecordingTimer(true); // 启动超长定时器
|
||||
} else if(_errorBody?.code === 3) { // 路数不够等问题
|
||||
closeWS();
|
||||
stopAudioRecord(); // 关掉录音
|
||||
showErrorTip(_errorBody?.msg ?? '请稍后再试!');
|
||||
|
||||
setTimeout(() => closePopup, Math.max(700 - recordDurationMs, 0));
|
||||
}
|
||||
} else if (msgType === MessageType.TIP_MESSAGE) {
|
||||
tempText.value = body as string;
|
||||
} else if (msgType === MessageType.CONTROL_CMD) {
|
||||
// 返回结束消息
|
||||
const controlBody = body as ControlBody;
|
||||
if (controlBody.type === ControlCode.SERVER_TO_FRONT_OVER) {
|
||||
console.log(
|
||||
'识别结束',
|
||||
controlBody,
|
||||
voice_to_text_status.value,
|
||||
status.value,
|
||||
voice_to_text_status.value
|
||||
);
|
||||
// 如果不等于init说明已经超时,被超时函数处理完了,这里即使返回也不做处理
|
||||
if (
|
||||
status.value === 'voice_to_text' &&
|
||||
voice_to_text_status.value !== 'init'
|
||||
)
|
||||
return;
|
||||
if (status.value === 'send_over') return;
|
||||
// 时间太短,关掉的时候就处理了,这里不处理
|
||||
const recordDurationMs = Date.now() - recordStartTime.value;
|
||||
if (recordDurationMs <= props.minRecordDuration) return;
|
||||
closeWS();
|
||||
status.value = 'send_over'; // 设置成发送完成
|
||||
if (voiceTimer) clearTimeout(voiceTimer);
|
||||
// 设置识别结果
|
||||
voice_to_text_status.value = controlBody?.txt === '' ? 'failed' : 'success';
|
||||
// 取值出来
|
||||
ossKey.value = controlBody?.ossKey || '';
|
||||
if (typeof controlBody?.txt === 'string') {
|
||||
allText.value = controlBody.txt.trim();
|
||||
tempText.value = ''
|
||||
}
|
||||
const _isOutStatus =
|
||||
workMode.value === 'text-only' && isOutStatus.value === '01'
|
||||
? '03'
|
||||
: isOutStatus.value;
|
||||
// 只有模式为01的时候才发送出去
|
||||
console.log('只有模式为01的时候才发送出去', controlBody);
|
||||
if (_isOutStatus === '01') {
|
||||
emit('submitVoice', {
|
||||
ossKey: ossKey.value,
|
||||
text: allText.value,
|
||||
recordId: recordId.value
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('后端发来的信息有问题', e, res.data);
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
stopRecord('networkError');
|
||||
console.log('网络出现问题');
|
||||
});
|
||||
startAudioRecord({
|
||||
onFrame: (pcmData: any) => {
|
||||
if (wsStatus.value && ws.value) {
|
||||
@@ -310,8 +405,6 @@
|
||||
// 发送完成后清空队列
|
||||
voiceQueue = [];
|
||||
}
|
||||
console.log('再发送当前帧的音频数据', pcmData.length);
|
||||
// 再发送当前帧的音频数据
|
||||
ws.value.send({
|
||||
data: ProtocolCodec.pack(MessageType.AUDIO_DATA, pcmData)
|
||||
});
|
||||
@@ -323,97 +416,6 @@
|
||||
console.log('启动', res);
|
||||
recordId.value = res.recordId ?? '';
|
||||
console.log('WS连接成功', isRecording.value);
|
||||
|
||||
initWebSocket(wsUrl + '?summary=' + getToken())
|
||||
.then((socketTask) => {
|
||||
ws.value = socketTask;
|
||||
|
||||
ws.value.onMessage((res: { data: Uint8Array | ArrayBuffer }) => {
|
||||
try {
|
||||
const { msgType, body } = ProtocolCodec.unpack(res.data);
|
||||
if (msgType === MessageType.TEXT_MESSAGE) {
|
||||
tempText.value = '';
|
||||
allText.value = allText.value + body;
|
||||
} else if (msgType === MessageType.IDENTITY) {
|
||||
console.log("错误消息", body);
|
||||
const _errorBody = body as {code:number,msg:string };
|
||||
if(_errorBody?.code === 2) { // 认证错误,实际是登录失效
|
||||
closeWS();
|
||||
goto_login_fun()
|
||||
closePopup();
|
||||
return;
|
||||
} else if(_errorBody?.code === 1) {
|
||||
if (!isRecording.value) {
|
||||
ws.value = null;
|
||||
wsStatus.value = false;
|
||||
return;
|
||||
}
|
||||
console.log('开启语音');
|
||||
wsStatus.value = true;
|
||||
startRecordingTimer(true); // 启动超长定时器
|
||||
} else if(_errorBody?.code === 3) { // 路数不够等问题
|
||||
closeWS();
|
||||
closePopup();
|
||||
showErrorTip(_errorBody?.msg ?? '请稍后再试!');
|
||||
}
|
||||
} else if (msgType === MessageType.TIP_MESSAGE) {
|
||||
tempText.value = body as string;
|
||||
} else if (msgType === MessageType.CONTROL_CMD) {
|
||||
// 返回结束消息
|
||||
const controlBody = body as ControlBody;
|
||||
if (controlBody.type === ControlCode.SERVER_TO_FRONT_OVER) {
|
||||
console.log(
|
||||
'识别结束',
|
||||
controlBody,
|
||||
voice_to_text_status.value,
|
||||
status.value,
|
||||
voice_to_text_status.value
|
||||
);
|
||||
// 如果不等于init说明已经超时,被超时函数处理完了,这里即使返回也不做处理
|
||||
if (
|
||||
status.value === 'voice_to_text' &&
|
||||
voice_to_text_status.value !== 'init'
|
||||
)
|
||||
return;
|
||||
if (status.value === 'send_over') return;
|
||||
// 时间太短,关掉的时候就处理了,这里不处理
|
||||
const recordDurationMs = Date.now() - recordStartTime.value;
|
||||
if (recordDurationMs <= props.minRecordDuration) return;
|
||||
closeWS();
|
||||
status.value = 'send_over'; // 设置成发送完成
|
||||
if (voiceTimer) clearTimeout(voiceTimer);
|
||||
// 设置识别结果
|
||||
voice_to_text_status.value = controlBody?.txt === '' ? 'failed' : 'success';
|
||||
// 取值出来
|
||||
ossKey.value = controlBody?.ossKey || '';
|
||||
if (typeof controlBody?.txt === 'string') {
|
||||
allText.value = controlBody.txt.trim();
|
||||
tempText.value = ''
|
||||
}
|
||||
const _isOutStatus =
|
||||
workMode.value === 'text-only' && isOutStatus.value === '01'
|
||||
? '03'
|
||||
: isOutStatus.value;
|
||||
// 只有模式为01的时候才发送出去
|
||||
console.log('只有模式为01的时候才发送出去', controlBody);
|
||||
if (_isOutStatus === '01') {
|
||||
emit('submitVoice', {
|
||||
ossKey: ossKey.value,
|
||||
text: allText.value,
|
||||
recordId: recordId.value
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('后端发来的信息有问题', e, res.data);
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
stopRecord('networkError');
|
||||
console.log('网络出现问题');
|
||||
});
|
||||
},
|
||||
onStop: () => {
|
||||
console.log('关闭');
|
||||
@@ -425,7 +427,7 @@
|
||||
}
|
||||
}
|
||||
});
|
||||
}, 120);
|
||||
}, 70);
|
||||
};
|
||||
|
||||
// 结束录音
|
||||
@@ -437,6 +439,7 @@
|
||||
// 结束ws
|
||||
const closeWS = () => {
|
||||
startRecordingTimer(false);
|
||||
console.log('ws.value', ws.value, wsStatus.value);
|
||||
if (ws.value) {
|
||||
ws.value.close({
|
||||
code: 1000,
|
||||
@@ -452,11 +455,14 @@
|
||||
|
||||
const stopRecord = async (errrStatus = '') => {
|
||||
console.log('松手stopRecord', pressTimer, isRecording.value, status.value);
|
||||
// 核心:计算录音总时长(毫秒转秒,保留1位小数)
|
||||
const recordDurationMs = Date.now() - recordStartTime.value;
|
||||
if (pressTimer) {
|
||||
clearTimeout(pressTimer);
|
||||
console.log('取消一个存在的定时器');
|
||||
pressTimer = null;
|
||||
isRecording.value = false;
|
||||
setTimeout(() => closePopup, Math.max(700 - recordDurationMs, 0));
|
||||
setTimeout(() => {
|
||||
status.value = 'default';
|
||||
}, 50);
|
||||
@@ -468,8 +474,7 @@
|
||||
closePopup();
|
||||
return;
|
||||
}
|
||||
// 核心:计算录音总时长(毫秒转秒,保留1位小数)
|
||||
const recordDurationMs = Date.now() - recordStartTime.value;
|
||||
|
||||
console.log('recordDurationMs', recordDurationMs);
|
||||
// 拖动到转文字按钮上抬手或者仅文字模式的发送也强行算他转文字
|
||||
const _isOutStatus = workMode.value === 'text-only' && isOutStatus.value === '01' ? '03' : isOutStatus.value;
|
||||
|
||||
@@ -103,8 +103,7 @@ export function startAudioRecord(options: StartAudioRecordOptions){
|
||||
bufferSize= 2048, // 缓冲区大小,常用 1024/2048
|
||||
format= inputFormat, // 音频格式
|
||||
block=(buffer: AVAudioPCMBuffer, time: any) => {
|
||||
const frameLen = buffer.frameLength as number;
|
||||
const inputFrameLen = frameLen as Double;
|
||||
const inputFrameLen = Double(buffer.frameLength);
|
||||
const sampleRateRatio = targetSampleRate / inputFormat.sampleRate;
|
||||
const outputFrameCounta = inputFrameLen * sampleRateRatio
|
||||
const outputFrameCount = AVAudioFrameCount(outputFrameCounta);
|
||||
|
||||
Reference in New Issue
Block a user