feat: sound manager added

This commit is contained in:
Sale Djenic 2022-02-10 15:03:31 +01:00 committed by Michał
parent 36167739e9
commit 3d08d2536e
5 changed files with 134 additions and 3 deletions

View File

@ -4,7 +4,7 @@ include(GNUInstallDirs)
# Macro for merging common code between static and shared
macro(add_target name type)
find_package(Qt5 COMPONENTS Core Qml Gui Quick QuickControls2 Widgets Network REQUIRED)
find_package(Qt5 COMPONENTS Core Qml Gui Quick QuickControls2 Widgets Network Multimedia REQUIRED)
file(GLOB_RECURSE HEADERS include/DOtherSide/*.h)
if(APPLE)
@ -27,10 +27,10 @@ macro(add_target name type)
target_include_directories(${name} PUBLIC include include/Qt)
endif()
target_link_libraries(${name} PRIVATE Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Qml Qt5::Quick Qt5::Network)
target_link_libraries(${name} PRIVATE Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Qml Qt5::Quick Qt5::Network Qt5::Multimedia)
# for DOtherSide.pc
set(PC_REQUIRES "Qt5Core, Qt5Gui, Qt5Widgets, Qt5Qml, Qt5Quick, Qt5Network, Qt5DBus")
set(PC_REQUIRES "Qt5Core, Qt5Gui, Qt5Widgets, Qt5Qml, Qt5Quick, Qt5Network, Qt5DBus, Qt5Multimedia")
if (${Qt5QuickControls2_FOUND})
target_link_libraries(${name} PRIVATE Qt5::QuickControls2)
set(PC_REQUIRES "${PC_REQUIRES}, Qt5QuickControls2")

View File

@ -1027,6 +1027,14 @@ DOS_API void dos_keychainmanager_delete(DosKeychainManager* vptr);
#pragma endregion
#pragma region SoundManager exposed methods
DOS_API void dos_soundmanager_play_sound(const char* soundUrl);
DOS_API void dos_soundmanager_set_player_volume(int volume);
DOS_API void dos_soundmanager_stop_player();
#pragma endregion
DOS_API char *dos_to_local_file(const char* fileUrl);
DOS_API char *dos_from_local_file(const char* filePath);

View File

@ -0,0 +1,64 @@
#ifndef STATUS_SOUND_MANAGER_H
#define STATUS_SOUND_MANAGER_H
#include <QObject>
#include <QMediaPlayer>
#include <memory>
namespace Status
{
class SoundManager : public QObject
{
Q_OBJECT
public:
/*!
* Singleton instance.
*/
static SoundManager &instance();
/*!
* Delete copy constructor.
*/
SoundManager(const SoundManager &) = delete;
/*!
* Delete move constructor.
*/
SoundManager(SoundManager &&) = delete;
/*!
* Delete copy asignment operator.
*/
SoundManager &operator=(const SoundManager &) = delete;
/*!
* Delete move asignment operator.
*/
SoundManager &operator=(SoundManager &&) = delete;
/*!
* Plays a sound with soundUrl.
*
* @param soundUrl Url of the sound to play.
*/
void playSound(const QUrl &soundUrl);
/*!
* Sets a volume.
*
* @param volume Volume in range 0 - 100.
*/
void setPlayerVolume(int volume);
/*!
* Stops playing, and resets the play position to the beginning.
*/
void stopPlayer();
private:
/*!
* Constructor.
*/
SoundManager();
std::unique_ptr<QMediaPlayer> m_player;
};
}
#endif

View File

@ -66,6 +66,7 @@
#include "DOtherSide/Status/OSThemeEvent.h"
#include "DOtherSide/Status/OSNotification.h"
#include "DOtherSide/Status/KeychainManager.h"
#include "DOtherSide/Status/SoundManager.h"
#include "DOtherSide/DosSpellchecker.h"
namespace {
@ -1474,6 +1475,27 @@ void dos_keychainmanager_delete(DosKeychainManager* vptr)
}
#pragma endregion
#pragma region SoundManager
void dos_soundmanager_play_sound(const char* soundUrl)
{
auto sound = QUrl(QString::fromUtf8(soundUrl));
Status::SoundManager::instance().playSound(sound);
}
void dos_soundmanager_set_player_volume(int volume)
{
Status::SoundManager::instance().setPlayerVolume(volume);
}
void dos_soundmanager_stop_player()
{
Status::SoundManager::instance().stopPlayer();
}
#pragma endregion
char* dos_to_local_file(const char* fileUrl)
{
return convert_to_cstring(QUrl(QString::fromUtf8(fileUrl)).toLocalFile());

View File

@ -0,0 +1,37 @@
#include "DOtherSide/Status/SoundManager.h"
using namespace Status;
SoundManager &SoundManager::instance()
{
static SoundManager self;
return self;
}
SoundManager::SoundManager() : QObject()
{
m_player.reset(new QMediaPlayer());
}
void SoundManager::playSound(const QUrl &soundUrl)
{
if (m_player->state() != QMediaPlayer::PlayingState)
{
if (m_player->currentMedia().canonicalUrl() != soundUrl)
{
m_player->setMedia(soundUrl);
}
m_player->play();
}
}
void SoundManager::setPlayerVolume(int volume)
{
m_player->setVolume(volume);
}
void SoundManager::stopPlayer()
{
m_player->stop();
}