mcu_hi3321_watch/tjd/ui/app/player/TjdUiAppPlayerPresenter.h
2025-05-26 20:15:20 +08:00

255 lines
7.7 KiB
C++

/*----------------------------------------------------------------------------
* Copyright (c) TJD Technologies Co., Ltd. 2025. All rights reserved.
*
* Description: TjdUiAppPlayerPresenter.h
*
* Author: luziquan@ss-tjd.com
*
* Create: 2024-06-07
*--------------------------------------------------------------------------*/
#ifndef TJD_UI_APP_PLAYER_PRESENTER_H
#define TJD_UI_APP_PLAYER_PRESENTER_H
#include "LyricParser.h"
#include "Presenter.h"
#include "TjdUiAppPlayerBTImpl.h"
#include "TjdUiScreenDrag.h"
#include "TjdUiTaskListener.h"
#include "audio_manager.h"
#include "cmsis_os2.h"
#include "components/root_view.h"
#include "components/ui_slider.h"
#include "player.h"
#include <atomic>
#include <condition_variable>
#include <list>
#include <mutex>
#include <thread>
#include <unordered_map>
namespace TJD {
#define BTN_MUSIC_LIST_VIEW_ID "btnMusicList"
#define BTN_LEFT_VIEW_ID "btnLeft"
#define BTN_RIGHT_VIEW_ID "btnRight"
#define BTN_START_VIEW_ID "btnStart"
#define BTN_CONNECT_VIEW_ID "btnConnect"
#define BTN_LOOP_VIEW_ID "btnLoop"
#define BTN_VOLUME_VIEW_ID "btnVolume"
#define BTN_SEARCH_VIEW_ID "btnSearch"
#define BTN_TARGET_HEADSET_VIEW_ID "btnTargetHeadset"
#define BTN_CONNECT_ENTER_VIEW_ID "btnConnectEnter"
#define BTN_CONNECT_CANCEL_VIEW_ID "btnConnectCancel"
#define BTN_DEVICE_UNPAIR_VIEW_ID "btnDeviceUnpair"
#define BTN_DELETE_ENTRY_MUSIC_VIEW_ID "btnDeleteEntryMusic"
#define BTN_DELETE_CANCEL_MUSIC_VIEW_ID "btnDeleteCancelMusic"
enum PlayerViewIndex
{
PLAYER_VIEW = 0,
MUSIC_LIST_VIEW,
VOLUME_VIEW,
DEVICE_VIEW,
CONNECT_LIST_VIEW,
CONNECT_STATUS_VIEW,
LOW_POWER_VIEW,
SEARCH_LIST_VIEW,
SEARCHING_VIEW,
DEVICE_CONNECT_VIEW,
CONNECT_SUCCESS_VIEW,
CONNECT_FAIL_VIEW,
CONNECT_APP_FAIL_VIEW,
DELETE_MUSIC_VIEW,
PLAYER_UNKNOWN
};
enum PlayerDeviceState
{
PLAYER_PHONE = 0, // 手机音乐
PLAYER_WATCH, // 手表音乐
PLAYER_WATCH_BT, // 蓝牙耳机音乐
};
#define VOLUME_MIN 0
#define VOLUME_MAX 100
enum PlayMode : uint8_t
{
SEQUENCE, // 顺序
REPEATS, // 单曲
RANDOM // 随机
};
struct PlayerCaseInfo
{
char sliceId[256];
int16_t index;
};
class PlayerListOnClickListener : public OHOS::UIView::OnClickListener
{
public:
bool OnClick(OHOS::UIView &view, const OHOS::ClickEvent &event) override;
};
class PlayerInterruptListener : public Audio::InterruptListener
{
public:
void SetOwner(std::shared_ptr<OHOS::Media::Player> player) { player_ = player; }
void SetPaused(bool value) { isPaused_ = value; }
void SetResumed(bool value) { isResumed_ = value; }
void ResetFlag()
{
isPaused_ = false;
isDelayed_ = false;
isResumed_ = false;
isBeginStopped_ = false;
isEndStopped_ = false;
};
bool IsPaused() { return isPaused_; }
bool IsDelayed() { return isDelayed_; }
bool IsResumed() { return isResumed_; }
bool IsBeginStopped() { return isBeginStopped_; }
bool IsEndStopped() { return isEndStopped_; }
void OnInterrupt(int32_t type, int32_t hint) override;
static void PausePlayer(void);
static void ResumePlayer(void);
static void StopPlayer(void);
std::shared_ptr<OHOS::Media::Player> GetPlayer() { return player_; };
private:
std::shared_ptr<OHOS::Media::Player> player_{nullptr};
bool isPaused_{false};
bool isDelayed_{false};
bool isResumed_{false};
bool isBeginStopped_{false};
bool isEndStopped_{false};
};
class PlayerCallbackImpl : public OHOS::Media::PlayerCallback
{
public:
void OnPlaybackComplete();
void OnError(int32_t errorType, int32_t errorCode);
void OnInfo(int type, int extra);
void OnRewindToComplete();
private:
};
class VolumeChangeListener : public OHOS::UISlider::UISliderEventListener
{
public:
VolumeChangeListener();
~VolumeChangeListener();
void DebouncedSetVolume();
virtual void OnChange(int32_t value) override;
virtual void OnRelease(int32_t sounding) override;
private:
std::atomic<int32_t> lastVolume_{-1};
std::atomic<bool> debounceActive_{false};
std::mutex mtx_;
std::condition_variable cv_;
bool stopThread_{false};
std::thread debounceThread_;
uint32_t debounceInterval_{150}; // 防抖间隔时间,单位为毫秒
};
class TjdUiAppPlayerView;
class TjdUiAppPlayerPresenter : public OHOS::Presenter<TjdUiAppPlayerView>,
public TjdUiTaskListener,
public OHOS::UIView::OnClickListener,
public TjdUiScreenDragListener,
public OHOS::RootView::OnKeyActListener,
public OHOS::UISlider::UISliderEventListener
{
public:
TjdUiAppPlayerPresenter();
virtual ~TjdUiAppPlayerPresenter();
static TjdUiAppPlayerPresenter *GetInstance(void);
void Notify() override;
void OnStart() override;
void OnStop() override;
void ScreenDragEventCallback(OHOS::UIView &view, const OHOS::DragEvent &event) override { ViewExitProcess(true); }
void PhonePlayerStatusChange(int state);
void UpdateLrcData(void);
void LrcSyncCallback(const char *lrc, bool isFirst);
virtual bool OnKeyAct(OHOS::UIView &view, const OHOS::KeyEvent &event) override;
virtual bool OnClick(OHOS::UIView &view, const OHOS::ClickEvent &event) override;
virtual void OnChange(int32_t value) override;
virtual void OnRelease(int32_t value) override;
void MusicListOnClick();
void LeftOnClick();
void RightOnClick();
void StartOnClick();
void ConnectOnClick();
void LoopOnClick();
void VolumeOnClick();
void ScanHeadsetOnClick();
void SelectPhoneDeviceOnClick(void);
void SelectWatchDeviceOnClick(void);
void HeadsetConnectOnClick(void);
void HeadsetConnectDeviceOnClick(void);
void HeadsetConnectCancelOnClick(void);
void ClickDeleteCancelEvent(void);
void ClickDeleteEntryEvent(void);
void PlayerButtonReset(void);
void PlayerBackReset(void);
void SetMusicName(const char *musicName);
int16_t GetCurMusicIndex(void);
bool IsPlaying();
void SetConnectDeviceAddr(bd_addr_t addr);
void HeadsetUnpairDeviceOnClick(void);
void HeadsetEnableOnClick(void);
void HeadsetDisableOnClick(void);
void StartJumpTimer(PlayerViewIndex jumpView, uint32_t ticks = 2000);
void DeleteJumpTimer(void);
void SetSelectView(OHOS::UIView *view)
{
if (view == nullptr) {
selectView_ = nullptr;
return;
}
selectView_ = view;
}
OHOS::UIView *GetSelectView() { return selectView_; }
bool ClickListMusic(OHOS::UIView *view, bool isDelete);
std::unique_ptr<VolumeChangeListener> &GetVolumeListener() { return volumeListener_; }
std::list<HeadsetInfo> &GetHeadsetList() { return btImpl_->GetHeadsetList(); }
bool IsNeedUpdateHeadsetList{false};
bool IsDragSlider{false};
private:
std::unique_ptr<VolumeChangeListener> volumeListener_;
std::unique_ptr<TjdUiAppPlayerBTImpl> btImpl_;
std::unique_ptr<LrcParser> lrcParser_;
void ExitPlayerView(void);
void ViewExitProcess(bool isSwipe);
std::string *GetLrcPath();
osTimerId_t jumpTimerId_{nullptr};
std::string lrcPath_;
std::string lrc_{""};
OHOS::UIView *selectView_{nullptr};
int selectIndex_;
bool seekFlag_{false};
int64_t seekTime_{0};
std::atomic<bool> lrcRunning_{false};
std::unordered_map<int, int> playModeResMap;
};
} // namespace TJD
#endif