正在录音视图(subView),添加计时功能,并实现录音完,切换到录音列表视图同步刷新刚刚录的音
authorluozheng <1732036298@qq.com>
Tue, 5 Mar 2024 07:28:38 +0000 (15:28 +0800)
committerluozheng <1732036298@qq.com>
Tue, 5 Mar 2024 07:28:38 +0000 (15:28 +0800)
13 files changed:
code/sdk/application/wearable/fd230app/nativeui/applist/src/ApplistPresenter.cpp
code/sdk/application/wearable/fd230app/nativeui/record/include/RecordGoingView.h
code/sdk/application/wearable/fd230app/nativeui/record/include/RecordListView.h
code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingListAdapter.h
code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingModel.h
code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingPresenter.h
code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingView.h
code/sdk/application/wearable/fd230app/nativeui/record/src/RecordGoingView.cpp
code/sdk/application/wearable/fd230app/nativeui/record/src/RecordListView.cpp
code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingListAdapter.cpp
code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingModel.cpp
code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingPresenter.cpp
code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingView.cpp

index ffb4de6..e4f3f27 100644 (file)
@@ -79,7 +79,7 @@ namespace OHOS
         UNUSED(event);
         AppItemView *itemView = static_cast<AppItemView *>(&view);
         AppViewId viewId = itemView->GetViewId();
-        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ApplistPresenter::OnClick viewid = %d %d %s\n", viewId,__LINE__,__func__);
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "ApplistPresenter::OnClick viewid = %d\n", viewId);
         if (viewId < VIEW_MAX_INTER_APP)
         {
             NativeAbility::GetInstance().ChangeSlice(viewId);
index abfc4a7..9dde355 100644 (file)
@@ -31,7 +31,7 @@ namespace OHOS
         static RecordGoingView *GetInstance(void);\r
         UIScrollView *RecordGoingViewInit(void);\r
         void ClearAll(void);\r
-\r
+        UILabel *GetRecordingDurationLabel(void);\r
     private:\r
         bool SetUpScrollView(void);\r
         UILabel *SetUpLabel(LabelConfig &labelconfig); // 设置标签\r
@@ -45,7 +45,7 @@ namespace OHOS
         UIImageView *RECORD_GOING_IMAGE{nullptr};\r
         UIButton *RECORD_GOING_BUTTON{nullptr};\r
 \r
-        \r
+\r
         bool initViewStatus{false};\r
     };\r
 } // namespace OHOS\r
index 610055b..179b5e5 100644 (file)
@@ -35,7 +35,7 @@ namespace OHOS
         UIScrollView *RecordListViewInit(void);\r
         void ClearAll(void);\r
         void IsVisibleLabel(bool value);\r
-\r
+        void RefreshRecordingListData(void);\r
     private:\r
         bool SetUpScrollView(void);\r
         UILabel *SetUpLabel(LabelConfig &labelconfig); // 设置标签\r
index 923e3c0..adf226a 100644 (file)
@@ -30,6 +30,7 @@ namespace OHOS
          */
         void SetData(std::list<RecordingListItem> &data);
 
+        void ClearData(void);
         /* *
          * @brief Obtains the <b>data</b> size of the UILabel adapter.
          *
index 4d95a1d..ebf3053 100644 (file)
@@ -70,6 +70,11 @@ namespace OHOS
         std::string GetCleanFileName(const std::string &filePath);
         void SearchFiles(const std::string &directory, std::vector<std::string> &fileNames, int &mp3FileCount);
 
+        /* ----------------------------------- 定时器 ---------------------------------- */
+        void Recording_StartTimer(uint32_t interval);
+        void Recording_StopTimer(void);
+        char *DisplayRecordDuration(void);
+
     private:
         RecordingModel();
         RecordingModel(const RecordingModel &) = delete;
@@ -93,6 +98,9 @@ namespace OHOS
         int RecordingMp3FileCount = 0;      // 录音MP3数量
         const std::string directory = "/user/res/record/recording/";
         RecordingListItem *Recordings = nullptr;
+
+        /* ----------------------------------- 定时器 ---------------------------------- */
+        std::string recordDuration; // 录音时长
     };
 }
 
index 05a6154..c809261 100644 (file)
@@ -16,6 +16,7 @@ namespace OHOS
         MAX_RECORDINGVIEWID,\r
     } RECORDINGVIEWID;\r
     static const char *RECORD_GOING_BUTTON_VIEWID = "RECORD_GOING_BUTTON"; // 点击按钮后停止录音\r
+    static const char *RECORD_BUTTON = "RecordButton";  //录音按钮\r
     class RecordListViewOnClickListener : public UIView::OnClickListener\r
     {\r
     public:\r
index eb7fb84..13a12e1 100644 (file)
 \r
 #include "record/include/RecordListView.h"\r
 #include "record/include/RecordGoingView.h"\r
+\r
+#define NUM_MAX_RECORDINGVIEWSCROLLVIEW 10\r
 namespace OHOS\r
 {\r
-    static const char *RECORD_BUTTON = "RecordButton";  //录音按钮\r
+\r
     class RecordingPresenter;\r
     class RecordingView : public View<RecordingPresenter>\r
     {\r
@@ -34,9 +36,9 @@ namespace OHOS
         virtual ~RecordingView();\r
         static RecordingView *GetInstance(void);\r
 \r
-        bool ChangeRecordingView(int16_t thisView); // 切换到“录音”视图\r
-        bool ChangeRecordListView(int16_t thisView);//切换到"录音列表"视图\r
-        bool ChangeRecordGoingView(int16_t thisView);//切换到“正在录音”视图\r
+        bool ChangeRecordingView(int16_t thisView);   // 切换到“录音”视图\r
+        bool ChangeRecordListView(int16_t thisView);  // 切换到"录音列表"视图\r
+        bool ChangeRecordGoingView(int16_t thisView); // 切换到“正在录音”视图\r
         bool ClickFunc(int16_t interface);\r
         void ChangeView(int16_t hideView, int16_t showView);\r
         UIScrollView *GetRecordingViewScrollView(int i);\r
@@ -49,14 +51,15 @@ namespace OHOS
 \r
     private:\r
         RecordListView *p_RecordListView{nullptr};\r
-        RecordGoingView*p_RecordGoingView{nullptr};\r
+        RecordGoingView *p_RecordGoingView{nullptr};\r
+\r
     private:\r
         void RecordingViewInit(void);\r
         void ClearAll(void);\r
         bool SetUpScrollView(void);\r
         bool SetTheTitle(void);\r
         UIButton *SetTheButton(ButtonConfig &buttonconfig, UIView::OnClickListener *clickListener);\r
-        UIScrollView *RecordingViewScrollView[3]{nullptr};\r
+        UIScrollView *RecordingViewScrollView[NUM_MAX_RECORDINGVIEWSCROLLVIEW]{nullptr};\r
         UILabel *RecordingViewTitleLabel{nullptr};\r
         UIButton *RecordingViewButton{nullptr};\r
         UILabel *AttentionLabel{nullptr};\r
index 33b40d8..ac2ebfc 100644 (file)
@@ -123,7 +123,7 @@ namespace OHOS
             .height = 77,\r
             .fontSize = 65,\r
             .lineHeigh = 76,\r
-            .text = "00:00:12",\r
+            .text = "00:00:00",\r
             .lineBreakMode = UILabel::LINE_BREAK_WRAP,\r
         };\r
         Timer = SetUpLabel(labelconfig);\r
@@ -170,6 +170,16 @@ namespace OHOS
         return true;\r
     }\r
 \r
+    UILabel *RecordGoingView::GetRecordingDurationLabel(void)\r
+    {\r
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RecordGoingView::GetRecordingDurationLabel");\r
+        if(Timer == nullptr)\r
+        {\r
+            return nullptr;\r
+        }\r
+        return Timer;\r
+    }\r
+\r
     UIScrollView *RecordGoingView::RecordGoingViewInit()\r
     {\r
         if (!SetUpScrollView())\r
index c8a094f..9debc92 100644 (file)
@@ -174,6 +174,16 @@ namespace OHOS
         }\r
     }\r
 \r
+    // 每次录音完将数据刷新显示到list\r
+    void RecordListView::RefreshRecordingListData(void)\r
+    {\r
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordListView::RefreshRecordingListData");\r
+        RecordingModel::GetInstance()->InitRecordingListItem();\r
+        p_RecordingListAdapter->ClearData();\r
+        InitRecordingInfo();\r
+        recordList->RefreshList();\r
+    }\r
+\r
     // 设置提醒标签是否可见\r
     void RecordListView::IsVisibleLabel(bool value)\r
     {\r
index 05841ae..a558f12 100644 (file)
@@ -30,6 +30,14 @@ namespace OHOS
         }
     }
 
