Audio Handler fix

This commit is contained in:
luiscuenca 2017-11-30 15:06:02 -07:00
parent b890a29b3a
commit e02e810399

View file

@ -121,10 +121,10 @@ uint64_t uvec2ToUint64(const uvec2& v) {
class AudioHandler : public QObject, QRunnable { class AudioHandler : public QObject, QRunnable {
Q_OBJECT Q_OBJECT
public: public:
AudioHandler(QObject* container, const QString& deviceName, int runDelayMs = 0, QObject* parent = nullptr) : QObject(parent) { AudioHandler(OffscreenQmlSurface* surface, const QString& deviceName, int runDelayMs = 0, QObject* parent = nullptr) : QObject(parent) {
_container = container;
_newTargetDevice = deviceName; _newTargetDevice = deviceName;
_runDelayMs = runDelayMs; _runDelayMs = runDelayMs;
_surface = surface;
setAutoDelete(true); setAutoDelete(true);
QThreadPool::globalInstance()->start(this); QThreadPool::globalInstance()->start(this);
} }
@ -139,7 +139,10 @@ public:
QThread::msleep(_runDelayMs); QThread::msleep(_runDelayMs);
} }
auto audioIO = DependencyManager::get<AudioClient>(); auto audioIO = DependencyManager::get<AudioClient>();
for (auto player : _container->findChildren<QMediaPlayer*>()) {
auto rootItem = _surface->getRootItem();
if (rootItem) {
for (auto player : rootItem->findChildren<QMediaPlayer*>()) {
auto mediaState = player->state(); auto mediaState = player->state();
QMediaService *svc = player->service(); QMediaService *svc = player->service();
if (nullptr == svc) { if (nullptr == svc) {
@ -166,16 +169,18 @@ public:
// this will reset it back to a paused state // this will reset it back to a paused state
if (mediaState == QMediaPlayer::State::PausedState) { if (mediaState == QMediaPlayer::State::PausedState) {
player->pause(); player->pause();
} else if (mediaState == QMediaPlayer::State::StoppedState) { }
else if (mediaState == QMediaPlayer::State::StoppedState) {
player->stop(); player->stop();
} }
} }
}
qDebug() << "QML Audio changed to " << _newTargetDevice; qDebug() << "QML Audio changed to " << _newTargetDevice;
} }
private: private:
QString _newTargetDevice; QString _newTargetDevice;
QObject* _container; OffscreenQmlSurface* _surface;
int _runDelayMs; int _runDelayMs;
}; };
@ -701,7 +706,7 @@ void OffscreenQmlSurface::forceQmlAudioOutputDeviceUpdate() {
QString deviceName = audioIO->getActiveAudioDevice(QAudio::AudioOutput).deviceName(); QString deviceName = audioIO->getActiveAudioDevice(QAudio::AudioOutput).deviceName();
int waitForAudioQmlMs = 200; int waitForAudioQmlMs = 200;
// The audio device need to be change using oth // The audio device need to be change using oth
new AudioHandler(_rootItem, deviceName, waitForAudioQmlMs); new AudioHandler(this, deviceName, waitForAudioQmlMs);
} }
} }