QMediaPlayer QML Binding Issue



  • I'd like to implement a QMediaPlayer video stream that will be shown on QML. I tried to extend the QMediaPlayer class and created below MyMediaPlayer class. I instantiate this class in my main.cpp file to show it on GUI. Even though there are no errors, the video isn't visible on the QML window. Where is my fault?

    mymediaplayer.cpp

    #include "MyMediaPlayer.h"
    
    MyMediaPlayer::MyMediaPlayer(QObject* parent, Flags flags): QMediaPlayer(parent, flags)
    {
    }
    
    void MyMediaPlayer::setVideoSurface(QAbstractVideoSurface* surface)
    {
        qDebug() << "Changing surface";
        m_surface = surface;
        setVideoOutput(m_surface);
    }
    
    QAbstractVideoSurface* MyMediaPlayer::getVideoSurface()
    {
        qDebug() << "Surface Got";
        return m_surface;
    }
    

    mymediaplayer.h

    #ifndef MYMEDIAPLAYER_H
    #define MYMEDIAPLAYER_H
    #include <QMediaPlayer>
    
    class MyMediaPlayer: public QMediaPlayer
    {
        Q_OBJECT
        Q_PROPERTY(QAbstractVideoSurface* videoSurface READ getVideoSurface WRITE setVideoSurface )
    
    public:
        MyMediaPlayer(QObject * parent = 0, Flags flags = 0);
    
    public slots:
        //virtual void play(const QString& strFile);
        void setVideoSurface(QAbstractVideoSurface* surface);
        QAbstractVideoSurface* getVideoSurface();
    
    private:
        QAbstractVideoSurface* m_surface;
    };
    
    #endif // MYMEDIAPLAYER_H
    

    main.cpp

     MyMediaPlayer* player = new MyMediaPlayer();
    
    
    engine.rootContext()->setContextProperty("mediaplayer", player);
    
    QQuickView view;
    view.engine()->rootContext()->setContextProperty("mediaplayer", player);
    
    player->setMedia(QUrl(QStringLiteral("qrc:/1.mp4")));
    qDebug() << "media set";
    player->play();
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    

    main.qml

    VideoOutput {
            z: 1000
            id: videooutput
            width: 320
            height: 240
            source: mediaplayer
        }

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.