2019-04-11 14:40:54 +08:00
|
|
|
|
// Copyright (c) 2016-2018 Kiwano - Nomango
|
2020-01-21 10:09:55 +08:00
|
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
2020-01-21 10:09:55 +08:00
|
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
|
// all copies or substantial portions of the Software.
|
2020-01-21 10:09:55 +08:00
|
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
2019-10-11 21:55:29 +08:00
|
|
|
|
#include <kiwano-audio/Sound.h>
|
2020-01-21 10:09:55 +08:00
|
|
|
|
#include <kiwano/core/ObjectBase.h>
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
|
namespace kiwano
|
2019-03-31 01:37:06 +08:00
|
|
|
|
{
|
2020-01-21 10:09:55 +08:00
|
|
|
|
namespace audio
|
|
|
|
|
|
{
|
|
|
|
|
|
KGE_DECLARE_SMART_PTR(SoundPlayer);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \addtogroup Audio
|
|
|
|
|
|
* @{
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
* @brief 音频播放器
|
2020-01-21 10:09:55 +08:00
|
|
|
|
*/
|
|
|
|
|
|
class KGE_API SoundPlayer : public virtual ObjectBase
|
|
|
|
|
|
{
|
|
|
|
|
|
public:
|
2020-02-06 16:54:47 +08:00
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 创建音频播放器
|
2020-02-06 16:54:47 +08:00
|
|
|
|
static SoundPlayerPtr Create();
|
|
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
SoundPlayer();
|
|
|
|
|
|
|
|
|
|
|
|
~SoundPlayer();
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 加载本地音频文件
|
|
|
|
|
|
/// @param file_path 本地音频文件路径
|
|
|
|
|
|
/// @return 音频标识符
|
2020-02-19 12:09:50 +08:00
|
|
|
|
size_t Load(const String& file_path);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 加载音频资源
|
|
|
|
|
|
/// @param res 音频资源
|
|
|
|
|
|
/// @return 音频标识符
|
2020-02-19 12:09:50 +08:00
|
|
|
|
size_t Load(const Resource& res);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 播放音频
|
|
|
|
|
|
/// @param id 音频标识符
|
|
|
|
|
|
/// @param loop_count 播放循环次数,设置 -1 为循环播放
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void Play(size_t id, int loop_count = 0);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 暂停音频
|
|
|
|
|
|
/// @param id 音频标识符
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void Pause(size_t id);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 继续播放音频
|
|
|
|
|
|
/// @param id 音频标识符
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void Resume(size_t id);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 停止音频
|
|
|
|
|
|
/// @param id 音频标识符
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void Stop(size_t id);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 获取音频播放状态
|
|
|
|
|
|
/// @param id 音频标识符
|
2020-01-21 10:09:55 +08:00
|
|
|
|
bool IsPlaying(size_t id);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 获取音量
|
2020-01-21 10:09:55 +08:00
|
|
|
|
float GetVolume() const;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 设置音量
|
|
|
|
|
|
/// @param volume 音量大小,1.0 为原始音量, 大于 1 为放大音量, 0 为最小音量
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void SetVolume(float volume);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 暂停所有音频
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void PauseAll();
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 继续播放所有音频
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void ResumeAll();
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 停止所有音频
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void StopAll();
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-02-10 17:32:04 +08:00
|
|
|
|
/// @brief 清除缓存
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void ClearCache();
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
float volume_;
|
|
|
|
|
|
|
|
|
|
|
|
using SoundMap = Map<size_t, SoundPtr>;
|
|
|
|
|
|
SoundMap sound_cache_;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
|
} // namespace audio
|
|
|
|
|
|
} // namespace kiwano
|