Skip to content

Commit

Permalink
Sound module: replace SoundEffect with QmediaPlayer to support more f…
Browse files Browse the repository at this point in the history
…ile formats. Adjust the priority of "qt" sound system in autodetect
  • Loading branch information
ctrlaltca committed Jul 26, 2024
1 parent 60ca49c commit 070d7b8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
59 changes: 43 additions & 16 deletions src/modules/snd/libkvisnd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
#include "KviQString.h"

#ifdef COMPILE_QTMULTIMEDIA_SUPPORT
#include <QSoundEffect>
#include <QMediaPlayer>
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
#include <QAudioOutput>
#endif
#endif //!COMPILE_QTMULTIMEDIA_SUPPORT

#ifdef COMPILE_PHONON_SUPPORT
Expand Down Expand Up @@ -89,7 +92,10 @@ KviSoundPlayer::KviSoundPlayer()
#endif //!COMPILE_PHONON_SUPPORT

#ifdef COMPILE_QTMULTIMEDIA_SUPPORT
m_pSoundEffect = nullptr;
m_pMediaPlayer = nullptr;
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
m_pAudioOutput = nullptr;
#endif
#endif //COMPILE_QTMULTIMEDIA_SUPPORT

m_pLastUsedSoundPlayerEntry = nullptr;
Expand Down Expand Up @@ -138,8 +144,12 @@ KviSoundPlayer::~KviSoundPlayer()
#endif //COMPILE_PHONON_SUPPORT

#ifdef COMPILE_QTMULTIMEDIA_SUPPORT
if(m_pSoundEffect)
delete m_pSoundEffect;
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
if(m_pAudioOutput)
delete m_pAudioOutput;
#endif
if(m_pMediaPlayer)
delete m_pMediaPlayer;
#endif //!COMPILE_QTMULTIMEDIA_SUPPORT

g_pSoundPlayer = nullptr;
Expand Down Expand Up @@ -182,6 +192,17 @@ bool KviSoundPlayer::havePlayingSounds()
if(m_pThreadList)
if(m_pThreadList->count() > 0)
return true;

#ifdef COMPILE_QTMULTIMEDIA_SUPPORT
if(m_pMediaPlayer)
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
if(m_pMediaPlayer->state() == QMediaPlayer::PlayingState)
#else
if(m_pMediaPlayer->isPlaying())
#endif
return true;
#endif

#ifdef COMPILE_PHONON_SUPPORT
if(m_pPhononPlayer)
if(m_pPhononPlayer->state() == Phonon::PlayingState)
Expand Down Expand Up @@ -219,14 +240,14 @@ bool KviSoundPlayer::detectSoundSystem(QString & szSoundSystem)
szSoundSystem = "winmm";
return true;
#endif
#ifdef COMPILE_PHONON_SUPPORT
szSoundSystem = "phonon";
return true;
#endif //!COMPILE_PHONON_SUPPORT
#ifdef COMPILE_QTMULTIMEDIA_SUPPORT
szSoundSystem = "qt";
return true;
#endif
#ifdef COMPILE_PHONON_SUPPORT
szSoundSystem = "phonon";
return true;
#endif //!COMPILE_PHONON_SUPPORT
#ifdef COMPILE_OSS_SUPPORT
#ifdef COMPILE_AUDIOFILE_SUPPORT
szSoundSystem = "oss+audiofile";
Expand Down Expand Up @@ -336,21 +357,27 @@ bool KviSoundPlayer::playQt(const QString & szFileName)
if(isMuted())
return true;

if(!m_pSoundEffect)
m_pSoundEffect = new QSoundEffect;
m_pSoundEffect->setSource(QUrl::fromLocalFile(szFileName));
m_pSoundEffect->play();
if(!m_pMediaPlayer)
m_pMediaPlayer = new QMediaPlayer;
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
m_pAudioOutput = new QAudioOutput;
m_pMediaPlayer->setAudioOutput(m_pAudioOutput);
m_pMediaPlayer->setSource(QUrl::fromLocalFile(szFileName));
#else
m_pMediaPlayer->setMedia(QUrl::fromLocalFile(szFileName));
#endif
m_pMediaPlayer->play();
return true;
}

void KviSoundPlayer::cleanupQt()
{
if(!m_pSoundEffect)
if(!m_pMediaPlayer)
return;

m_pSoundEffect->stop();
delete m_pSoundEffect;
m_pSoundEffect = nullptr;
m_pMediaPlayer->stop();
delete m_pMediaPlayer;
m_pMediaPlayer = nullptr;
}
#endif

Expand Down
10 changes: 8 additions & 2 deletions src/modules/snd/libkvisnd.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ namespace Phonon
#endif //!COMPILE_PHONON_SUPPORT

#ifdef COMPILE_QTMULTIMEDIA_SUPPORT
class QSoundEffect;
class QMediaPlayer;
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
class QAudioOutput;
#endif
#endif

typedef bool (KviSoundPlayer::*SoundSystemPlayRoutine)(const QString & szFileName);
Expand Down Expand Up @@ -146,7 +149,10 @@ class KviSoundPlayer : public QObject
Phonon::MediaObject * m_pPhononPlayer;
#endif //!COMPILE_PHONON_SUPPORT
#ifdef COMPILE_QTMULTIMEDIA_SUPPORT
QSoundEffect * m_pSoundEffect;
QMediaPlayer * m_pMediaPlayer;
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
QAudioOutput * m_pAudioOutput;
#endif
#endif //!COMPILE_QTMULTIMEDIA_SUPPORT
KviSoundPlayerEntry * m_pLastUsedSoundPlayerEntry;

Expand Down

0 comments on commit 070d7b8

Please sign in to comment.