From 3ed7ec437b19c09d0acbc224b30e7950099e27fd Mon Sep 17 00:00:00 2001 From: luozheng <1732036298@qq.com> Date: Wed, 28 Aug 2024 15:50:53 +0800 Subject: [PATCH] =?utf8?q?=E5=BD=95=E9=9F=B3=E5=AE=8C=E6=88=90=E8=BF=9B?= =?utf8?q?=E5=BA=A6=E6=9D=A1=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../src/AlarmClockTimerConfig.cpp | 5 + .../nativeui/record/include/RecordingModel.h | 11 +- .../record/include/RecordingPlayerView.h | 8 + .../record/include/RecordingPresenter.h | 8 +- .../nativeui/record/include/RecordingView.h | 4 +- .../nativeui/record/src/RecordingModel.cpp | 340 +++++++++++------- .../record/src/RecordingPlayerView.cpp | 35 +- .../record/src/RecordingPresenter.cpp | 83 ++++- .../nativeui/record/src/RecordingView.cpp | 10 + .../nativeui/uiservice/include/ui_service.h | 3 +- .../nativeui/uiservice/src/ui_service.cpp | 10 + 11 files changed, 359 insertions(+), 158 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/alarmclocktimerconfig/src/AlarmClockTimerConfig.cpp b/code/sdk/application/wearable/fd230app/nativeui/alarmclocktimerconfig/src/AlarmClockTimerConfig.cpp index f1c8935bf..34aba5fb8 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/alarmclocktimerconfig/src/AlarmClockTimerConfig.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/alarmclocktimerconfig/src/AlarmClockTimerConfig.cpp @@ -32,6 +32,7 @@ #include "custom_runtime.h" #include "uiservice/include/ui_service.h" #include "power_display_service.h" +#include "record/include/RecordingModel.h" #ifdef __cplusplus extern "C" @@ -518,6 +519,10 @@ extern "C" OHOS::HeartModel::GetInstance()->Heart_Control(OHOS::HeartState::Start, OHOS::HeartCheck::PASSIVITY); } } + if(OHOS::RecordingModel::GetInstance()->GetRecordingPlayerViewCreate()) + { + SendMsgToUIService(UI_UPDATE_RECORDPROCESS, 0, nullptr, 0); + } } static int32_t AlarmCreate(AlarmCtrl *alarmCtrl, AlarmCallBack callback) 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 4a33ff169..3a2e5aaeb 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingModel.h +++ b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingModel.h @@ -58,14 +58,12 @@ namespace OHOS int GetCircleProgress(void); void AudioInit(void); void ConvertMillisecondToTime(int32_t milliseconds); - int RecordingPlayerInit(int index); + int RecordingListOnClickPlay(void); int RecordingPlayerStat(void); int GetRecordingPlayerState(void); int RecordingPlayerStop(void); void PreviousPlayout(void); void NextPlayout(void); - int ResetSourceAndPrepare(void); - int ResetPlayerModuleStatus(void); void ClearRecordingPlayer(void); /* -------------------------------------------------------------------------- */ void InitRecordingListItem(); @@ -80,7 +78,6 @@ namespace OHOS std::vector &GetFileNames(void); void SetTotalRecordDuration(std::string time); std::string GetTotalRecordDuration(void) const; - RecordingInfo *GetRecordingInfo(); void SetRecordingInfo(RecordingInfo &info, int index); void UpdateRecordingInfo(int index); void SetRecordFileName(std::string name); @@ -95,10 +92,15 @@ namespace OHOS void Recording_StartTimer(uint32_t interval = 1000); void Recording_StopTimer(void); char *DisplayRecordDuration(void); + bool CreateRecordPlayDirectory(const char *path); const char *GetRecordDirectory() const; static std::string recordTempDuration; // 录音时长 int RecordDataWriteToFile(); int RecordDataReadToFile(); + void GetDuration(int64_t &duration); + void UpdateRecordProcess(); + bool GetRecordingPlayerViewCreate(); + void SetRecordingPlayerViewCreate(bool state); private: RecordingModel(); @@ -136,6 +138,7 @@ namespace OHOS std::string RecordingTimeAndDuration; // 录音日期和录音时长 RecordingInfo recordingInfo[MAX_RECORDING_NUM] = {0}; /* -------------------------------------------------------------------------- */ + bool RecordingPlayerViewIsCreate{false}; }; } 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 60abb9b39..bd2478c04 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingPlayerView.h +++ b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingPlayerView.h @@ -1,3 +1,10 @@ +/* + * @Author: luozheng 1732036298@qq.com + * @Date: 2024-08-05 15:20:03 + * @LastEditors: luozheng 1732036298@qq.com + * @LastEditTime: 2024-08-27 19:38:39 + * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingPlayerView.h + */ #ifndef RECORD_PLAY_VIEW_H #define RECORD_PLAY_VIEW_H @@ -60,6 +67,7 @@ namespace OHOS UICheckBox::UICheckBoxState GetPlayerButtonState(void); void ResetComponentStatus(void); UICircleProgress * GetPlayerProgressBar(void); + void SetPlayerProgressBarValue(int value); private: UILabelExt *SetUpLabel(LabelConfig &labelconfig); // 设置标签 UIButton *SetTheButton(ButtonConfig &buttonconfig, UIView::OnClickListener *clickListener); 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 6d20f5285..80b6fe24e 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingPresenter.h +++ b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingPresenter.h @@ -2,7 +2,7 @@ * @Author: luozheng 1732036298@qq.com * @Date: 2024-02-27 09:16:51 * @LastEditors: luozheng 1732036298@qq.com - * @LastEditTime: 2024-08-26 15:23:43 + * @LastEditTime: 2024-08-28 15:44:25 * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingPresenter.h */ @@ -128,6 +128,7 @@ namespace OHOS static RecordingPresenter *GetInstance(); int convertNumericDateTime(const char *numericDateTime, char *formattedDateTime); + void UpdateRecordProcess(); private: // 因为继承了OnClickListener,所以有OnClick属性,至于参数,原函数也这样直接继承; @@ -135,9 +136,14 @@ namespace OHOS bool OnDrag(UIView &view, const DragEvent &event) override; bool OnDragEnd(UIView &view, const DragEvent &event) override; bool OnKeyAct(UIView &view, const KeyEvent &event) override; + void SetCircleProgress(); void StartRecord(void); void StopRecord(void); RecordingModel *p_RecordingModel{nullptr}; + uint8_t state_ = UICheckBox::UICheckBoxState::MAX_STATUS_NUM; + int64_t playerCurrentTime_{0}; + int64_t playerMusicDuration_{0}; + int64_t playerProgressValue_{0}; }; } 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 6324b81ad..27dc2fce2 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingView.h +++ b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingView.h @@ -2,7 +2,7 @@ * @Author: luozheng 1732036298@qq.com * @Date: 2024-03-30 09:14:38 * @LastEditors: luozheng 1732036298@qq.com - * @LastEditTime: 2024-08-24 16:01:13 + * @LastEditTime: 2024-08-28 14:45:22 * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingView.h */ #ifndef RECORD_VIEW_H @@ -65,6 +65,7 @@ namespace OHOS bool ClickFunc(int16_t interface); void ChangeView(int16_t hideView, int16_t showView); int GetCurrentViewType(UIScrollView * view); + UIScrollView *GetRecordingViewScrollView(int index); public: void OnStart() override; @@ -84,7 +85,6 @@ namespace OHOS bool RecordingLabelViewInit(void); bool RecordingButtonViewInit(void); bool RecordingImageViewInit(void); - bool RecordingImageAnimatorViewInit(void); void RecordingViewInit(void); void ClearAll(void); 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 21c74afeb..9897afa39 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingModel.cpp @@ -10,11 +10,16 @@ #include #include #include +#include +#include namespace OHOS { std::string RecordingModel::recordTempDuration = 0; static MutexId g_mutex; - RecordingModel::RecordingModel() {} + RecordingModel::RecordingModel() + { + CreateRecordPlayDirectory(directory.c_str()); + } RecordingModel::~RecordingModel() {} @@ -97,12 +102,18 @@ namespace OHOS audioInitStatus = value; } + void RecordingModel::ClearRecordingPlayer(void) + { + RecordingPlayerStop(); // 录音播放器停止 + ClearActivateAudioInterrupt(); // 清除音频中断 + } + /* * @brief 初始化音频系统并准备播放器控制器和回调 */ void RecordingModel::AudioInit(void) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordingModel::AudioInit"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RecordingModel::AudioInit"); /* ---------- 检查一个成员变量 audioInitStatus 是否已经是 true。如果是,这表示音频已经初始化过了。 --------- */ if (audioInitStatus == true) { @@ -150,95 +161,86 @@ namespace OHOS } } - /* - * @brief 录音播放初始化,包括录音文件名,创建资源对象,录音时间和总时长 - */ - int RecordingModel::RecordingPlayerInit(int index) + // 点击录音列表中录音播放 + int RecordingModel::RecordingListOnClickPlay(void) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordingModel::RecordingPlayerInit"); - int32_t ret; - std::string recordingFileName = fileNames[index]; - if (recordingFileName.c_str() == nullptr) + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordingModel::RecordingListOnClickPlay"); + int ret; + int32_t playerState; + uint8_t state = UICheckBox::UICheckBoxState::MAX_STATUS_NUM; + RecordingPlayerView *p_RecordingPlayerView = RecordingPlayerView::GetInstance(); + if (p_RecordingPlayerView == nullptr) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "No Recording files!!"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "p_RecordingPlayerView null!"); return OHOS_FAILURE; - }; - SetRecordingPlayerUri(directory + recordingFileName + ".mp3"); - - int state = GetRecordingPlayerState(); - if (state != PlayerStates::PLAYER_STATE_ERROR) // 每次setSource前回到idle状态 - { - ret = PlayerCtr->Reset(); - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Reset, ret:%d", ret); } + SetRecordingPlayerUri(directory + GetCurrentPlayRecordingName() + ".mp3"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "uri: %s.", GetRecordingPlayerUri().c_str()); - /* ------------------------- 创建资源对象Resource并进行资源申请。 ------------------------- */ - Resource = std::make_shared(GetRecordingPlayerUri()); - if (Resource.get() == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "source request failed!"); - return OHOS_FAILURE; - } - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Player Uri %s", GetRecordingPlayerUri().c_str()); - /* ----------------------- 设置播放器的数据源为Resource,并进行准备操作。 ---------------------- */ - PlayerCtr->SetSource(*Resource); - PlayerCtr->Prepare(); - /* ------------------------------ 获取播放内容的时长。 ------------------------------ */ - ret = PlayerCtr->GetDuration(DurationTime); - if (ret != -1) + state = p_RecordingPlayerView->GetPlayerButtonState(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "state %d", state); + if (state == UICheckBox::UICheckBoxState::UNSELECTED) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GetDuration:%ld, ret:%d", DurationTime, ret); + p_RecordingPlayerView->SetPlayerButtonState(UICheckBox::UICheckBoxState::SELECTED); + SetRecordingPlayButtonState(true); + /* start play */ + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "play"); + playerState = GetRecordingPlayerState(); + if (playerState == PlayerStates::PLAYER_PAUSED || playerState == PlayerStates::PLAYER_PLAYBACK_COMPLETE) + { + ret = PlayerCtr->Reset(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Reset, ret:%d", ret); + } + RecordingPlayerStat(); // 播放 } - else + else if (state == UICheckBox::UICheckBoxState::SELECTED) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Get Duration time fail!"); - return OHOS_FAILURE; + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "List to play"); + playerState = GetRecordingPlayerState(); + if (playerState == PlayerStates::PLAYER_STARTED) + { + ret = PlayerCtr->Stop(); // 停止 + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Stop, ret:%d", ret); + ret = PlayerCtr->Reset(); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Reset, ret:%d", ret); + } + RecordingPlayerStat(); } - ConvertMillisecondToTime(DurationTime); return OHOS_SUCCESS; } - /* - * @brief 退出录音播放界面后清除播放器资源 - */ - void RecordingModel::ClearRecordingPlayer(void) - { - if (GetPlayerCtr() != nullptr) - { - GetPlayerCtr()->Reset(); - GetPlayerCtr()->Release(); - SetAudioInitStatus(false); - ClearActivateAudioInterrupt(); - GetPlayerCtr().reset(); - } - } - - /* - * @brief 播放器状态的控制 - */ + // 录音播放 int RecordingModel::RecordingPlayerStat() { - int ret; - /* ------------------------------ 设置播放器的回调函数。 ----------------------------- */ - if (GetRecordingPlayerState() != PlayerStates::PLAYER_PREPARED) // 如果录音播放器状态为Prepared状态无需重置回到IDLE状态和setsource,Prepare + int32_t ret; + + // 设置回调函数 + PlayerCtr->SetPlayerCallback(PlayerCallback); + // 申请资源 + Resource = std::make_shared(Uri, Header); + if (Resource.get() == nullptr) { - ResetPlayerModuleStatus(); - ResetSourceAndPrepare(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Resource new fail"); + return OHOS_FAILURE; } - PlayerCtr->SetPlayerCallback(PlayerCallback); - /* ----------------------------- 如果未激活音频中断处理: ----------------------------- */ + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Player uri %s.", Uri.c_str()); + PlayerCtr->SetSource(*Resource); + PlayerCtr->Prepare(); + ret = PlayerCtr->GetDuration(DurationTime); + ConvertMillisecondToTime(DurationTime); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[%s,%d] GetDuration:%lld,%s, ret:%d", __func__, __LINE__, DurationTime, TotalRecordDuration.c_str(), ret); + /* -------------------------------------------------------------------------- */ if (!IsActivateAudioInterrupt) { - /* -------------------------------- 申请音频会话ID。 -------------------------------- */ + // 申请焦点 AudioManager &amIntance = AudioManager::GetInstance(); SessionId = amIntance.MakeSessionId(); if (SessionId == AUDIO_SESSION_ID_NONE) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SessionId invalid"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "session invalid"); return OHOS_FAILURE; } - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MakeSessionId %d", SessionId); - /* ----------------------------- 创建并设置播放器中断监听器。 ----------------------------- */ + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MakeSessionId %d", SessionId); p_RecordingPlayerInterruptListener = std::make_shared(); if (p_RecordingPlayerInterruptListener.get() == nullptr) { @@ -246,32 +248,34 @@ namespace OHOS return OHOS_FAILURE; } p_RecordingPlayerInterruptListener->SetOwner(PlayerCtr); - /* - struct AudioInterrupt - { - AudioStreamType streamType; 标识不同种类的音频流 - AudioSession sessionID; 唯一标识不同的音频会话 - std::shared_ptr interruptListener;理音频中断事件的监听器 - }; - */ interrupt = {AUDIO_STREAM_MUSIC, SessionId, p_RecordingPlayerInterruptListener}; - /* --------------------------- 激活音频中断处理,设置超时时间为5秒 --------------------------- */ // 激活音频中断 5s超时 if (amIntance.ActivateAudioInterrupt(interrupt) == INTERRUPT_FAILED) { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "ActivateAudioInterrupt faild"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ActivateAudioInterrupt faild"); return OHOS_FAILURE; } IsActivateAudioInterrupt = true; } - /* ----------------------------- 设置播放器的音频会话ID。 ---------------------------- */ PlayerCtr->SetAudioSessionId(SessionId); - /* ---------------------------- 开始播放内容,如果播放失败: ---------------------------- */ + // 播放 ret = PlayerCtr->Play(); if (ret != 0) { PlayerCtr->Stop(); // 暂停 PlayerCtr->Reset(); // 初始化 + RecordingPlayerView *p_RecordingPlayerView = RecordingPlayerView::GetInstance(); + if (p_RecordingPlayerView != nullptr) + { + p_RecordingPlayerView->SetPlayerButtonState(UICheckBox::UNSELECTED); + SetCircleProgress(100); // 100:进度100% + p_RecordingPlayerView->SetPlayerProgressBarValue(CircleProgress); + } + else + { + SetRecordingPlayButtonState(false); + SetCircleProgress(100); // 100:进度100% + } AudioManager::GetInstance().DeactivateAudioInterrupt(interrupt); IsActivateAudioInterrupt = false; } @@ -295,39 +299,6 @@ namespace OHOS return state; } - /* - * @brief 重新设置SetSource和Prepare - */ - int RecordingModel::ResetSourceAndPrepare(void) - { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RecordingModel::ResetSourceAndPrepare"); - if (PlayerCtr.get() == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "PlayerCtr.get() is nullptr!!"); - return OHOS_FAILURE; - } - PlayerCtr->SetSource(*Resource); - PlayerCtr->Prepare(); - - return OHOS_SUCCESS; - } - - /* - * @brief 重置播放器到IDLE状态 - */ - int RecordingModel::ResetPlayerModuleStatus(void) - { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RecordingModel::ResetPlayerModuleStatus"); - if (PlayerCtr.get() == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "PlayerCtr.get() is nullptr!!"); - return OHOS_FAILURE; - } - PlayerCtr->Reset(); - - return OHOS_SUCCESS; - } - /* * @brief 录音播放器停止 */ @@ -394,8 +365,18 @@ namespace OHOS { int index = GetClickRecordingIndex(); int num = GetRecordingListNum(); + if (num == 1) + { + return; // 只有一首录音 + } + RecordingPlayerView *p_RecordingPlayerView = RecordingPlayerView::GetInstance(); + if (p_RecordingPlayerView == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "p_RecordingPlayerView null!"); + return; + } - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "PreviousPlayout before index %d.", index); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "NextPlayout before index %d,%s", GetClickRecordingIndex(), GetCurrentPlayRecordingName().c_str()); if ((index > num - 1) || (index < 0)) { WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "currentIndex err"); @@ -403,8 +384,38 @@ namespace OHOS return; } SetClickRecordingIndex(--index < 0 ? num - 1 : index); // 如果从第一首录音设置上一首,那就跳到最后一首 - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "PreviousPlayout after index %d.", GetClickRecordingIndex()); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "NextPlayout after index %d,%s", GetClickRecordingIndex(), GetCurrentPlayRecordingName().c_str()); + SetRecordingPlayerUri(directory + GetCurrentPlayRecordingName() + ".mp3"); SetCircleProgress(0); + int32_t ret; + int32_t playerState; + PlayerCtr->GetPlayerState(playerState); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "playerState Value %d.", playerState); + if ((playerState == PlayerStates::PLAYER_STARTED) || (playerState == PlayerStates::PLAYER_PLAYBACK_COMPLETE)) + { + /* start play */ + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "start play, 160."); + ret = PlayerCtr->Stop(); // 停止 + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Stop, ret:%d", ret); + ret = PlayerCtr->Reset(); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Reset, ret:%d", ret); + + p_RecordingPlayerView->SetPlayerButtonState(UICheckBox::UICheckBoxState::SELECTED); + SetRecordingPlayButtonState(true); + + ret = RecordingPlayerStat(); // 播放 + if (ret != 0) + { + /* stop and exit */ + ret = PlayerCtr->Stop(); // 停止 + ret = PlayerCtr->Reset(); + } + } + else if (playerState == PlayerStates::PLAYER_PAUSED || (playerState == PlayerStates::PLAYER_IDLE)) + { + ret = PlayerCtr->Reset(); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Reset, ret:%d", ret); + } } /* @@ -414,8 +425,18 @@ namespace OHOS { int index = GetClickRecordingIndex(); int num = GetRecordingListNum(); + if (num == 1) + { + return; // 只有一首录音 + } + RecordingPlayerView *p_RecordingPlayerView = RecordingPlayerView::GetInstance(); + if (p_RecordingPlayerView == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "p_RecordingPlayerView null!"); + return; + } - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "NextPlayout before index %d.", index); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "NextPlayout before index %d,%s", GetClickRecordingIndex(), GetCurrentPlayRecordingName().c_str()); if ((index > num - 1) || (index < 0)) { WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "currentIndex err"); @@ -423,8 +444,38 @@ namespace OHOS return; } SetClickRecordingIndex(++index > num - 1 ? 0 : index); // 如果从最后一首录音设置下一首,那就跳到第一首 - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "NextPlayout after index %d.", GetClickRecordingIndex()); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "NextPlayout after index %d,%s", GetClickRecordingIndex(), GetCurrentPlayRecordingName().c_str()); + SetRecordingPlayerUri(directory + GetCurrentPlayRecordingName() + ".mp3"); SetCircleProgress(0); + int32_t ret; + int32_t playerState; + PlayerCtr->GetPlayerState(playerState); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "playerState Value %d.", playerState); + if ((playerState == PlayerStates::PLAYER_STARTED) || (playerState == PlayerStates::PLAYER_PLAYBACK_COMPLETE)) + { + /* start play */ + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "start play, 160."); + ret = PlayerCtr->Stop(); // 停止 + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Stop, ret:%d", ret); + ret = PlayerCtr->Reset(); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Reset, ret:%d", ret); + + p_RecordingPlayerView->SetPlayerButtonState(UICheckBox::UICheckBoxState::SELECTED); + SetRecordingPlayButtonState(true); + + ret = RecordingPlayerStat(); // 播放 + if (ret != 0) + { + /* stop and exit */ + ret = PlayerCtr->Stop(); // 停止 + ret = PlayerCtr->Reset(); + } + } + else if (playerState == PlayerStates::PLAYER_PAUSED || (playerState == PlayerStates::PLAYER_IDLE)) + { + ret = PlayerCtr->Reset(); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Reset, ret:%d", ret); + } } /* @@ -552,6 +603,7 @@ namespace OHOS return RecordingMp3FileCount; } + // 设置录音数量 void RecordingModel::SetRecordingListNum(int value) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordingModel::GetRecordingListNum %d", value); @@ -619,12 +671,7 @@ namespace OHOS return yyyymmhhh_date; } - RecordingInfo *RecordingModel::GetRecordingInfo() - { - return recordingInfo; - } - - // 设置每首录音的信息 + // 设置每首录音的信息:包含录音文件名,录音时长,录音时间 void RecordingModel::SetRecordingInfo(RecordingInfo &info, int index) { recordingInfo[index] = info; @@ -679,18 +726,61 @@ namespace OHOS std::string &RecordingModel::Getyyyymmhhh_dateAndTotalRecordDuration(void) { WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "[RecordingModel] Getyyyymmhhh_dateAndTotalRecordDuration"); - std::string yyyymmhhh_date = RecordingModel::GetInstance()->Getyyyymmhhh_date(); - std::string TotalRecordDuration = RecordingModel::GetInstance()->GetTotalRecordDuration(); + std::string yyyymmhhh_date = Getyyyymmhhh_date(); + std::string TotalRecordDuration = recordingInfo[RecordingMp3FileCount - index - 1].recordingDuration; // 去掉日期字符串末尾的换行符 if (!yyyymmhhh_date.empty() && yyyymmhhh_date.back() == '\n') { yyyymmhhh_date.erase(yyyymmhhh_date.length() - 1); } RecordingTimeAndDuration = (yyyymmhhh_date + "-" + TotalRecordDuration); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordingTimeAndDuration %s\n", RecordingTimeAndDuration.c_str()); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RecordingTimeAndDuration %s", RecordingTimeAndDuration.c_str()); return RecordingTimeAndDuration; } + void RecordingModel::GetDuration(int64_t &duration) + { + duration = DurationTime; + } + + void RecordingModel::UpdateRecordProcess() + { + if (RecordingPresenter::GetInstance()) + { + RecordingPresenter::GetInstance()->UpdateRecordProcess(); + } + } + + bool RecordingModel::GetRecordingPlayerViewCreate() + { + return RecordingPlayerViewIsCreate; + } + void RecordingModel::SetRecordingPlayerViewCreate(bool state) + { + RecordingPlayerViewIsCreate = state; + } + + /*创建录音播放路径*/ + bool RecordingModel::CreateRecordPlayDirectory(const char *path) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RecordingModel::CreateRecordPlayDirectory"); + if (access(path, F_OK) == -1) + { // 如果路径不存在 + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Directory %s not exist, creating directory", path); + if (mkdir(path, 0777) != 0) + { // 尝试创建目录,如果创建失败 + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Create directory %s failed", path); + return false; + } + } + else + { // 如果路径存在 + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Directory %s already exists", path); + } + return true; + } + + // 获取录音目录 const char *RecordingModel::GetRecordDirectory() const { return directory.c_str(); 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 1bca7622a..bdf785b01 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingPlayerView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingPlayerView.cpp @@ -347,7 +347,10 @@ namespace OHOS */ void RecordingPlayerView::SetPlayerButtonState(UICheckBox::UICheckBoxState state) { - RecordingPlayPauseButton->SetState(state); + if (RecordingPlayPauseButton) + { + RecordingPlayPauseButton->SetState(state); + } } /* @@ -366,27 +369,37 @@ namespace OHOS return PlayerProgressBar; } + /*设置录音播放进度条值*/ + void RecordingPlayerView::SetPlayerProgressBarValue(int value) + { + if (PlayerProgressBar) + { + PlayerProgressBar->SetValue(value); + PlayerProgressBar->Invalidate(); + } + } + /* * @brief 重置组件状态 */ void RecordingPlayerView::ResetComponentStatus(void) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[RecordingPlayerView] ResetComponentStatus"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "[RecordingPlayerView] ResetComponentStatus"); - if (GetPlayerProgressBar() != nullptr) + if (PlayerProgressBar != nullptr) { - GetPlayerProgressBar()->SetValue(0); // 0:进度0% - GetPlayerProgressBar()->Invalidate(); + PlayerProgressBar->SetValue(0); // 0:进度0% + PlayerProgressBar->Invalidate(); } - if (GetRecordingFileNameLabel() != nullptr) + if (RecordingFileName != nullptr) { - GetRecordingFileNameLabel()->SetText(RecordingModel::GetInstance()->GetCurrentPlayRecordingName().c_str()); - GetRecordingFileNameLabel()->Invalidate(); + RecordingFileName->SetText(RecordingModel::GetInstance()->GetCurrentPlayRecordingName().c_str()); + RecordingFileName->Invalidate(); } - if (GetRecordingTimeAndDurationLabel() != nullptr) + if (RecordingTimeAndDuration != nullptr) { - GetRecordingTimeAndDurationLabel()->SetText(RecordingModel::GetInstance()->Getyyyymmhhh_dateAndTotalRecordDuration().c_str()); - GetRecordingTimeAndDurationLabel()->Invalidate(); + RecordingTimeAndDuration->SetText(RecordingModel::GetInstance()->Getyyyymmhhh_dateAndTotalRecordDuration().c_str()); + RecordingTimeAndDuration->Invalidate(); } if (GetPlayerButton() != nullptr) { 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 487dc760e..09b010ebc 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingPresenter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingPresenter.cpp @@ -2,7 +2,7 @@ * @Author: luozheng 1732036298@qq.com * @Date: 2024-03-30 09:14:38 * @LastEditors: luozheng 1732036298@qq.com - * @LastEditTime: 2024-08-27 17:48:39 + * @LastEditTime: 2024-08-28 15:41:29 * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingPresenter.cpp */ @@ -69,7 +69,7 @@ namespace OHOS Presenter::OnStop(); p_RecordingModel->ClearRecordingPlayer(); StopRecord(); - + RecordingModel::GetInstance()->SetRecordingPlayerViewCreate(false); // ScreenAlwaysOnCtr(CANCEL_SCREEN_ALWAYS_ON); } @@ -285,6 +285,62 @@ namespace OHOS return true; } + void RecordingPresenter::SetCircleProgress() + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordingPresenter::SetCircleProgress"); + RecordingPlayerView *p_RecordingPlayerView = RecordingPlayerView::GetInstance(); + if (p_RecordingPlayerView == nullptr) + { + return; + } + UICircleProgress *playbackProgress = p_RecordingPlayerView->GetPlayerProgressBar(); + if (playbackProgress == nullptr) + { + return; + } + int32_t playerState; + playerState = p_RecordingModel->GetRecordingPlayerState(); + state_ = p_RecordingPlayerView->GetPlayerButtonState(); + if ((playerState != PLAYER_STARTED) && (state_ == UICheckBox::UICheckBoxState::SELECTED)) + { + p_RecordingPlayerView->SetPlayerButtonState(UICheckBox::UICheckBoxState::UNSELECTED); + } + else if ((playerState == PLAYER_STARTED) && (state_ == UICheckBox::UICheckBoxState::UNSELECTED)) + { + p_RecordingPlayerView->SetPlayerButtonState(UICheckBox::UICheckBoxState::SELECTED); + } + if (state_ == UICheckBox::UICheckBoxState::SELECTED) + { + p_RecordingModel->GetPlayerCtr()->GetCurrentTime(playerCurrentTime_); + p_RecordingModel->GetDuration(playerMusicDuration_); + if (playerMusicDuration_ != 0) + { + playerProgressValue_ = (playerCurrentTime_ * 100) / playerMusicDuration_; + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "playerProgressValue_ %lld", playerProgressValue_); + } + else + { + playerProgressValue_ = 100; // 100: max progress value + } + p_RecordingModel->SetCircleProgress(playerProgressValue_); + p_RecordingPlayerView->SetPlayerProgressBarValue(playerProgressValue_); + } + } + + void RecordingPresenter::UpdateRecordProcess() + { + if (view_ == nullptr) + { + return; + } + if (view_->GetRecordingViewScrollView(RECORDINGPLAYERVIEW)) + { + if (view_->GetRecordingViewScrollView(RECORDINGPLAYERVIEW)->IsVisible()) + { + SetCircleProgress(); + } + } + } // 录音列表点击监听 bool RecordListViewListener::OnClick(UIView &view, const ClickEvent &event) { @@ -292,7 +348,7 @@ namespace OHOS UNUSED(event); RecordingListitemView *p_RecordingListitemView = static_cast(&view); int index = p_RecordingListitemView->GetRecordingListIndex(); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordListViewListener::OnClick index:%d\n", index); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordListViewListener::OnClick index:%d", index); if (index >= RecordingModel::GetInstance()->GetRecordingListNum()) { WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "RecordListViewListener::OnClick is no index"); @@ -305,21 +361,20 @@ namespace OHOS WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordingModel GetInstance fail"); return false; } - p_RecordingModel->SetClickRecordingIndex(index); - /* ------------------------ 录音播放初始化,包括录音文件名,录音时间和总时长 ------------------------ */ - if (p_RecordingModel->RecordingPlayerInit(index) != OHOS_SUCCESS) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "p_RecordingModel->RecordingPlayerInit fail!!"); - return false; - } - /* ---------------------------------- 切换到录音播放界面 ---------------------------------- */ + p_RecordingModel->SetClickRecordingIndex(index); // 标记当前要播放录音的index_ RecordingView *p_RecordingView = RecordingView::GetInstance(); if (p_RecordingView == nullptr) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "RecordingView GetInstance fail"); return false; } - p_RecordingView->ChangeView(RECORDINGLISTVIEW, RECORDINGPLAYERVIEW); + p_RecordingView->ChangeView(RECORDINGLISTVIEW, RECORDINGPLAYERVIEW); // 切换到录音播放界面 + /* ------------------------ 播放录音 ------------------------ */ + if (p_RecordingModel->RecordingListOnClickPlay() != OHOS_SUCCESS) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "p_RecordingModel->RecordingListOnClickPlay fail!!"); + return false; + } return true; } @@ -762,8 +817,8 @@ namespace OHOS p_RecordingPlayerView->GetPlayerProgressBar()->Invalidate(); } } - // RecordingModel::GetInstance()->ClearActivateAudioInterrupt(); - // usleep(20000); + // GraphicService::GetInstance()->PostGraphicEvent( + // std::bind(&OHOS::RecordingModel::ClearActivateAudioInterrupt, OHOS::RecordingModel::GetInstance())); } void RecordingPlayerCallbackImpl::OnError(int32_t errorType, int32_t errorCode) 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 d1e297b08..4e5eefd36 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingView.cpp @@ -105,6 +105,11 @@ namespace OHOS return MAX_RECORDINGVIEWID; } + UIScrollView *RecordingView::GetRecordingViewScrollView(int index) + { + return RecordingViewScrollView[index]; + } + // 设置按键监听 static void SetKeyActListener() { @@ -143,6 +148,10 @@ namespace OHOS RecordingViewScrollView[thisView] = p_RecordListView->RecordListViewInit(); container_->Add(RecordingViewScrollView[thisView]); } + else + { + RecordingModel::GetInstance()->ClearRecordingPlayer(); + } if (p_RecordListView) { p_RecordListView->RefreshRecordingListData(); @@ -185,6 +194,7 @@ namespace OHOS return false; } RecordingViewScrollView[thisView] = p_RecordingPlayerView->RecordingPlayerViewInit(); + RecordingModel::GetInstance()->SetRecordingPlayerViewCreate(true); container_->Add(RecordingViewScrollView[thisView]); } // 设置“录音播放器”【录音文件名标签】的"文本内容"为被点击的"录音"的录音名 diff --git a/code/sdk/application/wearable/fd230app/nativeui/uiservice/include/ui_service.h b/code/sdk/application/wearable/fd230app/nativeui/uiservice/include/ui_service.h index f8cedc04a..0ee571b81 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/uiservice/include/ui_service.h +++ b/code/sdk/application/wearable/fd230app/nativeui/uiservice/include/ui_service.h @@ -8,7 +8,7 @@ * @Author: luozheng 1732036298@qq.com * @Date: 2024-06-12 11:44:04 * @LastEditors: luozheng 1732036298@qq.com - * @LastEditTime: 2024-08-13 11:06:07 + * @LastEditTime: 2024-08-28 15:50:33 * @FilePath: /FD230/code/sdk/application/wearable/nativeapp/nativeui/uiservice/include/ui_service.h */ /* @@ -84,6 +84,7 @@ typedef enum { UI_UPDATE_USER_QUESTION_STR,//更新用户问题的字符串 UI_UPDATE_AI_ANSWER_STR, //更新AI答案的字符串 UI_HAND_WATCH_FIRST_PAIRED, //手表已配对:给第一次手表启动用配对成功调用 + UI_UPDATE_RECORDPROCESS, //更新录音进度条 UI_UPDATE_OBU_MESSAGE, MAX_NUM_HANDLER, } UiServiceMsgId; diff --git a/code/sdk/application/wearable/fd230app/nativeui/uiservice/src/ui_service.cpp b/code/sdk/application/wearable/fd230app/nativeui/uiservice/src/ui_service.cpp index 141e5405d..e3349af5b 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/uiservice/src/ui_service.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/uiservice/src/ui_service.cpp @@ -23,6 +23,7 @@ #include "obumessage/include/OBUMessageModel.h" #include "player/include/PlayersModel.h" #include "sleep/include/SleepModel.h" +#include "record/include/RecordingModel.h" #include "message/include/MessageModel.h" #include "heartrate/include/HeartModel.h" #include "aivoice/include/AiVoiceModel.h" @@ -119,6 +120,7 @@ extern "C" static void HandleUpdateAIAnswerStr(Request *request); static void HandleWatchFirstPaired(Request *request); static void HandleUpdateOBUMessage(Request *request); + static void HandleUpdateRecordProcess(Request *request); static void HandleUpdateSleep(Request *request); static void HandleUpdateMessage(Request *request); static void HandleUpdateHeartRate(Request *request); @@ -152,6 +154,7 @@ extern "C" {UI_HAND_WATCH_FIRST_PAIRED, HandleWatchFirstPaired}, {UI_UPDATE_OBU_MESSAGE, HandleUpdateOBUMessage}, {UI_AI_JUMP_TO_LISTENING_VIEW, HandleAiJumpToListeningMessage}, + {UI_UPDATE_RECORDPROCESS, HandleUpdateRecordProcess}, {UI_UPDATE_SLEEP_DATA, HandleUpdateSleep}, {UI_UPDATE_MESSAGE_DATA, HandleUpdateMessage}, @@ -608,6 +611,13 @@ extern "C" std::bind(&OHOS::AiVoiceModel::ChangeToAIVoiceListeningView, &OHOS::AiVoiceModel::GetInstance(), state)); } + void HandleUpdateRecordProcess(Request *request) + { + WEARABLE_LOGI(WEARABLE_LOG_MODULE_UI_SERVICE, "HandleUpdateRecordProcess"); + GraphicService::GetInstance()->PostGraphicEvent( + std::bind(&OHOS::RecordingModel::UpdateRecordProcess, OHOS::RecordingModel::GetInstance())); + } + // 更新睡眠数据 void HandleUpdateSleep(Request *request) { -- 2.17.1