Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QMediaplayer в потоке



  • Делаю синхронное проигрывание.

    Есть класс SndPlayer

    #include "sndplayer.h"
     
    SndPlayer::SndPlayer(QObject *parent, const QString &file) : QThread(parent)
    {
        fileName = file;
        mp = 0;
     
        moveToThread(this);
     
        connect(this, &QThread::started, this, &SndPlayer::open);
        connect(this, &QThread::finished, this, &SndPlayer::close);
     
        start();
    }
     
    SndPlayer::~SndPlayer()
    {
    }
     
    void SndPlayer::open()
    {
     
        mp = new QMediaPlayer(this);
     
        connect(mp, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), this, SLOT(mediaStatusChanged(QMediaPlayer::MediaStatus)), Qt::QueuedConnection);
     
        mp->setMedia(QUrl::fromLocalFile(fileName));
    }
     
    void SndPlayer::close()
    {
        if (mp)
        {
            delete mp;
            mp = 0;
        }
     
    }
     
    void SndPlayer::mediaStatusChanged(QMediaPlayer::MediaStatus status)
    {
        switch (status)
        {
            case QMediaPlayer::LoadedMedia:
                mp->play();
                break;
            case QMediaPlayer::InvalidMedia:
                exit(-1);
                break;
            case QMediaPlayer::EndOfMedia:
                exit(0);
                break;
        }
    }
    

    Вызов делаю так

    ```
    

    SndPlayer player(0, QCoreApplication::applicationDirPath()+"/test.ogg");
    player.wait();

    
    При запуске получаю сообщение
    

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QObject(0x7f63951264a0), parent's thread is QThread(0x249c800), current thread is SndPlayer(0x7fff16aa99b0)

    
    Как правильно создать QMediaPlayer ?


  • не нужно наследоваться от QThread

    вот классический пример:

    https://stackoverflow.com/questions/11033971/qt-thread-with-movetothread

    https://wiki.qt.io/QThreads_general_usage



  • #include "sndplayer.h"
    #include <QDebug>
    
    SndPlayer::SndPlayer(QObject *parent, const QString &file)
    {
        fileName = file;
    }
    
    SndPlayer::~SndPlayer(){
    
    }
    
    void SndPlayer::open(){
        mp = new QMediaPlayer;
    
        connect(mp, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), this, SLOT(mediaStatusChanged(QMediaPlayer::MediaStatus)), Qt::QueuedConnection);
    
        mp->setMedia(QUrl::fromLocalFile(fileName));
    }
    
    void SndPlayer::close()
    {
        if (mp)
        {
            delete mp;
            mp = 0;
        }
    }
    
    void SndPlayer::mediaStatusChanged(QMediaPlayer::MediaStatus status)
    {
        qDebug() << status;
        switch (status)
        {
            case QMediaPlayer::LoadedMedia:
                mp->play();
                break;
            case QMediaPlayer::InvalidMedia:
                exit(-1);
                break;
            case QMediaPlayer::EndOfMedia:
                exit(0);
                break;
    
            default:
                break;
        }
    }
    

    вызов делаю так
    ```
    QThread *thread = new QThread( );
    SndPlayer *pl= new SndPlayer (nullptr, QCoreApplication::applicationDirPath()+"/test.ogg");
    pl->moveToThread(thread);
    connect(thread, &QThread::started, pl, &SndPlayer::open);
    thread->start();
    thread->wait();

    
    Ничего не изменилось, получаю
    

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QObject(0x7f55616604a0), parent's thread is QThread(0x2520800), current thread is QThread(0x288e7c0)



  • Похоже это вообще невозможно.
    https://bugreports.qt.io/browse/QTBUG-47077


Log in to reply