mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 23:10:05 +00:00
Add play audio functionality to media manager
This commit is contained in:
parent
318f35aebd
commit
3ad3c98daf
2 changed files with 64 additions and 2 deletions
|
|
@ -1,6 +1,11 @@
|
|||
#include <scwx/qt/manager/media_manager.hpp>
|
||||
#include <scwx/util/logger.hpp>
|
||||
|
||||
#include <QAudioDevice>
|
||||
#include <QAudioOutput>
|
||||
#include <QMediaPlayer>
|
||||
#include <QUrl>
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
namespace qt
|
||||
|
|
@ -14,14 +19,67 @@ static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
|
|||
class MediaManager::Impl
|
||||
{
|
||||
public:
|
||||
explicit Impl() {}
|
||||
explicit Impl(MediaManager* self) :
|
||||
self_ {self},
|
||||
mediaPlayer_ {new QMediaPlayer(self)},
|
||||
audioOutput_ {new QAudioOutput(self)}
|
||||
{
|
||||
audioOutput_->setVolume(1.0f);
|
||||
mediaPlayer_->setAudioOutput(audioOutput_);
|
||||
|
||||
logger_->debug("Audio device: {}",
|
||||
audioOutput_->device().description().toStdString());
|
||||
|
||||
ConnectSignals();
|
||||
}
|
||||
|
||||
~Impl() {}
|
||||
|
||||
void ConnectSignals();
|
||||
|
||||
MediaManager* self_;
|
||||
|
||||
QMediaPlayer* mediaPlayer_;
|
||||
QAudioOutput* audioOutput_;
|
||||
};
|
||||
|
||||
MediaManager::MediaManager() : p(std::make_unique<Impl>()) {}
|
||||
MediaManager::MediaManager() : p(std::make_unique<Impl>(this)) {}
|
||||
MediaManager::~MediaManager() = default;
|
||||
|
||||
void MediaManager::Impl::ConnectSignals()
|
||||
{
|
||||
QObject::connect(audioOutput_,
|
||||
&QAudioOutput::deviceChanged,
|
||||
self_,
|
||||
[this]()
|
||||
{
|
||||
logger_->debug(
|
||||
"Audio device changed: {}",
|
||||
audioOutput_->device().description().toStdString());
|
||||
});
|
||||
|
||||
QObject::connect(mediaPlayer_,
|
||||
&QMediaPlayer::errorOccurred,
|
||||
self_,
|
||||
[](QMediaPlayer::Error error, const QString& errorString)
|
||||
{
|
||||
logger_->error("Error {}: {}",
|
||||
static_cast<int>(error),
|
||||
errorString.toStdString());
|
||||
});
|
||||
}
|
||||
|
||||
void MediaManager::Play(types::AudioFile media)
|
||||
{
|
||||
const std::string path = types::GetMediaPath(media);
|
||||
|
||||
logger_->debug("Playing audio: {}", path);
|
||||
|
||||
p->mediaPlayer_->setSource(QUrl(QString::fromStdString(path)));
|
||||
|
||||
QMetaObject::invokeMethod(p->mediaPlayer_, &QMediaPlayer::play);
|
||||
}
|
||||
|
||||
std::shared_ptr<MediaManager> MediaManager::Instance()
|
||||
{
|
||||
static std::weak_ptr<MediaManager> mediaManagerReference_ {};
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <scwx/qt/types/media_types.hpp>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <QObject>
|
||||
|
|
@ -20,6 +22,8 @@ public:
|
|||
explicit MediaManager();
|
||||
~MediaManager();
|
||||
|
||||
void Play(types::AudioFile media);
|
||||
|
||||
static std::shared_ptr<MediaManager> Instance();
|
||||
|
||||
private:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue