From: luozheng <1732036298@qq.com> Date: Thu, 7 Mar 2024 10:27:31 +0000 (+0800) Subject: 录音视图实现删除录音功能 X-Git-Tag: release_V1.2.70_20241020_135402~1274^2~19^2~8 X-Git-Url: http://git.snicker.com.cn/css/html/static/gitweb.css?a=commitdiff_plain;h=f48573d0172e9666829f4f3de25592bcb11c28f9;p=mirrors%2FFD230.git 录音视图实现删除录音功能 --- 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 ebf3053b0..aeafcbf19 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingModel.h +++ b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingModel.h @@ -65,11 +65,15 @@ namespace OHOS int RecordingPlayerStat(); /* -------------------------------------------------------------------------- */ void InitRecordingListItem(); - const RecordingListItem *GetRecordinglistItems() const; - uint8_t GetRecordingListNum() const; + std::vector &GetRecordinglistItems(); + int &GetRecordingListNum(); std::string GetCleanFileName(const std::string &filePath); void SearchFiles(const std::string &directory, std::vector &fileNames, int &mp3FileCount); - + void KeepClickRecordingIndex(int index_); + int GetClickRecordingIndex(void); + std::vector &GetFileNames(void); + void KeepTotalRecordDuration(std::string time); + const char *GetTotalRecordDuration(void); /* ----------------------------------- 定时器 ---------------------------------- */ void Recording_StartTimer(uint32_t interval); void Recording_StopTimer(void); @@ -94,13 +98,14 @@ namespace OHOS std::map Header; std::shared_ptr Resource{nullptr}; /* -------------------------------------------------------------------------- */ - std::vector fileNames; // 文件名 - int RecordingMp3FileCount = 0; // 录音MP3数量 - const std::string directory = "/user/res/record/recording/"; - RecordingListItem *Recordings = nullptr; - - /* ----------------------------------- 定时器 ---------------------------------- */ - std::string recordDuration; // 录音时长 + std::vector fileNames; // 文件名 + int RecordingMp3FileCount = 0; // 录音MP3数量 + const std::string directory = "/user/res/record/recording/"; // 录音目录 + std::vector Recordings; // 存放录音信息的动态数组 + std::string recordDuration; // 录音时长 + int index; // 保存被点击录音的下标 + std::string yyyymmhhh_date; // 保存年月日时间 + std::string TotalRecordDuration; // 保存录音的总时长 }; } diff --git a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingPlayerView.h b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingPlayerView.h index 44eab9d58..9524bf282 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingPlayerView.h +++ b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingPlayerView.h @@ -52,7 +52,8 @@ namespace OHOS static RecordingPlayerView *GetInstance(void); UIScrollView *RecordingPlayerViewInit(void); void ClearAll(void); - + UILabelExt * GetRecordingFileNameLabel(void); + UILabelExt *GetRecordingTimeAndDurationLabel(void); private: UILabelExt *SetUpLabel(LabelConfig &labelconfig); // 设置标签 UIButton *SetTheButton(ButtonConfig &buttonconfig, UIView::OnClickListener *clickListener); 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 afcd9c760..011de866f 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordListView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordListView.cpp @@ -47,7 +47,7 @@ namespace OHOS delete clicklister; clicklister = nullptr; } - + delete recordList; recordList = nullptr; @@ -174,14 +174,16 @@ namespace OHOS // 把录音信息数据拿到存储到list中 void RecordListView::InitRecordingInfo(void) { - const RecordingListItem *itemInfo = RecordingModel::GetInstance()->GetRecordinglistItems(); + std::vector &item = RecordingModel::GetInstance()->GetRecordinglistItems(); for (int i = 0; i < RecordingModel::GetInstance()->GetRecordingListNum(); i++) { - AddRecordingToList(itemInfo[i]); + AddRecordingToList(item[i]); } } - // 每次录音完将数据刷新显示到list + /* + * @brief 每次录音完将数据刷新显示到list + */ void RecordListView::RefreshRecordingListData(void) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordListView::RefreshRecordingListData"); @@ -189,6 +191,15 @@ namespace OHOS p_RecordingListAdapter->ClearData(); InitRecordingInfo(); recordList->RefreshList(); + if (RecordingModel::GetInstance()->GetRecordingListNum() == 0) + { + IsVisibleLabel(true); + } + else + { + IsVisibleLabel(false); + } + return; } // 设置提醒标签是否可见 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 0ba8acd97..209c9ed7f 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingModel.cpp @@ -5,7 +5,7 @@ #include "bts_a2dp_source.h" #include "audio_manager.h" #include -#include "../../../../../../drivers/peripheral/utils/custom_utils.h" +#include "custom_utils.h" #include #include #include "record/include/RecordGoingView.h" @@ -135,7 +135,7 @@ namespace OHOS } /* - * @brief 获取纯净文件名 + * @brief 获取纯净.mp3后缀文件名 * @param 文件路径 * @return 返回纯净文件名 */ @@ -193,17 +193,21 @@ namespace OHOS } /* - * @brief 初始化录音列表数据 - */ + * @brief 初始化录音列表数据 + */ void RecordingModel::InitRecordingListItem(void) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordingModel::InitRecordingListItem"); - char *date = uapi_get_local_yyyymmdd_date(); // 获取日期 + yyyymmhhh_date = uapi_get_local_yyyymmdd_date(); // 获取日期 RecordingMp3FileCount = 0; if (!fileNames.empty()) { fileNames.clear(); } + if (!Recordings.empty()) + { + Recordings.clear(); + } SearchFiles(directory, fileNames, RecordingMp3FileCount); if (RecordingMp3FileCount != 0) { @@ -212,41 +216,66 @@ namespace OHOS { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "File Name::%s\n", filename.c_str()); } - Recordings = new RecordingListItem[RecordingMp3FileCount]; - if (Recordings == NULL) + // 对 Recordings 数组进行操作 + for (int i = 0; i < RecordingMp3FileCount; i++) { - // 内存分配失败处理 - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Recordings new fail!\n"); - } - else - { - // 对 Recordings 数组进行操作 - for (int i = 0; i < RecordingMp3FileCount; i++) - { - Recordings[i] = {i, fileNames[i].c_str(), "09:51", date}; - } - // 记得在不需要使用 Recordings 后释放内存 - // free(Recordings); + Recordings.push_back({i, fileNames[i].c_str(), "09:51", yyyymmhhh_date.c_str()}); } } } - /// @brief 获取存放录音的基本信息数组 - /// @return - const RecordingListItem *RecordingModel::GetRecordinglistItems() const + /* + * @brief 获取存放录音的基本信息动态数组 + * @return 返回该动态数组 + */ + std::vector &RecordingModel::GetRecordinglistItems(void) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordingModel::GetRecordinglistItems"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RecordingModel::GetRecordinglistItems"); return Recordings; } - /// @brief 获取录音数量 - /// @return - uint8_t RecordingModel::GetRecordingListNum() const + /* + * @brief 获取录音数量 + */ + int &RecordingModel::GetRecordingListNum(void) { WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RecordingModel::GetRecordingListNum"); return RecordingMp3FileCount; } + /* + * @brief 保存被点击录音下标 + */ + void RecordingModel::KeepClickRecordingIndex(int index_) + { + index = index_; + } + + /* + * @brief 获取被点击录音下标 + */ + int RecordingModel::GetClickRecordingIndex(void) + { + return index; + } + + /* + *@brief 获取保存文件名的动态数组 + */ + std::vector &RecordingModel::GetFileNames(void) + { + return fileNames; + } + + void RecordingModel::KeepTotalRecordDuration(std::string time) + { + TotalRecordDuration = time; + } + + const char *RecordingModel::GetTotalRecordDuration(void) + { + return TotalRecordDuration.c_str(); + } /* ----------------------------------- 定时器 ---------------------------------- */ // 声明定时器变量 osTimerId_t g_timer = NULL; @@ -258,7 +287,9 @@ namespace OHOS static constexpr int16 NUM_TIME_HOUR = 3600; static constexpr int32 NUM_TIME_DAY = 86400; - /// @brief 显示录音时长 + /* + * @brief 显示录音时长 + */ char *RecordingModel::DisplayRecordDuration() { recordDuration.erase(); @@ -311,7 +342,9 @@ namespace OHOS return (char *)recordDuration.c_str(); } - // 在这里添加定时器到期后的处理逻辑 + /* + * @brief 在这里添加定时器到期后的处理逻辑 + */ static void Recording_TimerCallbackProc(int data) { g_RecordingTimes++; @@ -338,14 +371,18 @@ namespace OHOS RecordGoingView::GetInstance()->GetRecordingDurationLabel()->SetText(RecordingModel::GetInstance()->DisplayRecordDuration()); } - // 回调函数 + /* + * @brief 录音回调函数 + */ static void Recording_TimerCallback(int data) { GraphicService::GetInstance()->PostGraphicEvent(std::bind(Recording_TimerCallbackProc, data)); } - /// @brief 开启定时器函数 - /// @param interval 时间间隔,单位ms + /* + * @brief 开启定时器函数 + * @param interval 时间间隔,单位ms + */ void RecordingModel::Recording_StartTimer(uint32_t interval) { /* -------------------------- 1.检查定时器是否已经存在,如果存在则返回 ------------------------- */ @@ -383,7 +420,9 @@ namespace OHOS return; } - // 停止定时器函数 + /* + * @brief 停止定时器 + */ void RecordingModel::Recording_StopTimer(void) { // 检查定时器是否存在 diff --git a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingPlayerView.cpp b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingPlayerView.cpp index 21a0c897b..0ab7dd729 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingPlayerView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingPlayerView.cpp @@ -317,6 +317,22 @@ namespace OHOS return true; } + /* + * @brief 获取录音文件名标签 + */ + UILabelExt *RecordingPlayerView::GetRecordingFileNameLabel(void) + { + return RecordingFileName; + } + + /* + * @brief 获取录音时间和时长标签 + */ + UILabelExt *RecordingPlayerView::GetRecordingTimeAndDurationLabel(void) + { + return RecordingTimeAndDuration; + } + UIScrollView *RecordingPlayerView::RecordingPlayerViewInit() { if (!SetUpScrollView()) 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 84160cc1c..b75c190c3 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingPresenter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingPresenter.cpp @@ -127,7 +127,7 @@ namespace OHOS { if (std::remove(filepath) == 0) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Deleted file: %s", filepath); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Deleted file: %s successed!", filepath); } else { @@ -204,8 +204,15 @@ namespace OHOS } else if (strcmp(view.GetViewId(), RECORD_DELETE_ON_BUTTON_VIEWID) == 0) { - deleteFile("/user/res/record/recording/record_test.mp3"); - p_RecordingView->ChangeView(RECORDINGDELETEVIEW, RECORDINGPLAYERVIEW); + RecordingModel *p_RecordingModel = RecordingModel::GetInstance(); + std::string fileName = "/user/res/record/recording/" + p_RecordingModel->GetFileNames()[p_RecordingModel->GetClickRecordingIndex()] + ".mp3"; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fileName:%s\n", fileName.c_str()); + deleteFile(fileName.c_str()); + if (--p_RecordingModel->GetRecordingListNum() < 0) + { + p_RecordingModel->GetRecordingListNum() = 0; + } + p_RecordingView->ChangeView(RECORDINGDELETEVIEW, RECORDINGVIEW); return true; } else if (strcmp(view.GetViewId(), RECORD_DELETE_OFF_BUTTON_VIEWID) == 0) @@ -252,13 +259,20 @@ namespace OHOS UNUSED(event); RecordingListitemView *p_RecordingListitemView = static_cast(&view); int index = p_RecordingListitemView->GetRecordingListIndex(); - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RecordListViewOnClickListener::OnClick index:%d\n", index); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordListViewOnClickListener::OnClick index:%d\n", index); if (index >= RecordingModel::GetInstance()->GetRecordingListNum()) { WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RecordListViewOnClickListener::OnClick is no index"); return false; } - + // 保存被点击录音下标 + RecordingModel *p_RecordingModel = RecordingModel::GetInstance(); + p_RecordingModel->KeepClickRecordingIndex(index); + if (p_RecordingModel == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordingModel GetInstance fail"); + return false; + } RecordingView *p_RecordingView = RecordingView::GetInstance(); if (p_RecordingView == nullptr) { @@ -266,6 +280,8 @@ namespace OHOS return false; } p_RecordingView->ChangeView(RECORDINGLISTVIEW, RECORDINGPLAYERVIEW); + + 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 5314e2116..e70eb186e 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingView.cpp @@ -2,6 +2,7 @@ #include "record/include/RecordingPresenter.h" #include "UiConfig.h" #include "wearable_log.h" +#include "../include/RecordingModel.h" namespace OHOS { static ImageAnimatorInfo RecordingViewImageAnimatorInfo[2] = { @@ -274,6 +275,8 @@ namespace OHOS RecordingViewScrollView[thisView] = p_RecordingPlayerView->RecordingPlayerViewInit(); RootView::GetInstance()->Add(RecordingViewScrollView[thisView]); } + // 设置“录音播放器”【录音文件名标签】的"文本内容"为被点击的"录音"的录音名 + p_RecordingPlayerView->GetRecordingFileNameLabel()->SetText(RecordingModel::GetInstance()->GetFileNames()[RecordingModel::GetInstance()->GetClickRecordingIndex()].c_str()); return true; }