音频bug修复

This commit is contained in:
francis_fh
2026-01-23 12:12:51 +08:00
parent 5bf94a6223
commit 134c900946
3 changed files with 236 additions and 130 deletions

View File

@@ -51,101 +51,101 @@ function StreamRequestMiniProgram(url, data = {}, onDataReceived, onError, onCom
}
});
// UTF-8解码函数用于微信小程序真机环境
function utf8Decode(uint8Array) {
let result = '';
let i = 0;
const len = uint8Array.length;
while (i < len) {
const byte1 = uint8Array[i];
// 1字节字符 (0xxxxxxx)
if (byte1 < 0x80) {
result += String.fromCharCode(byte1);
i++;
}
// 2字节字符 (110xxxxx 10xxxxxx)
else if (byte1 >= 0xC0 && byte1 < 0xE0) {
if (i + 1 < len) {
const byte2 = uint8Array[i + 1];
if (byte2 >= 0x80 && byte2 < 0xC0) {
const codePoint = ((byte1 & 0x1F) << 6) | (byte2 & 0x3F);
result += String.fromCharCode(codePoint);
i += 2;
continue;
}
}
// 无效的UTF-8序列跳过
result += '<27>';
i++;
}
// 3字节字符 (1110xxxx 10xxxxxx 10xxxxxx)
else if (byte1 >= 0xE0 && byte1 < 0xF0) {
if (i + 2 < len) {
const byte2 = uint8Array[i + 1];
const byte3 = uint8Array[i + 2];
if ((byte2 >= 0x80 && byte2 < 0xC0) && (byte3 >= 0x80 && byte3 < 0xC0)) {
const codePoint = ((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F);
result += String.fromCharCode(codePoint);
i += 3;
continue;
}
}
// 无效的UTF-8序列跳过
result += '<27>';
i++;
}
// 4字节字符 (11110xxx 10xxxxxx 10xxxxxx 10xxxxxx)
else if (byte1 >= 0xF0 && byte1 < 0xF8) {
if (i + 3 < len) {
const byte2 = uint8Array[i + 1];
const byte3 = uint8Array[i + 2];
const byte4 = uint8Array[i + 3];
if ((byte2 >= 0x80 && byte2 < 0xC0) && (byte3 >= 0x80 && byte3 < 0xC0) && (byte4 >= 0x80 && byte4 < 0xC0)) {
let codePoint = ((byte1 & 0x07) << 18) | ((byte2 & 0x3F) << 12) | ((byte3 & 0x3F) << 6) | (byte4 & 0x3F);
// 处理UTF-16代理对
if (codePoint >= 0x10000) {
codePoint -= 0x10000;
const highSurrogate = (codePoint >> 10) + 0xD800;
const lowSurrogate = (codePoint & 0x3FF) + 0xDC00;
result += String.fromCharCode(highSurrogate, lowSurrogate);
} else {
result += String.fromCharCode(codePoint);
}
i += 4;
continue;
}
}
// 无效的UTF-8序列跳过
result += '<27>';
i++;
}
// 无效的UTF-8序列跳过
else {
result += '<27>';
i++;
}
}
return result;
}
// 监听分块数据
requestTask.onChunkReceived((res) => {
try {
// 微信小程序兼容处理微信小程序不支持TextDecoder使用自定义UTF-8解码
let chunk = '';
if (typeof TextDecoder !== 'undefined') {
// 支持TextDecoder的环境如开发者工具
const decoder = new TextDecoder('utf-8');
chunk = decoder.decode(new Uint8Array(res.data));
} else {
// 微信小程序真机环境使用自定义UTF-8解码函数
const uint8Array = new Uint8Array(res.data);
chunk = utf8Decode(uint8Array);
}
console.log('📦 收到分块数据:', chunk);
// UTF-8解码函数用于微信小程序真机环境
function utf8Decode(uint8Array) {
let result = '';
let i = 0;
const len = uint8Array.length;
while (i < len) {
const byte1 = uint8Array[i];
// 1字节字符 (0xxxxxxx)
if (byte1 < 0x80) {
result += String.fromCharCode(byte1);
i++;
}
// 2字节字符 (110xxxxx 10xxxxxx)
else if (byte1 >= 0xC0 && byte1 < 0xE0) {
if (i + 1 < len) {
const byte2 = uint8Array[i + 1];
if (byte2 >= 0x80 && byte2 < 0xC0) {
const codePoint = ((byte1 & 0x1F) << 6) | (byte2 & 0x3F);
result += String.fromCharCode(codePoint);
i += 2;
continue;
}
}
// 无效的UTF-8序列跳过
result += '<27>';
i++;
}
// 3字节字符 (1110xxxx 10xxxxxx 10xxxxxx)
else if (byte1 >= 0xE0 && byte1 < 0xF0) {
if (i + 2 < len) {
const byte2 = uint8Array[i + 1];
const byte3 = uint8Array[i + 2];
if ((byte2 >= 0x80 && byte2 < 0xC0) && (byte3 >= 0x80 && byte3 < 0xC0)) {
const codePoint = ((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F);
result += String.fromCharCode(codePoint);
i += 3;
continue;
}
}
// 无效的UTF-8序列跳过
result += '<27>';
i++;
}
// 4字节字符 (11110xxx 10xxxxxx 10xxxxxx 10xxxxxx)
else if (byte1 >= 0xF0 && byte1 < 0xF8) {
if (i + 3 < len) {
const byte2 = uint8Array[i + 1];
const byte3 = uint8Array[i + 2];
const byte4 = uint8Array[i + 3];
if ((byte2 >= 0x80 && byte2 < 0xC0) && (byte3 >= 0x80 && byte3 < 0xC0) && (byte4 >= 0x80 && byte4 < 0xC0)) {
let codePoint = ((byte1 & 0x07) << 18) | ((byte2 & 0x3F) << 12) | ((byte3 & 0x3F) << 6) | (byte4 & 0x3F);
// 处理UTF-16代理对
if (codePoint >= 0x10000) {
codePoint -= 0x10000;
const highSurrogate = (codePoint >> 10) + 0xD800;
const lowSurrogate = (codePoint & 0x3FF) + 0xDC00;
result += String.fromCharCode(highSurrogate, lowSurrogate);
} else {
result += String.fromCharCode(codePoint);
}
i += 4;
continue;
}
}
// 无效的UTF-8序列跳过
result += '<27>';
i++;
}
// 无效的UTF-8序列跳过
else {
result += '<27>';
i++;
}
}
return result;
}
// 监听分块数据
requestTask.onChunkReceived((res) => {
try {
// 微信小程序兼容处理微信小程序不支持TextDecoder使用自定义UTF-8解码
let chunk = '';
if (typeof TextDecoder !== 'undefined') {
// 支持TextDecoder的环境如开发者工具
const decoder = new TextDecoder('utf-8');
chunk = decoder.decode(new Uint8Array(res.data));
} else {
// 微信小程序真机环境使用自定义UTF-8解码函数
const uint8Array = new Uint8Array(res.data);
chunk = utf8Decode(uint8Array);
}
console.log('📦 收到分块数据:', chunk);
buffer += chunk;
let lines = buffer.split("\n");