diff options
author | Roland Reichwein <mail@reichwein.it> | 2025-01-19 21:03:06 +0000 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2025-01-19 21:03:06 +0000 |
commit | 892888da03028cbf67bf005747902b9e3e5b9773 (patch) | |
tree | a5fd97f9c07c4d08a343e47d0ba2e68f4b5d296e /AudioPlayer.cpp | |
parent | 7f54b359f0261ae5b70f303a2080b13dd70cd0c2 (diff) |
Play WAV
Diffstat (limited to 'AudioPlayer.cpp')
-rw-r--r-- | AudioPlayer.cpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/AudioPlayer.cpp b/AudioPlayer.cpp new file mode 100644 index 0000000..e60ae1d --- /dev/null +++ b/AudioPlayer.cpp @@ -0,0 +1,43 @@ +#include "AudioPlayer.h" + +#include <iostream> + +#include <fmt/format.h> + +namespace bp = boost::process; +namespace fs = std::filesystem; + +AudioPlayer::AudioPlayer(const fs::path& dir, const std::string& filename): m_dir(dir), m_file(filename) +{ +} + +void AudioPlayer::start() +{ + if (m_child_audioplayer.valid() && m_child_audioplayer.running()) { + stop(); + } + if (!m_file.empty()) { + m_child_audioplayer = bp::child(fmt::format("aplay \"{}\"", (m_dir / m_file).string()).c_str());//, bp::std_out > bp::null); + if (!m_child_audioplayer.valid() || !m_child_audioplayer.running()) { + throw std::runtime_error("aplay not started"); + } + } +} + +void AudioPlayer::stop() +{ + // note:: m_child_audioplayer.terminate() would kill via SIGKILL + + if (m_child_audioplayer.valid()) { + int result = kill(m_child_audioplayer.native_handle(), SIGTERM); + if (result < 0) { + std::cerr << "Error in AudioPlayer::stop(): kill() unsuccessful\n"; + } + } +} + +void AudioPlayer::set_file(const std::string& filename) +{ + m_file = filename; +} + |