+    void RecordingListAdapter::ClearData(void)
+    {
+        if (!listData.empty())
+        {
+            listData.clear();
+        }
+    }
+
     UIView *RecordingListAdapter::GetView(UIView *inView, int16_t index)
     {
         // 1.先检查列表数据是否为空,若为空则返回空指针
@@ -60,8 +68,7 @@ namespace OHOS
         if (inView == nullptr)
         {
             item = new RecordingListitemView();
-            
-        }
+                }
         else
         {
             item = static_cast<RecordingListitemView *>(inView);
index 3d1f560..8189ea5 100644 (file)
@@ -6,7 +6,9 @@
 #include "audio_manager.h"
 #include <wearable_log.h>
 #include "../../../../../../drivers/peripheral/utils/custom_utils.h"
-
+#include <cmsis_os2.h>
+#include <service/graphic_service.h>
+#include "record/include/RecordGoingView.h"
 namespace OHOS
 {
 
@@ -182,7 +184,7 @@ namespace OHOS
     void RecordingModel::InitRecordingListItem()
     {
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordingModel::InitRecordingListItem");
-        char *date = uapi_get_CurrentDate();//获取日期
+        char *date = uapi_get_CurrentDate(); // 获取日期
         RecordingMp3FileCount = 0;
         if (!fileNames.empty())
         {
@@ -231,4 +233,157 @@ namespace OHOS
         return RecordingMp3FileCount;
     }
 
+    /* ----------------------------------- 定时器 ---------------------------------- */
+    // 声明定时器变量
+    osTimerId_t g_timer = NULL;
+    static int g_RecordingTimes = {0};
+    static constexpr int32 NUM_TEN = 10;
+    static constexpr int16 ERROR = 0;
+    static constexpr int16 SUCCESS = 1;
+    static constexpr int16 NUM_TIME_MIN = 60;
+    static constexpr int16 NUM_TIME_HOUR = 3600;
+    static constexpr int32 NUM_TIME_DAY = 86400;
+
+    /// @brief 显示录音时长
+    char *RecordingModel::DisplayRecordDuration()
+    {
+        recordDuration.erase();
+        std::string hours;
+        std::string minute;
+        std::string seconds;
+        if (g_RecordingTimes >= NUM_TIME_DAY)
+        {
+            g_RecordingTimes = 0;
+        }
+        int hour = g_RecordingTimes / NUM_TIME_HOUR;
+        int min = (g_RecordingTimes % NUM_TIME_HOUR) / NUM_TIME_MIN;
+        int sec = (g_RecordingTimes % NUM_TIME_HOUR) % NUM_TIME_MIN;
+        if (hour >= 0 && hour < NUM_TEN)
+        {
+            hours = "0" + std::to_string(hour);
+        }
+        else
+        {
+            hours = std::to_string(hour);
+        }
+        if (min >= 0 && min < NUM_TEN)
+        {
+            minute = "0" + std::to_string(min);
+        }
+        else
+        {
+            minute = std::to_string(min);
+        }
+        if (sec >= 0 && sec < NUM_TEN)
+        {
+            seconds = "0" + std::to_string(sec);
+        }
+        else
+        {
+            seconds = std::to_string(sec);
+        }
+
+#if 0
+        if (hour == 0)
+        {
+            recordDuration = minute + ":" + seconds;
+        }
+        else
+        {
+            recordDuration = hours + ":" + minute + ":" + seconds;
+        }
+#endif
+        recordDuration = hours + ":" + minute + ":" + seconds;
+        return (char *)recordDuration.c_str();
+    }
+
+    // 在这里添加定时器到期后的处理逻辑
+    static void Recording_TimerCallbackProc(int data)
+    {
+        g_RecordingTimes++;
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "g_RecordingTimes:%d\n", g_RecordingTimes);
+
+        if (RecordGoingView::GetInstance() == nullptr)
+        {
+            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "[Recording_TimerCallbackProc] GetInstance is NULL");
+            return;
+        }
+
+        if (RecordGoingView::GetInstance()->GetRecordingDurationLabel() == nullptr)
+        {
+            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "[Recording_TimerCallbackProc] GetInstance()->GetRecordingDurationLabel is NULL");
+            return;
+        }
+
+        if (RecordingModel::GetInstance()->DisplayRecordDuration() == nullptr)
+        {
+            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "[Recording_TimerCallbackProc] DisplayRecordDuration is NULL");
+            return;
+        }
+
+        RecordGoingView::GetInstance()->GetRecordingDurationLabel()->SetText(RecordingModel::GetInstance()->DisplayRecordDuration());
+    }
+
+    // 回调函数
+    static void Recording_TimerCallback(int data)
+    {
+        GraphicService::GetInstance()->PostGraphicEvent(std::bind(Recording_TimerCallbackProc, data));
+    }
+
+    /// @brief 开启定时器函数
+    /// @param interval 时间间隔,单位ms
+    void RecordingModel::Recording_StartTimer(uint32_t interval)
+    {
+        /* -------------------------- 1.检查定时器是否已经存在,如果存在则返回 ------------------------- */
+        if (g_timer != NULL)
+        {
+            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RecordingModel::Recording_StartTimer Timer already exists!!");
+            return;
+        }
+
+        /* --------------------------------- 2.创建定时器 -------------------------------- */
+        g_timer = osTimerNew((osTimerFunc_t)Recording_TimerCallback, osTimerPeriodic, NULL, NULL);
+        // 检查定时器创建是否成功
+        if (g_timer == NULL)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordingModel::Recording_StartTimer Timer create failed!!");
+            return;
+        }
+
+        /* ----------------------------- 3.将毫秒转换为系统时钟节拍数 ---------------------------- */
+        int tick = osMs2Tick(interval);
+
+        /* --------------------------------- 4.启动定时器 -------------------------------- */
+        osStatus_t ret = osTimerStart(g_timer, tick);
+        /* -------------------- 5.检查定时器启动是否成功,如果启动失败,删除定时器并置为 NULL -------------------- */
+        if (ret != osOK)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ScanTimerCallback osTimerDelete failed, ret = %lx!!", ret);
+            if (g_timer != NULL)
+            {
+                ret = osTimerDelete(g_timer);
+                return;
+            }
+            g_timer = NULL;
+        }
+        return;
+    }
+
+    // 停止定时器函数
+    void RecordingModel::Recording_StopTimer(void)
+    {
+        // 检查定时器是否存在
+        if (g_timer != NULL)
+        {
+            // 删除定时器并置为 NULL
+            osStatus_t ret = osTimerDelete(g_timer);
+            if (ret != osOK)
+            {
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ScanTimerCallback osTimerDelete failed, ret = %lx!!", ret);
+            }
+            g_timer = NULL;
+            g_RecordingTimes = 0;
+        }
+    }
+
 }
