387 lines
14 KiB
C++
387 lines
14 KiB
C++
/*----------------------------------------------------------------------------
|
||
* Copyright (c) TJD Technologies Co., Ltd. 2024. All rights reserved.
|
||
*
|
||
* Description: TjdUiAppMainPageMusic.cpp
|
||
*
|
||
* Author: luziquan@ss-tjd.com
|
||
*
|
||
* Create: 2024-12-30
|
||
*--------------------------------------------------------------------------*/
|
||
|
||
#include "TjdUiAppMainPageMusic.h"
|
||
#include "NativeAbility.h"
|
||
#include "TjdUiAppIds.h"
|
||
#include "TjdUiAppPlayerModel.h"
|
||
#include "TjdUiImageIds.h"
|
||
#include "TjdUiMemManage.h"
|
||
#include "TjdUiMultiLanguageExt.h"
|
||
#include "common/image_cache_manager.h"
|
||
#include "common/screen.h"
|
||
#include "hal_tick.h"
|
||
#include "sys_config.h"
|
||
#include <iomanip>
|
||
#include <sstream>
|
||
|
||
namespace TJD {
|
||
|
||
#define ENABLE_PRINT_INFO 1
|
||
#if ENABLE_PRINT_INFO
|
||
#define static_print_info(...) sys_ui_log_i(__VA_ARGS__) // 一般信息打印宏控制
|
||
#define static_print_warn(...) sys_ui_log_w(__VA_ARGS__) // 警告信息打印一般常开
|
||
#define static_print_error(...) sys_ui_log_e(__VA_ARGS__) // 错误信息打印一般常开
|
||
#else
|
||
#define static_print_info(...)
|
||
#define static_print_warn(...)
|
||
#define static_print_error(...)
|
||
#endif
|
||
|
||
#define MUSIC_IMAGE_BIN_PATH TJD_IMAGE_PATH "img_main_music.bin"
|
||
|
||
static constexpr const char *MUSIC_LEFT_VIEW_ID = "musicLeft";
|
||
static constexpr const char *MUSIC_RIGHT_VIEW_ID = "musicRight";
|
||
static constexpr const char *START_ICON_VIEW_ID = "musicStart";
|
||
static constexpr const char *AI_ICON_VIEW_ID = "aiIcon";
|
||
static constexpr const char *RECORD_ICON_VIEW_ID = "recordIcon";
|
||
|
||
#define MUSIC_ANIMATOR_COUNT 6
|
||
|
||
static const int g_musicAnimatorId[MUSIC_ANIMATOR_COUNT] = {
|
||
IMG_MAIN_MUSIC_DT_MUSIC_01_03, IMG_MAIN_MUSIC_DT_MUSIC_02_03, IMG_MAIN_MUSIC_DT_MUSIC_03_03,
|
||
IMG_MAIN_MUSIC_DT_MUSIC_04_03, IMG_MAIN_MUSIC_DT_MUSIC_05_03, IMG_MAIN_MUSIC_DT_MUSIC_06_03,
|
||
};
|
||
|
||
static TjdUiAppMainPageMusic *g_mainMusicPage = nullptr;
|
||
|
||
TjdUiAppMainPageMusic::TjdUiAppMainPageMusic()
|
||
{
|
||
SetPosition(0, 0, OHOS::Screen::GetInstance().GetWidth(), OHOS::Screen::GetInstance().GetHeight());
|
||
auto playerModel = TjdUiAppPlayerModel::GetInstance();
|
||
playerModel->PlayerStop();
|
||
g_mainMusicPage = this;
|
||
}
|
||
|
||
TjdUiAppMainPageMusic::~TjdUiAppMainPageMusic()
|
||
{
|
||
static_print_debug("TjdUiAppMainPageMusic::~TjdUiAppMainPageMusic");
|
||
UnLoad();
|
||
g_mainMusicPage = nullptr;
|
||
}
|
||
|
||
TjdUiAppMainPageMusic *TjdUiAppMainPageMusic::GetInstance(void) { return g_mainMusicPage; }
|
||
|
||
void TjdUiAppMainPageMusic::PreLoad(void)
|
||
{
|
||
static bool isSetUpListCase = false;
|
||
auto playerModel = TjdUiAppPlayerModel::GetInstance();
|
||
OHOS::ImageCacheManager &image = OHOS::ImageCacheManager::GetInstance();
|
||
if (!viewiInitStatus) {
|
||
image.LoadAllInMultiRes(MUSIC_IMAGE_BIN_PATH);
|
||
for (int i = 0; i < MUSIC_ANIMATOR_COUNT; i++) {
|
||
animatorInfo_[i].imageInfo = image.LoadOneInMultiRes(g_musicAnimatorId[i], MUSIC_IMAGE_BIN_PATH);
|
||
animatorInfo_[i].width = 317;
|
||
animatorInfo_[i].height = 95;
|
||
animatorInfo_[i].pos = {75, 70};
|
||
animatorInfo_[i].imageType = OHOS::IMG_SRC_IMAGE_INFO;
|
||
}
|
||
InitView();
|
||
if (isSetUpListCase == false) {
|
||
PlayersListGroup::GetInstance()->SetUpListCase();
|
||
isSetUpListCase = true;
|
||
}
|
||
playerModel->PlayerInit();
|
||
SyncState();
|
||
UpdateValue();
|
||
viewiInitStatus = true;
|
||
}
|
||
}
|
||
|
||
void TjdUiAppMainPageMusic::UnLoad(void)
|
||
{
|
||
if (viewiInitStatus) {
|
||
OHOS::ImageCacheManager::GetInstance().UnloadAllInMultiRes(MUSIC_IMAGE_BIN_PATH);
|
||
RemoveAll();
|
||
if (container_ != nullptr) {
|
||
container_->RemoveAll();
|
||
delete container_;
|
||
container_ = nullptr;
|
||
}
|
||
if (imageAnimator_ != nullptr) {
|
||
delete imageAnimator_;
|
||
imageAnimator_ = nullptr;
|
||
}
|
||
if (lefunAiIcon_ != nullptr) {
|
||
delete lefunAiIcon_;
|
||
lefunAiIcon_ = nullptr;
|
||
}
|
||
if (recordIcon_ != nullptr) {
|
||
delete recordIcon_;
|
||
recordIcon_ = nullptr;
|
||
}
|
||
if (musicName_ != nullptr) {
|
||
delete musicName_;
|
||
musicName_ = nullptr;
|
||
}
|
||
if (left_ != nullptr) {
|
||
delete left_;
|
||
left_ = nullptr;
|
||
}
|
||
if (start_ != nullptr) {
|
||
delete start_;
|
||
start_ = nullptr;
|
||
}
|
||
if (right_ != nullptr) {
|
||
delete right_;
|
||
right_ = nullptr;
|
||
}
|
||
if (circleProgress_ != nullptr) {
|
||
delete circleProgress_;
|
||
circleProgress_ = nullptr;
|
||
}
|
||
if (leftTime_ != nullptr) {
|
||
delete leftTime_;
|
||
leftTime_ = nullptr;
|
||
}
|
||
if (rightTime_ != nullptr) {
|
||
delete rightTime_;
|
||
rightTime_ = nullptr;
|
||
}
|
||
if (circleProgressBg_ != nullptr) {
|
||
delete circleProgressBg_;
|
||
circleProgressBg_ = nullptr;
|
||
}
|
||
|
||
viewiInitStatus = false;
|
||
}
|
||
}
|
||
|
||
static std::string ConvertToTimeFormat(uint64_t Seconds)
|
||
{
|
||
uint64_t minutes = Seconds / 1000 / 60; // 计算分钟数
|
||
uint64_t seconds = Seconds / 1000 % 60; // 计算剩余的秒数
|
||
|
||
// 使用setw设置宽度,setfill设置填充字符,确保分钟和秒都是两位数
|
||
std::stringstream ss;
|
||
ss << std::setw(2) << std::setfill('0') << minutes << ":" << std::setw(2) << std::setfill('0') << seconds;
|
||
return ss.str();
|
||
}
|
||
|
||
void TjdUiAppMainPageMusic::NotifyTick(void)
|
||
{
|
||
static uint32_t lastUpdateTime = 0;
|
||
uint32_t curTime = OHOS::HALTick::GetInstance().GetTime();
|
||
if (lastUpdateTime == 0) {
|
||
lastUpdateTime = curTime;
|
||
}
|
||
/* 每秒刷新一次 */
|
||
if (curTime - lastUpdateTime < 1 * 1000) {
|
||
return;
|
||
}
|
||
lastUpdateTime = curTime;
|
||
UpdateValue();
|
||
}
|
||
|
||
bool TjdUiAppMainPageMusic::OnClick(OHOS::UIView &view, const OHOS::ClickEvent &event)
|
||
{
|
||
int ret = 0;
|
||
auto playerModel = TjdUiAppPlayerModel::GetInstance();
|
||
std::string viewId = view.GetViewId();
|
||
if (viewId == MUSIC_LEFT_VIEW_ID) {
|
||
playerModel->PreviousPlayout();
|
||
} else if (viewId == MUSIC_RIGHT_VIEW_ID) {
|
||
playerModel->LoopingPlayout();
|
||
} else if (viewId == START_ICON_VIEW_ID) {
|
||
if (start_ == nullptr) {
|
||
return true;
|
||
}
|
||
if (start_->GetState() == OHOS::UICheckBox::SELECTED) {
|
||
playerModel->ConnectBtAndLocalPlay(0, 0);
|
||
if (playerModel->GetPlayerState() != OHOS::Media::PlayerStates::PLAYER_STARTED) {
|
||
start_->SetState(OHOS::UICheckBox::UNSELECTED);
|
||
static_print_error("PlayerStart fail");
|
||
}
|
||
} else {
|
||
playerModel->ConnectBtAndLocalPause(0, 0);
|
||
}
|
||
} else if (viewId == AI_ICON_VIEW_ID) {
|
||
OHOS::NativeAbility::GetInstance().ChangeSlice(TJD_APP_VIEW_LEFUN_AI);
|
||
} else if (viewId == RECORD_ICON_VIEW_ID) {
|
||
OHOS::NativeAbility::GetInstance().ChangeSlice(TJD_APP_VIEW_RECORD);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
void TjdUiAppMainPageMusic::InitView(void)
|
||
{
|
||
OHOS::ImageCacheManager &image = OHOS::ImageCacheManager::GetInstance();
|
||
auto screenWidth = OHOS::Screen::GetInstance().GetWidth();
|
||
auto screenHeight = OHOS::Screen::GetInstance().GetHeight();
|
||
|
||
container_ = new UIViewGroup();
|
||
container_->SetPosition(0, 0, screenWidth, screenHeight);
|
||
|
||
imageAnimator_ = new OHOS::UIImageAnimatorView();
|
||
imageAnimator_->SetPosition(75, 70, 317, 95);
|
||
imageAnimator_->SetImageAnimatorSrc(animatorInfo_, MUSIC_ANIMATOR_COUNT, 100);
|
||
container_->Add(imageAnimator_);
|
||
imageAnimator_->Start();
|
||
|
||
lefunAiIcon_ = new OHOS::UIImageView();
|
||
lefunAiIcon_->SetPosition(113, 183, 100, 100);
|
||
lefunAiIcon_->SetTouchable(true);
|
||
lefunAiIcon_->SetViewId(AI_ICON_VIEW_ID);
|
||
lefunAiIcon_->SetOnClickListener(this);
|
||
lefunAiIcon_->SetSrc(image.LoadOneInMultiRes(IMG_MAIN_MUSIC_LEFUN_AI, MUSIC_IMAGE_BIN_PATH));
|
||
container_->Add(lefunAiIcon_);
|
||
|
||
recordIcon_ = new OHOS::UIImageView();
|
||
recordIcon_->SetPosition(251, 183, 100, 100);
|
||
recordIcon_->SetTouchable(true);
|
||
recordIcon_->SetViewId(RECORD_ICON_VIEW_ID);
|
||
recordIcon_->SetOnClickListener(this);
|
||
recordIcon_->SetSrc(image.LoadOneInMultiRes(IMG_MAIN_MUSIC_RECORDING, MUSIC_IMAGE_BIN_PATH));
|
||
container_->Add(recordIcon_);
|
||
|
||
musicName_ = new OHOS::UILabel();
|
||
musicName_->SetFont(TJD_D_DIN_PRO_REGULAR_FONT_FILENAME, 28);
|
||
musicName_->SetText("Enough for you");
|
||
musicName_->SetLineBreakMode(OHOS::UILabel::LINE_BREAK_ELLIPSIS);
|
||
musicName_->SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER);
|
||
musicName_->SetPosition(60, 300, 347, 28);
|
||
container_->Add(musicName_);
|
||
|
||
left_ = new OHOS::UIImageView();
|
||
left_->SetPosition(117, 374, 23, 23);
|
||
left_->SetTouchable(true);
|
||
left_->SetViewId(MUSIC_LEFT_VIEW_ID);
|
||
left_->SetOnClickListener(this);
|
||
left_->SetSrc(image.LoadOneInMultiRes(IMG_MAIN_MUSIC_L, MUSIC_IMAGE_BIN_PATH));
|
||
container_->Add(left_);
|
||
|
||
start_ = new OHOS::UICheckBox();
|
||
start_->SetPosition(213, 364, 40, 46);
|
||
start_->SetTouchable(true);
|
||
start_->SetViewId(START_ICON_VIEW_ID);
|
||
start_->SetOnClickListener(this);
|
||
start_->SetImages(image.LoadOneInMultiRes(IMG_MAIN_MUSIC_SUSPEND, MUSIC_IMAGE_BIN_PATH),
|
||
image.LoadOneInMultiRes(IMG_MAIN_MUSIC_START, MUSIC_IMAGE_BIN_PATH));
|
||
container_->Add(start_);
|
||
|
||
right_ = new OHOS::UIImageView();
|
||
right_->SetPosition(316, 374, 23, 23);
|
||
right_->SetTouchable(true);
|
||
right_->SetViewId(MUSIC_RIGHT_VIEW_ID);
|
||
right_->SetOnClickListener(this);
|
||
right_->SetSrc(image.LoadOneInMultiRes(IMG_MAIN_MUSIC_R, MUSIC_IMAGE_BIN_PATH));
|
||
container_->Add(right_);
|
||
|
||
leftTime_ = new OHOS::UILabel();
|
||
leftTime_->SetStyle(OHOS::STYLE_TEXT_OPA, OHOS::OPA_OPAQUE * 0.3);
|
||
leftTime_->SetFont(TJD_D_DIN_PRO_REGULAR_FONT_FILENAME, 20);
|
||
leftTime_->SetText("0:00");
|
||
leftTime_->SetLineBreakMode(OHOS::UILabel::LINE_BREAK_ELLIPSIS);
|
||
leftTime_->SetAlign(OHOS::TEXT_ALIGNMENT_LEFT, OHOS::TEXT_ALIGNMENT_CENTER);
|
||
leftTime_->SetPosition(14, 250, 65, 20);
|
||
container_->Add(leftTime_);
|
||
|
||
rightTime_ = new OHOS::UILabel();
|
||
rightTime_->SetStyle(OHOS::STYLE_TEXT_OPA, OHOS::OPA_OPAQUE * 0.3);
|
||
rightTime_->SetFont(TJD_D_DIN_PRO_REGULAR_FONT_FILENAME, 20);
|
||
rightTime_->SetText("0:00");
|
||
rightTime_->SetLineBreakMode(OHOS::UILabel::LINE_BREAK_ELLIPSIS);
|
||
rightTime_->SetAlign(OHOS::TEXT_ALIGNMENT_LEFT, OHOS::TEXT_ALIGNMENT_CENTER);
|
||
rightTime_->SetPosition(420, 250, 65, 20);
|
||
container_->Add(rightTime_);
|
||
|
||
circleProgressBg_ = new OHOS::UIImageView();
|
||
circleProgressBg_->SetPosition(15, 286, 436, 469);
|
||
circleProgressBg_->SetSrc(image.LoadOneInMultiRes(IMG_MAIN_MUSIC_MUSIC_PROGRESS, MUSIC_IMAGE_BIN_PATH));
|
||
container_->Add(circleProgressBg_);
|
||
|
||
// auto foregroundImage = image.LoadOneInMultiRes(IMG_MAIN_MUSIC_MUSIC_PROGRESS, MUSIC_IMAGE_BIN_PATH);
|
||
// auto backgroundImage = image.LoadOneInMultiRes(IMG_MAIN_MUSIC_MUSIC_BJ, MUSIC_IMAGE_BIN_PATH);
|
||
// if (foregroundImage && backgroundImage) {
|
||
// circleProgress_ = new OHOS::UICircleProgress();
|
||
// circleProgress_->SetPosition(0, 0, screenWidth, screenHeight);
|
||
// circleProgress_->SetCenterPosition(screenWidth / 2, screenHeight / 2);
|
||
// circleProgress_->SetRadius(233);
|
||
// circleProgress_->SetBackgroundStyle(OHOS::STYLE_LINE_WIDTH, 300);
|
||
// circleProgress_->SetForegroundStyle(OHOS::STYLE_LINE_WIDTH, 300);
|
||
// circleProgress_->SetStartAngle(260);
|
||
// circleProgress_->SetEndAngle(100);
|
||
// circleProgress_->SetImage(foregroundImage, backgroundImage);
|
||
// circleProgress_->SetLineColor(OHOS::Color::Red());
|
||
// circleProgress_->SetProgressImagePosition(17, screenHeight / 2 + 58);
|
||
// circleProgress_->SetBackgroundImagePosition(17, screenHeight / 2 + 58);
|
||
// container_->Add(circleProgress_);
|
||
// }
|
||
|
||
Add(container_);
|
||
}
|
||
|
||
void TjdUiAppMainPageMusic::UpdateValue(void)
|
||
{
|
||
auto playerModel = TjdUiAppPlayerModel::GetInstance();
|
||
std::string musicName = PlayersListGroup::GetInstance()->GetCase(playerModel->GetCurrentIndex());
|
||
if (musicName.empty() || musicName == "o") {
|
||
musicName = "No music files";
|
||
}
|
||
if (musicName_) {
|
||
musicName_->SetText(musicName.c_str());
|
||
}
|
||
auto playerCtr = playerModel->GetPlayerCtr().get();
|
||
if (playerCtr == nullptr) {
|
||
if (start_) {
|
||
start_->SetState(OHOS::UICheckBox::UNSELECTED);
|
||
}
|
||
return;
|
||
}
|
||
int32_t state = 0;
|
||
playerCtr->GetPlayerState(state);
|
||
|
||
if (state == OHOS::Media::PlayerStates::PLAYER_STARTED) {
|
||
uint64_t duration = playerModel->GetDuration();
|
||
int64_t curPos = playerModel->GetCurrentPosition();
|
||
if (rightTime_) {
|
||
rightTime_->SetText(ConvertToTimeFormat(duration).c_str());
|
||
}
|
||
if (leftTime_) {
|
||
leftTime_->SetText(ConvertToTimeFormat(curPos).c_str());
|
||
}
|
||
if (circleProgress_) {
|
||
std::stringstream ss;
|
||
ss << curPos * 100 / duration;
|
||
// curPos / duration 为当前进度 转为0-100
|
||
int value = std::stoi(ss.str().c_str());
|
||
circleProgress_->SetValue(value);
|
||
}
|
||
} else if (state == OHOS::Media::PlayerStates::PLAYER_STOPPED ||
|
||
state == OHOS::Media::PlayerStates::PLAYER_PLAYBACK_COMPLETE ||
|
||
state == OHOS::Media::PlayerStates::PLAYER_IDLE) {
|
||
if (start_) {
|
||
start_->SetState(OHOS::UICheckBox::UNSELECTED);
|
||
}
|
||
}
|
||
}
|
||
|
||
void TjdUiAppMainPageMusic::SyncState(void)
|
||
{
|
||
auto playerModel = TjdUiAppPlayerModel::GetInstance();
|
||
auto playerCtr = playerModel->GetPlayerCtr().get();
|
||
if (playerCtr == nullptr) {
|
||
return;
|
||
}
|
||
int32_t state = 0;
|
||
playerCtr->GetPlayerState(state);
|
||
if (state == OHOS::Media::PlayerStates::PLAYER_STARTED) {
|
||
if (start_) {
|
||
start_->SetState(OHOS::UICheckBox::SELECTED);
|
||
}
|
||
} else {
|
||
if (start_) {
|
||
start_->SetState(OHOS::UICheckBox::UNSELECTED);
|
||
}
|
||
}
|
||
}
|
||
|
||
} // namespace TJD
|