soundengine.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #ifndef INCLUDESOUNDENGINE_H
  2. #define INCLUDESOUNDENGINE_H
  3. #include <QObject>
  4. #include <QSound>
  5. #include <QMediaPlayer>
  6. #include <QMediaPlaylist>
  7. #include <memory>
  8. #include <set>
  9. class AsyncSound : public QObject, public std::enable_shared_from_this<AsyncSound> {
  10. Q_OBJECT
  11. public:
  12. AsyncSound() = delete;
  13. explicit AsyncSound(QString path);
  14. ~AsyncSound();
  15. QMediaPlayer* operator->() const;
  16. private:
  17. QMediaPlayer* player;
  18. private slots:
  19. void onStateChanged(const QMediaPlayer::State& state);
  20. signals:
  21. void finished(std::shared_ptr<AsyncSound>);
  22. };
  23. class SoundEngine : public QObject
  24. {
  25. Q_OBJECT
  26. public:
  27. static SoundEngine& getInstance() {
  28. static SoundEngine instance(nullptr);
  29. return instance;
  30. }
  31. QMediaPlayer& player() const;
  32. QMediaPlaylist& playlist() const;
  33. std::shared_ptr<AsyncSound> playAsync(QString path);
  34. private:
  35. SoundEngine() = delete;
  36. SoundEngine(const SoundEngine&) = delete;
  37. explicit SoundEngine(QObject *parent = nullptr);
  38. ~SoundEngine();
  39. private slots:
  40. void onAsyncSoundFinished(const std::shared_ptr<AsyncSound> &sound);
  41. private:
  42. QMediaPlayer *player_; // Аудио плеер
  43. QMediaPlaylist *playlist_; // Плейлист
  44. std::set<std::shared_ptr<AsyncSound>> async_sounds; // Список активных асинхронных звуков
  45. };
  46. #endif // INCLUDESOUNDENGINE_H