Терминальный проект КиберПлат [open source]
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.5KB

  1. /* @file Менеджер для работы со звуком. */
  2. // Qt
  3. #include <Common/QtHeadersBegin.h>
  4. #include <QtCore/QString>
  5. #include <QtCore/QSet>
  6. #include <QtCore/QDir>
  7. #include <QtCore/QFile>
  8. #include <Common/QtHeadersEnd.h>
  9. // Проект
  10. #include "System/IApplication.h"
  11. #include "System/SettingsConstants.h"
  12. #include "Services/ServiceNames.h"
  13. #include "Services/AudioService.h"
  14. //---------------------------------------------------------------------------
  15. namespace CAudioService
  16. {
  17. /// Название лога.
  18. const char LogName[] = "Interface";
  19. }
  20. //---------------------------------------------------------------------------
  21. AudioService * AudioService::instance(IApplication * aApplication)
  22. {
  23. return static_cast<AudioService *>(aApplication->getCore()->getService(CServices::AudioService));
  24. }
  25. //---------------------------------------------------------------------------
  26. AudioService::AudioService(IApplication * aApplication)
  27. : mApplication(aApplication),
  28. ILogable(CAudioService::LogName)
  29. {
  30. // Получаем директорию с файлами интерфейса из настроек.
  31. mInterfacePath = IApplication::toAbsolutePath(mApplication->getSettings().value(CSettings::InterfacePath).toString());
  32. }
  33. //---------------------------------------------------------------------------
  34. AudioService::~AudioService()
  35. {
  36. }
  37. //---------------------------------------------------------------------------
  38. bool AudioService::initialize()
  39. {
  40. mPlayer = QSharedPointer<QMediaPlayer>(new QMediaPlayer());
  41. connect(mPlayer.data(), SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(stateChanged(QMediaPlayer::State)));
  42. return true;
  43. }
  44. //------------------------------------------------------------------------------
  45. void AudioService::finishInitialize()
  46. {
  47. }
  48. //---------------------------------------------------------------------------
  49. bool AudioService::canShutdown()
  50. {
  51. return true;
  52. }
  53. //---------------------------------------------------------------------------
  54. bool AudioService::shutdown()
  55. {
  56. return true;
  57. }
  58. //---------------------------------------------------------------------------
  59. QString AudioService::getName() const
  60. {
  61. return CServices::AudioService;
  62. }
  63. //---------------------------------------------------------------------------
  64. const QSet<QString> & AudioService::getRequiredServices() const
  65. {
  66. static QSet<QString> requiredResources;
  67. return requiredResources;
  68. }
  69. //---------------------------------------------------------------------------
  70. QVariantMap AudioService::getParameters() const
  71. {
  72. return QVariantMap();
  73. }
  74. //---------------------------------------------------------------------------
  75. void AudioService::resetParameters(const QSet<QString> &)
  76. {
  77. }
  78. //---------------------------------------------------------------------------
  79. void AudioService::play(const QString & aFileName)
  80. {
  81. QString filePath = mInterfacePath + "/" + aFileName;
  82. if (QFile::exists(filePath))
  83. {
  84. if (mPlayer->state() != QMediaPlayer::StoppedState)
  85. {
  86. stop();
  87. }
  88. mPlayer->setMedia(QUrl::fromLocalFile(filePath));
  89. mPlayer->play();
  90. }
  91. else
  92. {
  93. stop();
  94. toLog(LogLevel::Warning, QString("Audio file %1 not found.").arg(aFileName));
  95. }
  96. }
  97. //---------------------------------------------------------------------------
  98. void AudioService::stop()
  99. {
  100. if (mPlayer)
  101. {
  102. mPlayer->stop();
  103. }
  104. }
  105. //---------------------------------------------------------------------------
  106. void AudioService::stateChanged(QMediaPlayer::State aState)
  107. {
  108. if (aState == QMediaPlayer::StoppedState)
  109. {
  110. }
  111. }
  112. //---------------------------------------------------------------------------