\ No newline at end of file
index 8b86d99..46066e5 100644 (file)
@@ -167,6 +167,8 @@ namespace OHOS
             AudioCaptureSample(6, (const char **)args);\r
             /* --------------------------------- 跳转到录音界面 -------------------------------- */\r
             p_RecordingView->ChangeView(RECORDINGVIEW, RECORDGOINGVIEW);\r
+            RecordingModel::GetInstance()->Recording_StartTimer(1000);\r
+            RecordGoingView::GetInstance()->GetRecordingDurationLabel()->SetText("00:00:00");\r
             /* ---------------------------------- 释放内存 ---------------------------------- */\r
             for (int i = 0; i < 6; ++i)\r
             {\r
@@ -182,6 +184,7 @@ namespace OHOS
             uint8_t *argv = (uint8_t *)"stop";\r
             AudioCaptureSample(argc, (const char **)&argv);\r
             p_RecordingView->ChangeView(RECORDGOINGVIEW, RECORDINGVIEW);\r
+            RecordingModel::GetInstance()->Recording_StopTimer();\r
             return true;\r
         }\r
         return true;\r
index 4a63230..2ff6483 100644 (file)
@@ -220,6 +220,10 @@ namespace OHOS
             RecordingViewScrollView[thisView] = p_RecordListView->RecordListViewInit();\r
             RootView::GetInstance()->Add(RecordingViewScrollView[thisView]);\r
         }\r
+        else\r
+        {\r
+            RecordListView::GetInstance()->RefreshRecordingListData();\r
+        }\r
         return true;\r
     }\r
 \r