Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QMediaPlayer QML Binding Issue
Qt 6.11 is out! See what's new in the release blog

QMediaPlayer QML Binding Issue

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 432 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mkocabas
    wrote on last edited by
    #1

    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
        }
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved