From: luozheng <1732036298@qq.com> Date: Tue, 5 Mar 2024 07:28:38 +0000 (+0800) Subject: 正在录音视图(subView),添加计时功能,并实现录音完,切换到录音列表视图同步刷新刚刚录的音 X-Git-Tag: release_V1.2.70_20241020_135402~1274^2~30 X-Git-Url: http://git.snicker.com.cn/Users/apple/Desktop/static/gitweb.css?a=commitdiff_plain;h=dc70e4897da1409e8ea2d1284c906a011f980f12;p=mirrors%2FFD230.git 正在录音视图(subView),添加计时功能,并实现录音完,切换到录音列表视图同步刷新刚刚录的音 --- diff --git a/code/sdk/application/wearable/fd230app/nativeui/applist/src/ApplistPresenter.cpp b/code/sdk/application/wearable/fd230app/nativeui/applist/src/ApplistPresenter.cpp index ffb4de61a..e4f3f2796 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/applist/src/ApplistPresenter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/applist/src/ApplistPresenter.cpp @@ -79,7 +79,7 @@ namespace OHOS UNUSED(event); AppItemView *itemView = static_cast(&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); diff --git a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordGoingView.h b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordGoingView.h index abfc4a7fd..9dde35577 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordGoingView.h +++ b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordGoingView.h @@ -31,7 +31,7 @@ namespace OHOS static RecordGoingView *GetInstance(void); UIScrollView *RecordGoingViewInit(void); void ClearAll(void); - + UILabel *GetRecordingDurationLabel(void); private: bool SetUpScrollView(void); UILabel *SetUpLabel(LabelConfig &labelconfig); // 设置标签 @@ -45,7 +45,7 @@ namespace OHOS UIImageView *RECORD_GOING_IMAGE{nullptr}; UIButton *RECORD_GOING_BUTTON{nullptr}; - + bool initViewStatus{false}; }; } // namespace OHOS diff --git a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordListView.h b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordListView.h index 610055b3b..179b5e5ff 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordListView.h +++ b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordListView.h @@ -35,7 +35,7 @@ namespace OHOS UIScrollView *RecordListViewInit(void); void ClearAll(void); void IsVisibleLabel(bool value); - + void RefreshRecordingListData(void); private: bool SetUpScrollView(void); UILabel *SetUpLabel(LabelConfig &labelconfig); // 设置标签 diff --git a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingListAdapter.h b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingListAdapter.h index 923e3c064..adf226a46 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingListAdapter.h +++ b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingListAdapter.h @@ -30,6 +30,7 @@ namespace OHOS */ void SetData(std::list &data); + void ClearData(void); /* * * @brief Obtains the data size of the UILabel adapter. * diff --git a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingModel.h b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingModel.h index 4d95a1db0..ebf3053b0 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingModel.h +++ b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingModel.h @@ -70,6 +70,11 @@ namespace OHOS std::string GetCleanFileName(const std::string &filePath); void SearchFiles(const std::string &directory, std::vector &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; // 录音时长 }; } diff --git a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingPresenter.h b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingPresenter.h index 05a615496..c80926119 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingPresenter.h +++ b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingPresenter.h @@ -16,6 +16,7 @@ namespace OHOS MAX_RECORDINGVIEWID, } RECORDINGVIEWID; static const char *RECORD_GOING_BUTTON_VIEWID = "RECORD_GOING_BUTTON"; // 点击按钮后停止录音 + static const char *RECORD_BUTTON = "RecordButton"; //录音按钮 class RecordListViewOnClickListener : public UIView::OnClickListener { public: diff --git a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingView.h b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingView.h index eb7fb84ee..13a12e14d 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingView.h +++ b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingView.h @@ -13,9 +13,11 @@ #include "record/include/RecordListView.h" #include "record/include/RecordGoingView.h" + +#define NUM_MAX_RECORDINGVIEWSCROLLVIEW 10 namespace OHOS { - static const char *RECORD_BUTTON = "RecordButton"; //录音按钮 + class RecordingPresenter; class RecordingView : public View { @@ -34,9 +36,9 @@ namespace OHOS virtual ~RecordingView(); static RecordingView *GetInstance(void); - bool ChangeRecordingView(int16_t thisView); // 切换到“录音”视图 - bool ChangeRecordListView(int16_t thisView);//切换到"录音列表"视图 - bool ChangeRecordGoingView(int16_t thisView);//切换到“正在录音”视图 + bool ChangeRecordingView(int16_t thisView); // 切换到“录音”视图 + bool ChangeRecordListView(int16_t thisView); // 切换到"录音列表"视图 + bool ChangeRecordGoingView(int16_t thisView); // 切换到“正在录音”视图 bool ClickFunc(int16_t interface); void ChangeView(int16_t hideView, int16_t showView); UIScrollView *GetRecordingViewScrollView(int i); @@ -49,14 +51,15 @@ namespace OHOS private: RecordListView *p_RecordListView{nullptr}; - RecordGoingView*p_RecordGoingView{nullptr}; + RecordGoingView *p_RecordGoingView{nullptr}; + private: void RecordingViewInit(void); void ClearAll(void); bool SetUpScrollView(void); bool SetTheTitle(void); UIButton *SetTheButton(ButtonConfig &buttonconfig, UIView::OnClickListener *clickListener); - UIScrollView *RecordingViewScrollView[3]{nullptr}; + UIScrollView *RecordingViewScrollView[NUM_MAX_RECORDINGVIEWSCROLLVIEW]{nullptr}; UILabel *RecordingViewTitleLabel{nullptr}; UIButton *RecordingViewButton{nullptr}; UILabel *AttentionLabel{nullptr}; diff --git a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordGoingView.cpp b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordGoingView.cpp index 33b40d8bb..ac2ebfc5f 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordGoingView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordGoingView.cpp @@ -123,7 +123,7 @@ namespace OHOS .height = 77, .fontSize = 65, .lineHeigh = 76, - .text = "00:00:12", + .text = "00:00:00", .lineBreakMode = UILabel::LINE_BREAK_WRAP, }; Timer = SetUpLabel(labelconfig); @@ -170,6 +170,16 @@ namespace OHOS return true; } + UILabel *RecordGoingView::GetRecordingDurationLabel(void) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RecordGoingView::GetRecordingDurationLabel"); + if(Timer == nullptr) + { + return nullptr; + } + return Timer; + } + UIScrollView *RecordGoingView::RecordGoingViewInit() { if (!SetUpScrollView()) diff --git a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordListView.cpp b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordListView.cpp index c8a094fb6..9debc9288 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordListView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordListView.cpp @@ -174,6 +174,16 @@ namespace OHOS } } + // 每次录音完将数据刷新显示到list + void RecordListView::RefreshRecordingListData(void) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordListView::RefreshRecordingListData"); + RecordingModel::GetInstance()->InitRecordingListItem(); + p_RecordingListAdapter->ClearData(); + InitRecordingInfo(); + recordList->RefreshList(); + } + // 设置提醒标签是否可见 void RecordListView::IsVisibleLabel(bool value) { diff --git a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingListAdapter.cpp b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingListAdapter.cpp index 05841ae8c..a558f1217 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingListAdapter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingListAdapter.cpp @@ -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(inView); diff --git a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingModel.cpp index 3d1f560e1..8189ea534 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingModel.cpp @@ -6,7 +6,9 @@ #include "audio_manager.h" #include #include "../../../../../../drivers/peripheral/utils/custom_utils.h" - +#include +#include +#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 diff --git a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingPresenter.cpp b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingPresenter.cpp index 8b86d99aa..46066e51e 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingPresenter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingPresenter.cpp @@ -167,6 +167,8 @@ namespace OHOS AudioCaptureSample(6, (const char **)args); /* --------------------------------- 跳转到录音界面 -------------------------------- */ p_RecordingView->ChangeView(RECORDINGVIEW, RECORDGOINGVIEW); + RecordingModel::GetInstance()->Recording_StartTimer(1000); + RecordGoingView::GetInstance()->GetRecordingDurationLabel()->SetText("00:00:00"); /* ---------------------------------- 释放内存 ---------------------------------- */ for (int i = 0; i < 6; ++i) { @@ -182,6 +184,7 @@ namespace OHOS uint8_t *argv = (uint8_t *)"stop"; AudioCaptureSample(argc, (const char **)&argv); p_RecordingView->ChangeView(RECORDGOINGVIEW, RECORDINGVIEW); + RecordingModel::GetInstance()->Recording_StopTimer(); return true; } return true; diff --git a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingView.cpp b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingView.cpp index 4a63230f3..2ff648350 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingView.cpp @@ -220,6 +220,10 @@ namespace OHOS RecordingViewScrollView[thisView] = p_RecordListView->RecordListViewInit(); RootView::GetInstance()->Add(RecordingViewScrollView[thisView]); } + else + { + RecordListView::GetInstance()->RefreshRecordingListData(); + } return true; }