Ai_GirlFriend/xuniYou/文件读取问题诊断.md
2026-03-02 18:57:11 +08:00

210 lines
4.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 文件读取问题诊断
## 🔍 当前问题
从最新日志发现:
1.`onStop` 回调已触发
2. ✅ 有文件路径:`_doc/uniapp_temp_1772423197943/recorder/1772423239902.pcm`
3.**文件路径是相对路径,不是绝对路径**
4. ❌ 没有看到 "✅ 文件读取成功" 或 "❌ 文件读取失败" 的日志
5. ❌ 说明 `fs.readFile` 的回调没有被触发
## 🔧 已添加的修复
### 1. 文件路径转换
```javascript
// 如果是相对路径,转换为绝对路径
let filePath = res.tempFilePath
if (!filePath.startsWith('/') && !filePath.includes('://')) {
// #ifdef APP-PLUS
filePath = plus.io.convertLocalFileSystemURL(filePath)
console.log('📁 转换后的绝对路径:', filePath)
// #endif
}
```
### 2. 添加超时保护
```javascript
let readTimeout = setTimeout(() => {
console.error('❌ 文件读取超时5秒')
}, 5000)
```
### 3. 增强错误日志
```javascript
fail: (err) => {
console.error('❌ 文件读取失败:', err)
console.error('错误代码:', err.errCode)
console.error('错误信息:', err.errMsg)
console.error('尝试读取的文件路径:', filePath)
}
```
## 🧪 测试步骤
### 1. 重新编译
在 HBuilderX 中:
1. 停止当前运行
2. 清理缓存
3. 重新运行到手机
### 2. 测试并观察日志
按住说话 3-5 秒,观察以下关键日志:
```
⏹️ 录音已停止
📁 文件路径: xxx
📁 转换后的绝对路径: xxx ← 新增!检查路径是否正确
📂 获取文件系统管理器: 成功
📁 准备读取文件: xxx
```
然后应该看到以下之一:
**成功情况:**
```
✅ 文件读取成功
📊 数据类型: object
📊 是否为 ArrayBuffer: true
📊 实际文件大小: 160000 bytes
📦 开始分片发送
```
**失败情况:**
```
❌ 文件读取失败: xxx
错误代码: xxx
错误信息: xxx
```
**超时情况:**
```
❌ 文件读取超时5秒
```
## 🐛 可能的问题和解决方案
### 问题 1文件路径转换失败
**症状:**
- 没有看到 "📁 转换后的绝对路径" 日志
- 或者转换后的路径还是相对路径
**解决方案:**
尝试使用 `uni.env.USER_DATA_PATH` 拼接路径:
```javascript
let filePath = res.tempFilePath
if (!filePath.startsWith('/')) {
// 使用用户数据目录
filePath = `${uni.env.USER_DATA_PATH}/${filePath}`
console.log('📁 拼接后的路径:', filePath)
}
```
### 问题 2文件不存在
**症状:**
- 看到 "❌ 文件读取失败"
- 错误信息包含 "file not found" 或类似
**解决方案:**
检查文件是否真实存在:
```javascript
// 在读取前先检查文件是否存在
fs.access({
path: filePath,
success: () => {
console.log('✅ 文件存在,开始读取')
// 读取文件
},
fail: () => {
console.error('❌ 文件不存在:', filePath)
}
})
```
### 问题 3权限问题
**症状:**
- 看到 "❌ 文件读取失败"
- 错误信息包含 "permission denied" 或类似
**解决方案:**
1. 检查 App 权限设置
2. 确保在 `manifest.json` 中配置了存储权限:
```json
{
"permissions": {
"WRITE_EXTERNAL_STORAGE": {
"desc": "存储权限"
},
"READ_EXTERNAL_STORAGE": {
"desc": "读取存储权限"
}
}
}
```
### 问题 4录音格式问题
**症状:**
- 文件读取成功
- 但是文件大小为 0 或很小
**解决方案:**
尝试修改录音格式:
```javascript
const recorderOptions = {
format: 'aac', // 改为 aac 格式
// ... 其他参数
}
```
## 📝 备用方案:使用 plus.io
如果 `uni.getFileSystemManager()` 一直有问题,可以尝试使用 `plus.io`
```javascript
// #ifdef APP-PLUS
plus.io.resolveLocalFileSystemURL(res.tempFilePath, (entry) => {
entry.file((file) => {
const reader = new plus.io.FileReader()
reader.onloadend = (e) => {
console.log('✅ 文件读取成功')
const arrayBuffer = e.target.result
this.sendAudioInChunks(arrayBuffer)
}
reader.readAsArrayBuffer(file)
})
}, (err) => {
console.error('❌ 文件读取失败:', err)
})
// #endif
```
## 🚀 下一步
1. **重新编译并测试**
2. **观察新增的日志**
3. **根据日志确定具体问题**
4. **如果还有问题,提供完整日志**
重点关注:
- 📁 转换后的绝对路径是什么?
- ✅ 文件读取成功 还是 ❌ 文件读取失败?
- 如果失败,错误代码和错误信息是什么?