#include "AudioPlayer.h" #include #include 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; }