Russian Qt Forum

Qt => Многопоточное программирование, процессы => Тема начата: Fregloin от Ноябрь 07, 2012, 11:40



Название: Воспроизведение звука в Phonon в другом потоке
Отправлено: Fregloin от Ноябрь 07, 2012, 11:40
Добрый день. При проигрывании звука в основном потоке иногда заметны подтормаживания при отображении графики (GraphicsView).
Решил перенсти проигрывание в отдельный поток.
Код:
class CArmProject : public QObject
{
    Q_OBJECT

    CSoundServer                 *  fsoundServer; //обертка над фононом
    QThread                         fsoundThread; //поток в который хочу перенетсти звук
}

CArmProject::CArmProject(QMainWindow * mainWindow):
    QObject(NULL)
{
    fsoundServer = new CSoundServer; //создаю звуковой сервак в куче без родителя
    fsoundServer->moveToThread(&fsoundThread); //перевожу его в поток
    fsoundThread.start(); //запускаю поток
}

ArmProject::~CArmProject()
{
    if(fsoundThread.isRunning())
    {
        fsoundThread.quit();
        fsoundThread.wait();
    }
    fsoundServer->deleteLater();
}

связываю сигналы по которым нужно играть звуки

Код:
void CArmProject::relinkAlerts()
{
    foreach(CGenericRailObjectStorage * currentStorage, fstorageMap->values())    //проходимся по всем ЭЦ в проекте
    {
        foreach (CRailObjectProvider * currentProvider, currentStorage->providers().values())    //проходимся по всем провайдерам
        {
            foreach (CRailObject * currentObject, currentProvider->objects().values())  //и по всем объектам каждого провайдера
            {
                connect(currentObject,SIGNAL(soundAlert(const QString &)),fsoundServer,SLOT(playAlert(const QString &)));
            }
        }
    }
}

вот код проигрывателя звуков
Код:
class CSoundServer : public QObject
{
    Q_OBJECT

    Phonon::MediaObject             *   mediaObject;
    Phonon::AudioOutput             *   audioOutput;                    //устройсто вывода звука
    QString ffilePath;                                                  //путь к звуковым файлам
    QHash<QString,QString>              falertLinkToFileHash;           //хеш ссылок objectId:alertRole -> fileName

public:

    explicit    CSoundServer(QObject *parent = 0);
                ~CSoundServer();

    void        setAlerts(CGenericRailObjectStorageMap *storageMap, const CSoundAlertItemList & alerts);
    const   QString &   filePath()  const;

public slots:

    void        playFile(const QString & fileName);
    void        playAlert(const QString & alertRole);   //проиграть алерт
    void        clear();

};

Код:
void CSoundServer::playFile(const QString & fileName)
{
    mediaObject->setCurrentSource(Phonon::MediaSource(filePath()+"/"+fileName));
    mediaObject->play();
}

void CSoundServer::playAlert(const QString &alertRole)
{
    CRailObject *   railObject = qobject_cast<CRailObject*>(sender());
    if(railObject)
    {
        QString linkId = linkTemplate.arg(railObject->parentStorage()->id()).arg(railObject->objectId()).arg(alertRole);
        QString fileName = falertLinkToFileHash.value(linkId);
        //qDebug("alert: %s",qPrintable(linkId));
        if(!fileName.isEmpty())
        {
            playFile(fileName);
        }
    }
}

Звук воспроизводится, но в консоли ругается
Код:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x7f135400d500)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x7f135400d500)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x7f135400d500)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QApplication(0x7fff6df795c0), parent's thread is QThread(0x1939d40), current thread is QThread(0x1b36de0)
Читал ветки касательно moveToThread, но почему то все равно получаю это :(