解决4GAi豆包ai开头会发“\n\n”造成答案开头出现空白问题 2085/1 2085/head 2086/target/1 2087/target/1
authorluozheng <1732036298@qq.com>
Fri, 15 Aug 2025 07:42:34 +0000 (15:42 +0800)
committerluozheng <1732036298@qq.com>
Fri, 15 Aug 2025 07:42:34 +0000 (15:42 +0800)
code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AiVoiceModel.h
code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp

index 4b3a568..749b2b2 100644 (file)
@@ -163,6 +163,10 @@ namespace OHOS
 
         bool getentryTextViewState(void);
 
+        bool get_is_first_receive();
+        void set_is_first_receive(bool);
+        void delete_start_line_break_str(std::string &str);
+
     private:
         AiVoiceModel();
         ~AiVoiceModel();
@@ -198,6 +202,7 @@ namespace OHOS
         uint8_t select4gorbt{-1};           //0:4g 1:蓝牙 -1:未连接
 
         bool is_delay_start_asr{false};
+        bool isFirstReceive{true};        //静态变量标记是否是首次接收
     };
 
 }
index e45606d..6519562 100644 (file)
@@ -800,6 +800,7 @@ namespace OHOS
         {
             if (!OHOS::AiVoiceModel::GetInstance().GetIsLimitDisplayed()) // 如果ai答案没有显示到极限了,就更新ai答案字符串
             {
+                delete_start_line_break_str(str);
                 aiAnswerStr += str;
                 /* -------------------------------------------------------------------------- */
                 // 累积接收到的文本
@@ -858,6 +859,7 @@ namespace OHOS
         userQuestionStr = "";
         aiAnswerStr = "";
         accumulatedText = "";
+        set_is_first_receive(true);
     }
 
     /*获取AI音频路径*/
@@ -1527,7 +1529,7 @@ namespace OHOS
     static uint8_t tts_record_flag = 0;
     // 添加一个静态变量用于跟踪连续出现0的次数
     static int s_tts_tick_zero_count = 0;
-    static int continue_zero_count = 0;     // 静态变量:记录连续为0的次数(初始化为0)
+    static int continue_zero_count = 0; // 静态变量:记录连续为0的次数(初始化为0)
     static void AI4G_IS_START_TTS_CallbackProc(int data)
     {
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AI4G_IS_START_TTS_CallbackProc:%s", AiVoiceModel::GetInstance().GetAccumulatedText().c_str());
@@ -1644,8 +1646,8 @@ namespace OHOS
             }
             g_ai4g_is_start_tts_play_Handle = nullptr;
             AI4G_IS_START_TTS_PLAY_CNT = NUMBER_TEN;
-            s_tts_tick_zero_count = 0;  // 重置计数器
-            continue_zero_count = 0; // 重置静态变量
+            s_tts_tick_zero_count = 0; // 重置计数器
+            continue_zero_count = 0;   // 重置静态变量
         }
     }
 
@@ -1819,6 +1821,33 @@ namespace OHOS
         return entryTextViewState;
     }
 
+    bool AiVoiceModel::get_is_first_receive()
+    {
+        return isFirstReceive;
+    }
+
+    void AiVoiceModel::set_is_first_receive(bool val)
+    {
+        isFirstReceive = val;
+    }
+
+    void AiVoiceModel::delete_start_line_break_str(std::string &str)
+    {
+        if (GetSelect4gorbt() == AI_SELECT_4G && GetAiModelTextId() == STR_AIMODELS_DOUBAO)
+        {
+            if (isFirstReceive)
+            {
+                isFirstReceive = false;
+                // 检查是否以"\n\n"开头
+                if (str.size() >= 2 && str[0] == '\n' && str[1] == '\n')
+                {
+                    // 去除开头的两个换行符
+                    str = str.substr(2);
+                }
+            }
+        }
+    }
+
     /*获取已选择的ai模型的textid*/
     int AiVoiceModel::GetAiModelTextId()
     {