From: luozheng <1732036298@qq.com> Date: Fri, 1 Mar 2024 09:25:34 +0000 (+0800) Subject: 录音Model修改 X-Git-Tag: release_V1.2.70_20241020_135402~1274^2~38^2~11 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=96af7ad88aa3f10fcab980f3b88829c33a4619f9;p=mirrors%2FFD230.git 录音Model修改 --- 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 eed0a7abe..0534663bb 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingModel.h +++ b/code/sdk/application/wearable/fd230app/nativeui/record/include/RecordingModel.h @@ -8,53 +8,53 @@ #include "source.h" #include "audio_manager.h" #include "audio_base_type.h" - +#include "record/include/RecordingPresenter.h" namespace OHOS { - using namespace OHOS::Media; using OHOS::Media::Player; using OHOS::Media::Source; + using namespace OHOS::Media; + using namespace ::Audio; + using Audio::AudioManager; -// constexpr int ERR_RET = -1; -// static constexpr int VOLUME_MAXIMUM = 100; -// static constexpr int VOLUME_MINIMUM = 0; -// static constexpr int VOLUME_PER_CHANGE = 10; -// #define BUFFER_SIZE 15 - - // enum PlayerModelState : uint8_t - // { - // LOOP, - // SEQUENCE, - // REPEATS, - // RANDOM, - // }; + // 播放器 +class RecordingPlayerInterruptListener : public InterruptListener { +public: + RecordingPlayerInterruptListener(){} + ~RecordingPlayerInterruptListener(){} - // enum - // { - // NORMAL, - // NONE_SONG_PLAY, - // SONG_ALREADY_PLAY, - // SONG_ALREADY_PAUSE, - // SONG_ALREADY_STOP, - // EXECUTE_FUNC_FAILED, - // NO_SUPPORT_SCENE, - // PLAYER_RESPONESE_MAX - // }; + void SetOwner(std::shared_ptr player); + void SetPaused(bool value); + void SetResumed(bool value); + void RestFlag(); + bool IsPaused(); + bool IsDelayed(); + bool IsResumed(); + bool IsBeginStopped(); + bool IsEndStopped(); + void OnInterrupt(int32_t type, int32_t hint) override; + void PausePlayer(void); + void ResumePlayer(void); + void StopPlayer(void); + std::shared_ptr GetPlayer(); - // class PlayerInterruptListener; - // typedef struct - // { - // uint16 playerModel; - // uint32 value; - // } PlayersMsg; +private: + std::shared_ptr player_{nullptr}; + bool isPaused_{false}; + bool isDelayed_{false}; + bool isResumed_{false}; + bool isBeginStopped_{false}; + bool isEndStopped_{false}; +}; class RecordingModel { public: static RecordingModel *GetInstance(void); - std::string GetRecordingPlayerFilePath(void); - void SetRecordingPlayerFilePath(std::string src); + std::string GetRecordingPlayerUri(void); + void SetRecordingPlayerUri(std::string src); void SetRecordingPlayButtonState(bool state); + int RecordingPlayerStat(); private: RecordingModel(); @@ -63,8 +63,17 @@ namespace OHOS virtual ~RecordingModel(); private: - std::string FilePath; - bool playButtonState{false}; + int64_t DurationTime{0}; // 播放时长,单位ms + bool PlayButtonState{false}; // 播放按钮状态 + bool IsActivateAudioInterrupt{false}; // 是否激活音频中断 + AudioSession SessionId; // 音频会话ID + AudioInterrupt interrupt; // 音频中断管理 + std::shared_ptr p_RecordingPlayerInterruptListener{nullptr}; // 录音播放器中断监听 + std::shared_ptr PlayerCtr{nullptr}; // Playerç±»:提供了播放在线电影、离线电影以及流媒体的功能,比如播放本地电影和高级音频编码(AAC)流的功能。 + std::shared_ptr PlayerCallback{nullptr}; // 为媒体播放期间发生的事件和异常通知提供侦听器。 + std::string Uri; // 唯一标识一个资源的字符串。 + std::map Header; + std::shared_ptr Resource{nullptr}; }; } 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 6d762d068..df6da910f 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/record/src/RecordingModel.cpp @@ -3,6 +3,8 @@ #include "thread_adapter.h" #include "UiConfig.h" #include "bts_a2dp_source.h" +#include "audio_manager.h" +#include namespace OHOS { @@ -23,26 +25,111 @@ namespace OHOS return &instance; } - /// @brief 获取录音播放器文件路径 + /// @brief 获取录音播放器Uri /// @param void /// @return 文件路径 - std::string RecordingModel::GetRecordingPlayerFilePath(void) + std::string RecordingModel::GetRecordingPlayerUri(void) { - return FilePath; + return Uri; } - /// @brief 设置录音播放器文件路径 + /// @brief 设置录音播放器Uri /// @param src 文件路径 - void RecordingModel::SetRecordingPlayerFilePath(std::string src) + void RecordingModel::SetRecordingPlayerUri(std::string src) { - FilePath = src; + Uri = src; } /// @brief 设置录音播放按钮状态 /// @param state true void RecordingModel::SetRecordingPlayButtonState(bool state) { - playButtonState = state; + PlayButtonState = state; + } + + //播放器状态的控制 + int RecordingModel::RecordingPlayerStat() + { + /* ------------------------------ 1.设置播放器的回调函数。 ----------------------------- */ + // 设置回调函数 + PlayerCtr->SetPlayerCallback(PlayerCallback); + /* ------------------------- 2.创建资源对象Resource并进行资源申请。 ------------------------- */ + Resource = std::make_shared(Uri, Header); + 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.", Uri.c_str()); + /* ----------------------- 3.设置播放器的数据源为Resource,并进行准备操作。 ---------------------- */ + PlayerCtr->SetSource(*Resource); + PlayerCtr->Prepare(); + /* ------------------------------ 4.获取播放内容的时长。 ------------------------------ */ + int32_t ret = PlayerCtr->GetDuration(DurationTime); + if (ret != -1) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[%s,%d] GetDuration:%lld, ret:%d", __func__, __LINE__, DurationTime, ret); + } + else + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Get Duration time fail!"); + return OHOS_FAILURE; + } + /* ----------------------------- 5.如果未激活音频中断处理: ----------------------------- */ + if (!IsActivateAudioInterrupt) + { + /* -------------------------------- 申请音频会话ID。 -------------------------------- */ + AudioManager &amIntance = AudioManager::GetInstance(); + SessionId = amIntance.MakeSessionId(); + if (SessionId == AUDIO_SESSION_ID_NONE) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SessionId invalid"); + return OHOS_FAILURE; + } + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MakeSessionId %d", SessionId); + /* ----------------------------- 创建并设置播放器中断监听器。 ----------------------------- */ + p_RecordingPlayerInterruptListener = std::make_shared(); + if (p_RecordingPlayerInterruptListener.get() == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "p_RecordingPlayerInterruptListener new fail"); + return OHOS_FAILURE; + } + p_RecordingPlayerInterruptListener->SetOwner(PlayerCtr); + /* + struct AudioInterrupt + { + AudioStreamType streamType; 标识不同种类的音频流 + AudioSession sessionID; 唯一标识不同的音频会话 + std::shared_ptr interruptListener;理音频中断事件的监听器 + }; + */ + interrupt = {AUDIO_STREAM_VOICE_RECORD, SessionId, p_RecordingPlayerInterruptListener}; + /* --------------------------- 激活音频中断处理,设置超时时间为5秒 --------------------------- */ + // 激活音频中断 5s超时 + if (amIntance.ActivateAudioInterrupt(interrupt) == INTERRUPT_FAILED) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "ActivateAudioInterrupt faild"); + return OHOS_FAILURE; + } + IsActivateAudioInterrupt = true; + } + /* ----------------------------- 6.设置播放器的音频会话ID。 ---------------------------- */ + PlayerCtr->SetAudioSessionId(SessionId); + /* ---------------------------- 7.开始播放内容,如果播放失败: ---------------------------- */ + ret = PlayerCtr->Play(); + if (ret != 0) + { + PlayerCtr->Stop(); // 暂停 + PlayerCtr->Reset(); // 初始化 + AudioManager::GetInstance().DeactivateAudioInterrupt(interrupt); + IsActivateAudioInterrupt = false; + } + return OHOS_SUCCESS; + } + + void RecordingPlayerInterruptListener::SetOwner(std::shared_ptr player) + { + player_ = player; } } \ No newline at end of file