|
| 1 | +#ifndef AUDIOPLAYER_H |
| 2 | +#define AUDIOPLAYER_H |
| 3 | + |
| 4 | +#include <iostream> |
| 5 | +#include <thread> |
| 6 | +#include <mutex> |
| 7 | +#include <condition_variable> |
| 8 | +#include <queue> |
| 9 | +#include <string> |
| 10 | +#include <atomic> |
| 11 | +#include <array> |
| 12 | +#include <SFML/Audio.hpp> |
| 13 | +using namespace std::chrono_literals; |
| 14 | + |
| 15 | +class AudioPlayer { |
| 16 | +public: |
| 17 | + AudioPlayer() : stop{ false }, player_thread{ &AudioPlayer::playMusic, this } |
| 18 | + {} |
| 19 | + |
| 20 | + ~AudioPlayer() { |
| 21 | + // 等待队列中所有音乐播放完毕 |
| 22 | + while (!audio_queue.empty()) { |
| 23 | + std::this_thread::sleep_for(50ms); |
| 24 | + } |
| 25 | + stop = true; |
| 26 | + cond.notify_all(); |
| 27 | + if (player_thread.joinable()) { |
| 28 | + player_thread.join(); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + void addAudioPath(const std::string& path) { |
| 33 | + std::lock_guard<std::mutex> lock{ mtx }; |
| 34 | + audio_queue.push(path); |
| 35 | + cond.notify_one(); // 通知线程新的音频 |
| 36 | + } |
| 37 | + |
| 38 | +private: |
| 39 | + void playMusic() { |
| 40 | + while (!stop) { |
| 41 | + std::string path; |
| 42 | + { |
| 43 | + std::unique_lock<std::mutex> lock{ mtx }; |
| 44 | + cond.wait(lock, [this] { return !audio_queue.empty() || stop; }); |
| 45 | + |
| 46 | + if (audio_queue.empty()) return; // 防止在对象为空时析构出错 |
| 47 | + |
| 48 | + path = audio_queue.front(); |
| 49 | + audio_queue.pop(); |
| 50 | + } |
| 51 | + |
| 52 | + if (!music.openFromFile(path)) { |
| 53 | + std::cerr << "无法加载音频文件: " << path << std::endl; |
| 54 | + continue; // 继续播放下一个音频 |
| 55 | + } |
| 56 | + |
| 57 | + music.play(); |
| 58 | + |
| 59 | + // 等待音频播放完毕 |
| 60 | + while (music.getStatus() == sf::SoundSource::Playing) { |
| 61 | + sf::sleep(sf::seconds(0.1f)); // sleep 避免忙等占用CPU |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + std::atomic<bool> stop; |
| 67 | + std::thread player_thread; |
| 68 | + std::mutex mtx; |
| 69 | + std::condition_variable cond; |
| 70 | + std::queue<std::string> audio_queue; |
| 71 | + sf::Music music; |
| 72 | + |
| 73 | +public: |
| 74 | + static constexpr std::array soundResources{ |
| 75 | + "./sound/01初始化失败.ogg", |
| 76 | + "./sound/02初始化成功.ogg", |
| 77 | + "./sound/03试剂不足,请添加.ogg", |
| 78 | + "./sound/04试剂已失效,请更新.ogg", |
| 79 | + "./sound/05清洗液不足,请添加.ogg", |
| 80 | + "./sound/06废液桶即将装满,请及时清空.ogg", |
| 81 | + "./sound/07废料箱即将装满,请及时清空.ogg", |
| 82 | + "./sound/08激发液A液不足,请添加.ogg", |
| 83 | + "./sound/09激发液B液不足,请添加.ogg", |
| 84 | + "./sound/10反应杯不足,请添加.ogg", |
| 85 | + "./sound/11检测全部完成.ogg" |
| 86 | + }; |
| 87 | + enum SoundIndex { |
| 88 | + InitializationFailed, |
| 89 | + InitializationSuccessful, |
| 90 | + ReagentInsufficient, |
| 91 | + ReagentExpired, |
| 92 | + CleaningAgentInsufficient, |
| 93 | + WasteBinAlmostFull, |
| 94 | + WasteContainerAlmostFull, |
| 95 | + LiquidAInsufficient, |
| 96 | + LiquidBInsufficient, |
| 97 | + ReactionCupInsufficient, |
| 98 | + DetectionCompleted, |
| 99 | + SoundCount // 总音频数量,用于计数 |
| 100 | + }; |
| 101 | +}; |
| 102 | + |
| 103 | +#endif // AUDIOPLAYER_H |
0 commit comments