Discuz! BBS

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 172|回复: 0

奇怪的closed

[复制链接]

426

主题

598

帖子

3491

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3491
发表于 2025-11-7 02:45:40 | 显示全部楼层 |阅读模式
E Connection closed:
E Error:
E 识别结果(长字符串): Client result ERROR

根据日志分析,问题出现在连接被意外关闭导致识别失败。以下是可能的原因和解决方案:
问题分析
连接提前关闭:从日志可以看到,在收到partial结果后连接立即被关闭
错误处理不完整:onClose 和 onError 方法中的错误处理可能不够完善
资源管理问题:连接关闭后没有正确处理后续操作
建议的解决方案
1. 改进 onMessage 方法

  1. @Override
  2. public void onMessage(String message) {
  3.     Log.i(tag, "Received message: " + message);
  4.    
  5.     // 更新resultText以存储最新的识别结果
  6.     if (message.contains("text") || message.contains("partial")) {
  7.         try {
  8.             JSONObject json = new JSONObject(message);
  9.             if (json.has("text")) {
  10.                 resultText = json.getString("text");
  11.             } else if (json.has("partial")) {
  12.                 resultText = json.getString("partial");
  13.             }
  14.         } catch (Exception e) {
  15.             // 如果不是JSON格式,直接使用原始消息
  16.             resultText = message;
  17.         }
  18.     }
  19.    
  20.     // 检查是否为EOF回执或其他重要消息
  21.     if (message.contains("eof") || message.contains("result") ||
  22.         message.contains("text") || message.contains("partial")) {
  23.         if (mCallBack != null) {
  24.             mCallBack.onGettingMsg(message);
  25.         }
  26.     }
  27. }
复制代码
2. 改进 onClose 方法
  1. @Override
  2. public void onClose(int code, String reason, boolean remote) {
  3.     Log.e(tag, "Connection closed: " + reason + ", code: " + code);
  4.     finished = true;
  5.    
  6.     // 确保文件流被关闭
  7.     closeFileStream();
  8.    
  9.     // 只有当不是正常关闭时才调用错误回调
  10.     if (mCallBack != null && code != 1000) { // 1000是正常关闭码
  11.         mCallBack.onError("Connection closed: " + reason);
  12.     }
  13. }
复制代码
3. 改进 sendAudioData 方法
  1. public void sendAudioData() {
  2.     new Thread(() -> {
  3.         try {
  4.             Log.i(tag, "@@@@@@ 准备发送文件 @@@@@@");
  5.             File audioFile = new File(audioFilePath);
  6.             fileInputStream = new FileInputStream(audioFile);

  7.             // 发送配置信息
  8.             sendConfig();

  9.             try {
  10.                 Thread.sleep(1000);
  11.             } catch (InterruptedException e) {
  12.                 Log.e(tag, "Sleep interrupted: " + e.getMessage());
  13.                 Thread.currentThread().interrupt();
  14.                 return;
  15.             }

  16.             int bufferSize = (int) (rate * 0.2 * 2);
  17.             byte[] buffer = new byte[bufferSize];

  18.             int bytesRead;
  19.             while ((bytesRead = fileInputStream.read(buffer)) != -1 && !finished) {
  20.                 send(java.util.Arrays.copyOf(buffer, bytesRead));
  21.             }
  22.             
  23.             // 只有在成功发送完数据后才发送EOF
  24.             if (!finished) {
  25.                 sendEOF();
  26.                 Log.i(tag, "发送成功!");
  27.             }
  28.         } catch (IOException e){
  29.             Log.e(tag, "File not found:" + e.getMessage());
  30.             if (mCallBack != null) {
  31.                 mCallBack.onError("File error: " + e.getMessage());
  32.             }
  33.         } catch (Exception e) {
  34.             Log.e(tag, "Error: " + e.getMessage());
  35.             if (mCallBack != null) {
  36.                 mCallBack.onError("Send error: " + e.getMessage());
  37.             }
  38.         } finally {
  39.             finished = true;
  40.             closeFileStream();
  41.         }
  42.     }).start();
  43. }
复制代码
这些修改应该能解决连接提前关闭和结果获取不完整的问题。


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-12-7 15:34 , Processed in 0.013302 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表