summaryrefslogtreecommitdiffhomepage
path: root/AudioPlayer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'AudioPlayer.cpp')
-rw-r--r--AudioPlayer.cpp43
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;
+}
+