From ab4efe774dfef0c65ea5b65ae5b3d61838b3853c Mon Sep 17 00:00:00 2001 From: Wescoeur Date: Fri, 16 Jun 2017 00:10:17 +0200 Subject: [PATCH] fix(SoundPlayer): handle ringer device change --- src/components/sound-player/SoundPlayer.cpp | 48 +++++++++++++++------ src/components/sound-player/SoundPlayer.hpp | 5 +++ 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/components/sound-player/SoundPlayer.cpp b/src/components/sound-player/SoundPlayer.cpp index 77813bfa4..48ab5f3fc 100644 --- a/src/components/sound-player/SoundPlayer.cpp +++ b/src/components/sound-player/SoundPlayer.cpp @@ -64,8 +64,7 @@ SoundPlayer::SoundPlayer (QObject *parent) : QObject(parent) { mHandlers = make_shared(this); - mInternalPlayer = CoreManager::getInstance()->getCore()->createLocalPlayer("", "", nullptr); - mInternalPlayer->setListener(mHandlers); + buildInternalPlayer(); } SoundPlayer::~SoundPlayer () { @@ -117,16 +116,7 @@ void SoundPlayer::play () { } void SoundPlayer::stop () { - if (mPlaybackState == SoundPlayer::StoppedState) - return; - - mForceCloseTimer->stop(); - mPlaybackState = SoundPlayer::StoppedState; - - mInternalPlayer->close(); - - emit stopped(); - emit playbackStateChanged(mPlaybackState); + stop(false); } // ----------------------------------------------------------------------------- @@ -143,6 +133,40 @@ int SoundPlayer::getPosition () const { // ----------------------------------------------------------------------------- +void SoundPlayer::buildInternalPlayer () { + CoreManager *coreManager = CoreManager::getInstance(); + SettingsModel *settingsModel = coreManager->getSettingsModel(); + + mInternalPlayer = coreManager->getCore()->createLocalPlayer( + ::Utils::appStringToCoreString(settingsModel->getRingerDevice()), "", nullptr + ); + mInternalPlayer->setListener(mHandlers); + + QObject::connect(settingsModel, &SettingsModel::ringerDeviceChanged, this, [this] { + rebuildInternalPlayer(); + }); +} + +void SoundPlayer::rebuildInternalPlayer () { + stop(true); + buildInternalPlayer(); +} + +void SoundPlayer::stop (bool force) { + if (mPlaybackState == SoundPlayer::StoppedState && !force) + return; + + mForceCloseTimer->stop(); + mPlaybackState = SoundPlayer::StoppedState; + + mInternalPlayer->close(); + + emit stopped(); + emit playbackStateChanged(mPlaybackState); +} + +// ----------------------------------------------------------------------------- + void SoundPlayer::handleEof () { mForceCloseMutex.lock(); diff --git a/src/components/sound-player/SoundPlayer.hpp b/src/components/sound-player/SoundPlayer.hpp index 18b7f15c3..88503281e 100644 --- a/src/components/sound-player/SoundPlayer.hpp +++ b/src/components/sound-player/SoundPlayer.hpp @@ -76,6 +76,11 @@ signals: void playbackStateChanged (PlaybackState playbackState); private: + void buildInternalPlayer (); + void rebuildInternalPlayer (); + + void stop (bool force); + void handleEof (); void setError (const QString &message);