Media player not working for multiple videos



  • This is my second post for this question . My code doesn't work at all . I don't know where I did mistakes . The problem is that when I load for multiple videos , I can only control last video player's play pause action and not the others. And I can't play other videos .Here is my code . By the way I got this code by combining from my last post with the help of @jslum.

    //header

    namespace Ui {
    class MainWindow;
    }
    QT_BEGIN_NAMESPACE
    class QAbstractButton;
    class QSlider;
    class QGraphicsVideoItem;
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
        bool isPlayerAvailable() const;
        QSize sizeHint() const override;
    
    public slots:
    
        void play();
    
    private slots:
        void mediaStateChanged(QMediaPlayer::State state);
        void positionChanged(qint64 position);
        void durationChanged(qint64 duration);
        void setPosition(int position);
    
    
    private:
        Ui::MainWindow *ui;
        //QMediaPlayer *player = nullptr;
        QVector<QMediaPlayer*> player;
        QMediaPlayer *mediaPlayer = nullptr;
        QMediaPlaylist *playlist = nullptr;
        QGraphicsVideoItem *videoItem = nullptr;
        QAbstractButton *m_playButton = nullptr;
        QSlider *m_positionSlider = nullptr;
    
    };
    
    

    //mainwindow.cpp

    ui->listWidget->setFlow(QListView::LeftToRight);
        ui->listWidget->setMinimumSize(760,350);
        ui->listWidget->setGridSize(QSize(360, 360));
        ui->listWidget->setResizeMode(QListView::Adjust);
        ui->listWidget->setViewMode(QListView::ListMode);
        ui->listWidget->setWrapping(true);
    
        QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home",
    
                                                           QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
    
        directory.setNameFilters({"*.mp4" , "*.avi" , "*.flv" , "*.mwv"});
    
        for(const QFileInfo & finfo: directory.entryInfoList()){
    
            //player = new QMediaPlayer(this,QMediaPlayer::VideoSurface);
            //player = new QVector<QMediaPlayer*>();
            //playlist = new QMediaPlaylist;
            // playlist->addMedia(QUrl::fromLocalFile(finfo.absoluteFilePath()));
            mediaPlayer = new QMediaPlayer();
            mediaPlayer->setMedia(QUrl::fromLocalFile(finfo.absoluteFilePath()));
            // mediaPlayer->setMedia(playlist);
            videoItem = new QGraphicsVideoItem;
            //videoItem->setSize(QSize(320,240));
            QGraphicsScene *scene = new QGraphicsScene(this);
            QGraphicsView *graphicsView = new QGraphicsView(scene);
            scene->addItem(videoItem);
    
            mediaPlayer->setVideoOutput(videoItem);
            player.append(mediaPlayer);
    
    
            m_playButton = new QPushButton;
            m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
    
    
            m_positionSlider = new QSlider(Qt::Horizontal);
            m_positionSlider->setRange(0, 0);
    
    
            auto    item = new QListWidgetItem("", ui->listWidget);
            auto    widget = new QWidget;
            auto    label = new QLabel(finfo.fileName());
            auto    vb = new QVBoxLayout;
    
            QBoxLayout *controlLayout = new QHBoxLayout;
            controlLayout->setMargin(0);
            controlLayout->addWidget(m_playButton);
            controlLayout->addWidget(m_positionSlider);
    
            vb->addWidget(label,1);
            vb->addWidget(graphicsView,9);
            vb->addLayout(controlLayout);
            widget->setLayout(vb);
            widget->setMinimumSize(340, 340);
            ui->listWidget->setItemWidget(item,widget);
    
    
        }
        connect(m_playButton, &QAbstractButton::clicked, this, &MainWindow::play);
        connect(m_positionSlider, &QAbstractSlider::sliderMoved,this, &MainWindow::setPosition);
    
        connect(mediaPlayer, &QMediaPlayer::stateChanged, this, &MainWindow::mediaStateChanged);
        connect(mediaPlayer, &QMediaPlayer::positionChanged, this, &MainWindow::positionChanged);
        connect(mediaPlayer, &QMediaPlayer::durationChanged, this, &MainWindow::durationChanged);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    QSize MainWindow::sizeHint() const
    {
        return (videoItem->size() * qreal(3) / qreal(2)).toSize();
    }
    
    bool MainWindow::isPlayerAvailable() const
    {
        return mediaPlayer->isAvailable();
    }
    
    
    
    void MainWindow::play()
    {
        switch (mediaPlayer->state()) {
        case QMediaPlayer::PlayingState:
            mediaPlayer->pause();
            break;
        default:
            mediaPlayer->play();
            break;
        }
    }
    
    void MainWindow::mediaStateChanged(QMediaPlayer::State state)
    {
        switch(state) {
        case QMediaPlayer::PlayingState:
            m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
            break;
        default:
            m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
            break;
        }
    }
    
    void MainWindow::positionChanged(qint64 position)
    {
        m_positionSlider->setValue(position);
    }
    
    void MainWindow::durationChanged(qint64 duration)
    {
        m_positionSlider->setRange(0, duration);
    }
    
    void MainWindow::setPosition(int position)
    {
        mediaPlayer->setPosition(position);
    }
    

  • Lifetime Qt Champion

    Hi,

    Because in your slot you are using the mediaPlayer member variable which is basically the last media player you created.

    If you want to control them all at the same time, you have to iterate through your vector of media players and apply what you want to do to all of them.


  • Moderators

    @Kinesis Please do not ask same question in a new thread.
    Here https://forum.qt.io/topic/92058/multiple-media-player-for-each-video your question was already answered, but you apparently did not do as suggested. This is what I suggested:

    QMediaPlayer *mediaPlayer = new QMediaPlayer();
    mediaPlayer->setMedia(QUrl::fromLocalFile(finfo.absoluteFilePath()));
    ...
    mediaPlayer->setVideoOutput(m_videoItem);
    m_mediaPlayer.append(mediaPlayer);
    

    So, here mediaPlayer is a not a MEMBER variable (as @SGaist suggested above).
    And I don't see the list to store the pointers to media players - did you actually try what was suggested?



  • @jsulm
    I tried as u said but I doesn't work . It won't even play a video . That's why I am asking for the second time



  • @Kinesis
    You should post your answers, further issues, etc. regarding your thread in the same thread. That is what threads are for after all :-).



  • @jsulm
    Here is waht I tried as u said . I placed "mediaPlayer" at the start of the program because I need it to use in slots. But it doesn't work .

    QMediaPlayer *mediaPlayer = new QMediaPlayer();
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        ui->listWidget->setFlow(QListView::LeftToRight);
        ui->listWidget->setMinimumSize(760,350);
        ui->listWidget->setGridSize(QSize(360, 360));
        ui->listWidget->setResizeMode(QListView::Adjust);
        ui->listWidget->setViewMode(QListView::ListMode);
        ui->listWidget->setWrapping(true);
    
        QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home",
    
                                                           QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
    
        directory.setNameFilters({"*.mp4" , "*.avi" , "*.flv" , "*.mwv"});
    
        for(const QFileInfo & finfo: directory.entryInfoList()){
    
            //player = new QMediaPlayer(this,QMediaPlayer::VideoSurface);
            //player = new QVector<QMediaPlayer*>();
            //playlist = new QMediaPlaylist;
            // playlist->addMedia(QUrl::fromLocalFile(finfo.absoluteFilePath()));
            //mediaPlayer = new QMediaPlayer();
            mediaPlayer->setMedia(QUrl::fromLocalFile(finfo.absoluteFilePath()));
            // mediaPlayer->setMedia(playlist);
            videoItem = new QGraphicsVideoItem;
            //videoItem->setSize(QSize(320,240));
            QGraphicsScene *scene = new QGraphicsScene(this);
            QGraphicsView *graphicsView = new QGraphicsView(scene);
            scene->addItem(videoItem);
    
            mediaPlayer->setVideoOutput(videoItem);
            player.append(mediaPlayer);
    
    
            m_playButton = new QPushButton;
            m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
    
    
            m_positionSlider = new QSlider(Qt::Horizontal);
            m_positionSlider->setRange(0, 0);
            connect(m_playButton, &QAbstractButton::clicked, this, &MainWindow::play);
            connect(m_positionSlider, &QAbstractSlider::sliderMoved,this, &MainWindow::setPosition);
    
            connect(mediaPlayer, &QMediaPlayer::stateChanged, this, &MainWindow::mediaStateChanged);
            connect(mediaPlayer, &QMediaPlayer::positionChanged, this, &MainWindow::positionChanged);
            connect(mediaPlayer, &QMediaPlayer::durationChanged, this, &MainWindow::durationChanged);
    
            auto    item = new QListWidgetItem("", ui->listWidget);
            auto    widget = new QWidget;
            auto    label = new QLabel(finfo.fileName());
            auto    vb = new QVBoxLayout;
    
            QBoxLayout *controlLayout = new QHBoxLayout;
            controlLayout->setMargin(0);
            controlLayout->addWidget(m_playButton);
            controlLayout->addWidget(m_positionSlider);
    
            vb->addWidget(label,1);
            vb->addWidget(graphicsView,9);
            vb->addLayout(controlLayout);
            widget->setLayout(vb);
            widget->setMinimumSize(340, 340);
            ui->listWidget->setItemWidget(item,widget);
    
    
        }
    
    }
    
    

  • Moderators

    @Kinesis Sorry, but this is not what I suggested, not at all. Why do you use SAME QMediaPlayer for all videos?
    What you are currently doing is: you're using the SAME QMediaPlayer for all the video files! This is not going to do what you expect it to do. And I don't see any need to have mediaPlayer as global variable.
    I post the code here once more:

    for(const QFileInfo & finfo: directory.entryInfoList()){
        QMediaPlayer *mediaPlayer = new QMediaPlayer(); // You need to create one for each video as you want to show all at the same time, right?
        mediaPlayer->setMedia(QUrl::fromLocalFile(finfo.absoluteFilePath()));
    ...
        mediaPlayer->setVideoOutput(m_videoItem);
        m_mediaPlayer.append(mediaPlayer);
    


  • @jsulm
    AHH , OK .
    But what should I do about slots . I am using slots to play and pause functions . Slots need mediaPlayer to link with actions. In that case *mediaPlayer is not member variable . So I can't use *mediaPlayer in slots. What should I do?


  • Moderators

    @Kinesis What is the problem with connect()? You have the pointer to QMediaPlayer inside the loop where you call connect():

    for(const QFileInfo & finfo: directory.entryInfoList()){
        QMediaPlayer *mediaPlayer = new QMediaPlayer(); // You need to create one for each video as you want to show all at the same time, right?
        mediaPlayer->setMedia(QUrl::fromLocalFile(finfo.absoluteFilePath()));
        connect(mediaPlayer, &QMediaPlayer::stateChanged, this, &MainWindow::mediaStateChanged);
    ...
        mediaPlayer->setVideoOutput(m_videoItem);
        m_mediaPlayer.append(mediaPlayer);
    
    


  • @jsulm
    It keeps showing "error: use of undeclared identifier 'mediaPlayer' " like in this slot

    void MainWindow::play()
    {
        switch (mediaPlayer->state()) {
        case QMediaPlayer::PlayingState:
            mediaPlayer->pause();
            break;
        default:
            mediaPlayer->play();
            break;
        }
    }
    

  • Moderators

    @Kinesis And how is this related to connect() call?
    You need to understand: you have MANY media players, not just one. You put all of them in a list. That means if user presses play you need to get the one media player user wants to play. And then use this player.
    Something like:

    void MainWindow::play()
    {
        QMediaPlayer *mediaPlayer = player[currentIndex];
        switch (mediaPlayer->state()) {
        case QMediaPlayer::PlayingState:
            mediaPlayer->pause();
            break;
        default:
            mediaPlayer->play();
            break;
        }
    }
    

    I don't know how to determine the value of currentIndex, as I don't know much about your app.



  • @jsulm
    Its seem like it will work this time. How can I determine the value of currentIndex?


  • Moderators

    @Kinesis "How can I determine the value of currentIndex?" - as I said - I don't know.
    How does the user select the video to play?



  • @jsulm
    I found that the buttons are linking each other . If I press the play button of 1st video It also play second video . How can I fix that .


  • Moderators

    @Kinesis You could use lambda (C++11: https://en.cppreference.com/w/cpp/language/lambda):

    connect(m_playButton, &QAbstractButton::clicked, [mediaPlayer]() {
    switch (mediaPlayer->state()) {
    case QMediaPlayer::PlayingState:
        mediaPlayer->pause();
        break;
    default:
        mediaPlayer->play();
        break;
    }
    });
    


  • @jsulm
    Thanks . now the buttons are not linked any more. But my first problem is back . I can't play all videos . I can just play 1 video(last video) when I load 2 videos , I can't play any videos when i load more than 2 . I don't know what to do now . I am really stuck at this stage for 4 days. I really appreciate your help for helping until now.


  • Lifetime Qt Champion

    Are your buttons supposed to control all video players at once ? If so, then do it as I suggested and go it iterate through the vector of players and control them all.

    If you want to control each player separately then you have have to create as many buttons as players.


  • Moderators

    @Kinesis said in Media player not working for multiple videos:

    I can just play 1 video(last video)

    Sounds like you still use global mediaPlayer - do you?
    Did you change the code as I suggested?

    for(const QFileInfo & finfo: directory.entryInfoList()){
        QMediaPlayer *mediaPlayer = new QMediaPlayer(); // DO NOT USE GLOBAL mediaPlayer!
        mediaPlayer->setMedia(QUrl::fromLocalFile(finfo.absoluteFilePath()));
    ...
        connect(m_playButton, &QAbstractButton::clicked, [mediaPlayer]() {
            switch (mediaPlayer->state()) {
            case QMediaPlayer::PlayingState:
                mediaPlayer->pause();
                break;
            default:
                mediaPlayer->play();
                break;
        }
        });
    ...
        mediaPlayer->setVideoOutput(m_videoItem);
        m_mediaPlayer.append(mediaPlayer);
    

  • Moderators

    @SGaist He creates one button for each video



  • @jsulm
    I am not using mediaPlayer as global variable . Here is my updated code

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        ui->listWidget->setFlow(QListView::LeftToRight);
        ui->listWidget->setMinimumSize(760,350);
        ui->listWidget->setGridSize(QSize(360, 360));
        ui->listWidget->setResizeMode(QListView::Adjust);
        ui->listWidget->setViewMode(QListView::ListMode);
        ui->listWidget->setWrapping(true);
    
        QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home",
    
                                                           QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
    
        directory.setNameFilters({"*.mp4" , "*.avi" , "*.flv" , "*.mwv"});
    
        for(const QFileInfo & finfo: directory.entryInfoList()){
            QMediaPlayer *mediaPlayer = new QMediaPlayer();
            //player = new QMediaPlayer(this,QMediaPlayer::VideoSurface);
            //player = new QVector<QMediaPlayer*>();
            //playlist = new QMediaPlaylist;
            // playlist->addMedia(QUrl::fromLocalFile(finfo.absoluteFilePath()));
            //mediaPlayer = new QMediaPlayer();
            mediaPlayer->setMedia(QUrl::fromLocalFile(finfo.absoluteFilePath()));
            // mediaPlayer->setMedia(playlist);
            videoItem = new QGraphicsVideoItem;
            //videoItem->setSize(QSize(320,240));
            QGraphicsScene *scene = new QGraphicsScene(this);
            QGraphicsView *graphicsView = new QGraphicsView(scene);
            scene->addItem(videoItem);
    
            mediaPlayer->setVideoOutput(videoItem);
            player.append(mediaPlayer);
    
            connect(mediaPlayer, &QMediaPlayer::stateChanged, this, &MainWindow::mediaStateChanged);
            connect(mediaPlayer, &QMediaPlayer::positionChanged, this, &MainWindow::positionChanged);
            connect(mediaPlayer, &QMediaPlayer::durationChanged, this, &MainWindow::durationChanged);
    
            m_playButton = new QPushButton;
            m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
    
    
            m_positionSlider = new QSlider(Qt::Horizontal);
            m_positionSlider->setRange(0, 0);
            connect(m_playButton, &QAbstractButton::clicked, this, &MainWindow::play);
            connect(m_positionSlider, &QAbstractSlider::sliderMoved,this, &MainWindow::setPosition);
    
    
    
            auto    item = new QListWidgetItem("", ui->listWidget);
            auto    widget = new QWidget;
            auto    label = new QLabel(finfo.fileName());
            auto    vb = new QVBoxLayout;
    
            QBoxLayout *controlLayout = new QHBoxLayout;
            controlLayout->setMargin(0);
            controlLayout->addWidget(m_playButton);
            controlLayout->addWidget(m_positionSlider);
    
            vb->addWidget(label,1);
            vb->addWidget(graphicsView,9);
            vb->addLayout(controlLayout);
            widget->setLayout(vb);
            widget->setMinimumSize(340, 340);
            ui->listWidget->setItemWidget(item,widget);
    
    
        }
        
    
    
    }
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    QSize MainWindow::sizeHint() const
    {
        return (videoItem->size() * qreal(3) / qreal(2)).toSize();
    }
    
    bool MainWindow::isPlayerAvailable() const
    {   QMediaPlayer *mediaPlayer = player[1];
        return mediaPlayer->isAvailable();
    }
    
    
    
    void MainWindow::play()
    {
        QMediaPlayer *mediaPlayer = player[1];
        connect(m_playButton, &QAbstractButton::clicked, [mediaPlayer]() {
        switch (mediaPlayer->state()) {
        case QMediaPlayer::PlayingState:
            mediaPlayer->pause();
            break;
        default:
            mediaPlayer->play();
            break;
        }
    });
    }
    
    void MainWindow::mediaStateChanged(QMediaPlayer::State state)
    {
        switch(state) {
        case QMediaPlayer::PlayingState:
            m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
            break;
        default:
            m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
            break;
        }
    }
    
    void MainWindow::positionChanged(qint64 position)
    {
        m_positionSlider->setValue(position);
    }
    
    void MainWindow::durationChanged(qint64 duration)
    {
        m_positionSlider->setRange(0, duration);
    }
    
    void MainWindow::setPosition(int position)
    {
        QMediaPlayer *mediaPlayer = player[1];
        mediaPlayer->setPosition(position);
    }
    


  • @Kinesis
    your still not doing what @jsulm suggested.

    this

    void MainWindow::play()
    {
        QMediaPlayer *mediaPlayer = player[1];
        connect(m_playButton, &QAbstractButton::clicked, [mediaPlayer]() {
        switch (mediaPlayer->state()) {
        case QMediaPlayer::PlayingState:
            mediaPlayer->pause();
            break;
        default:
            mediaPlayer->play();
            break;
        }
    });
    }
    

    is wrong and at the wrong place

    delete that function all together

    and in your loop replace

    connect(m_playButton, &QAbstractButton::clicked, this, &MainWindow::play);
    

    with

    @jsulm said in Media player not working for multiple videos:

    connect(m_playButton, &QAbstractButton::clicked, [mediaPlayer]() {
        switch (mediaPlayer->state()) {
        case QMediaPlayer::PlayingState:
            mediaPlayer->pause();
            break;
        default:
            mediaPlayer->play();
            break;
    }
    });

  • Moderators

    @Kinesis You should think about the code you're writing and you should read what I'm writing more carefully. To be honest I'm tired of writing code for you and then see that you're not trying it but doing something else. The code you posted now is NOT what I suggested (again).

    void MainWindow::play()
    {
        QMediaPlayer *mediaPlayer = player[1];
    

    Do you really think this code can work correctly? You always try to use SECOND player! How is it going to work?!
    I post here my code once more, but for the last time:

    for(const QFileInfo & finfo: directory.entryInfoList()){
        QMediaPlayer *mediaPlayer = new QMediaPlayer(); // DO NOT USE GLOBAL mediaPlayer!
        mediaPlayer->setMedia(QUrl::fromLocalFile(finfo.absoluteFilePath()));
    ...
        connect(m_playButton, &QAbstractButton::clicked, [mediaPlayer]() {
            switch (mediaPlayer->state()) {
            case QMediaPlayer::PlayingState:
                mediaPlayer->pause();
                break;
            default:
                mediaPlayer->play();
                break;
        }
        });
    ...
        mediaPlayer->setVideoOutput(m_videoItem);
        m_mediaPlayer.append(mediaPlayer);
    


  • @jsulm
    I got it now . Sorry for wasting your time . Now I can play both videos . Do I need to replace the button state change into loop? Because button state(Icon) is not changing correctly and linking with other media players.


  • Moderators

    @Kinesis You need to change all your other slots for mediaPlayer in the same way as play():

    connect(mediaPlayer, &QMediaPlayer::stateChanged, [m_playButton, this](QMediaPlayer::State state) {
        switch(state) {
        case QMediaPlayer::PlayingState:
            m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
            break;
        default:
            m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
            break;
        }
    });
    


  • @jsulm
    I got a lambda issue for "m_playButton" . The message is " m_play Button in capture list does not name a variable.


  • Moderators

    @Kinesis Please show your code...
    You need to put the code I shown AFTER you created m_playButton.


  • Qt Champions 2017

    @Kinesis I will tell you my insights from reading this thread: you should really take a C++ book and start with simple examples.

    you are lacking simple programming knowledge, sorry. most of your problems are not related to Qt.

    So please, start learning C++. it will help you a lot.



  • @aha_1980
    Thanks for your suggestion . I am learning c++ now . I also like to try writing UI that's why I am approaching QT .



  • @jsulm
    Here is the code

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        
        ui->listWidget->setFlow(QListView::LeftToRight);
        ui->listWidget->setMinimumSize(1200,350);
        ui->listWidget->setGridSize(QSize(360, 360));
        ui->listWidget->setResizeMode(QListView::Adjust);
        ui->listWidget->setViewMode(QListView::ListMode);
        ui->listWidget->setWrapping(true);
        
        QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home",
                                                           
                                                           QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
        
        directory.setNameFilters({"*.mp4" , "*.avi" , "*.flv" , "*.mwv"});
        
        for(const QFileInfo & finfo: directory.entryInfoList()){
            QMediaPlayer *mediaPlayer = new QMediaPlayer();
            mediaPlayer->setMedia(QUrl::fromLocalFile(finfo.absoluteFilePath()));
            // mediaPlayer->setMedia(playlist);
            videoItem = new QGraphicsVideoItem;
            videoItem->setSize(QSize(320,240));
            QGraphicsScene *scene = new QGraphicsScene(this);
            QGraphicsView *graphicsView = new QGraphicsView(scene);
            
            
            
            
            mediaPlayer->setVideoOutput(videoItem);
            player.append(mediaPlayer);
            scene->addItem(videoItem);
            
            connect(mediaPlayer, &QMediaPlayer::stateChanged, this, &MainWindow::mediaStateChanged);
            
            connect(mediaPlayer, &QMediaPlayer::positionChanged, this, &MainWindow::positionChanged);
            connect(mediaPlayer, &QMediaPlayer::durationChanged, this, &MainWindow::durationChanged);
            
            m_playButton = new QPushButton;
            m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
            
            connect(m_playButton, &QAbstractButton::clicked, [mediaPlayer]() {
                switch (mediaPlayer->state()) {
                case QMediaPlayer::PlayingState:
                    mediaPlayer->pause();
                    break;
                default:
                    mediaPlayer->play();
                    break;
                }
            });
            
            connect(mediaPlayer, &QMediaPlayer::stateChanged, [m_playButton, this](QMediaPlayer::State state) {
                switch(state) {
                case QMediaPlayer::PlayingState:
                    m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
                    break;
                default:
                    m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
                    break;
                }
            });
            
            
            //connect(m_playButton, &QAbstractButton::clicked, this, &MainWindow::play);
            //connect(m_positionSlider, &QAbstractSlider::sliderMoved,this, &MainWindow::setPosition);
            
            
            
            m_positionSlider = new QSlider(Qt::Horizontal);
            m_positionSlider->setRange(0, 0);
            
            
            auto    item = new QListWidgetItem("", ui->listWidget);
            auto    widget = new QWidget;
            auto    label = new QLabel(finfo.fileName());
            auto    vb = new QVBoxLayout;
            
            QBoxLayout *controlLayout = new QHBoxLayout;
            controlLayout->setMargin(0);
            controlLayout->addWidget(m_playButton);
            controlLayout->addWidget(m_positionSlider);
            
            vb->addWidget(label,1);
            vb->addWidget(graphicsView,9);
            vb->addLayout(controlLayout);
            widget->setLayout(vb);
            widget->setMinimumSize(340, 340);
            ui->listWidget->setItemWidget(item,widget);
            
            
        }
            
    }
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    QSize MainWindow::sizeHint() const
    {
        return (videoItem->size() * qreal(3) / qreal(2)).toSize();
    }
    
    void MainWindow::positionChanged(qint64 position)
    {
        m_positionSlider->setValue(position);
    }
    
    void MainWindow::durationChanged(qint64 duration)
    {
        m_positionSlider->setRange(0, duration);
    }
    
    void MainWindow::setPosition(int position)
    {
        QMediaPlayer *mediaPlayer = player[1];
        mediaPlayer->setPosition(position);
    }
    

  • Moderators

    @Kinesis And exactly this code does not build?



  • @Kinesis

    Declare QPushButton as a local variable like a mediaPlayer. A lambda function can not capture member variables of this.


  • Qt Champions 2017

    @Devopia53 No, it can:

    auto l = [this]() {
      // use members of this here
    }
    


  • @jsulm - Yes it does not work
    @Devopia53 - When I declare local variable , it works.
    @aha_1980 - I will also try like U said .
    Sorry for replying in the same reply . Because I am a new user . I can only post per every 600s



  • I still have a question . I replaced the slider slots into loop but I can't control the sliders and sliders are not moving when the videos are running .
    Here is the slider code

    QSlider *m_positionSlider = new QSlider(Qt::Horizontal);
            m_positionSlider->setRange(0, 0);
    
           
            connect(mediaPlayer, &QMediaPlayer::positionChanged ,[m_positionSlider, this](qint64 position){
    
                m_positionSlider->setValue(position);
            });
    
            connect(mediaPlayer, &QMediaPlayer::durationChanged ,[m_positionSlider, this](qint64 duration){
    
                m_positionSlider->setRange(0,duration);
            });
    
    
            connect(mediaPlayer , &QMediaPlayer::setPosition, [mediaPlayer ,this] (int position){
    
                mediaPlayer->setPosition(position);
            });
    
    

  • Moderators

    @Kinesis What is this:

    connect(mediaPlayer , &QMediaPlayer::setPosition, [mediaPlayer ,this] (int position){
    
                mediaPlayer->setPosition(position);
            });
    

    ?
    QMediaPlayer::setPosition() is a slot not a signal.
    I think you need to change the range

    m_positionSlider->setRange(0, 0);
    

    You can use http://doc.qt.io/qt-5/qmediaplayer.html#duration-prop to set the range.



  • @aha_1980

    I know that well(as [&] or [=]). But what I have described is that you have captured the member variables of the this object directly. Take a look at his source code. here: connect(mediaPlayer, &QMediaPlayer::stateChanged, [m_playButton, this](QMediaPlayer::State state)
    A m_playButton is a member of the this.



  • @jsulm
    Thanks for your suggestion . Now the slider is running along with video.But to use it like skip slider how should I correct this "QMediaPlayer::setPosition() " which is a slot but not a signal ?By the way , sorry for late response .


  • Moderators

    @Kinesis You don't need

    connect(mediaPlayer , &QMediaPlayer::setPosition, [mediaPlayer ,this] (int position){
    
                mediaPlayer->setPosition(position);
            });
    

    You're already using QMediaPlayer::positionChanged...



  • @jsulm
    I got this. It's done now. My code is working fully 100% now . Thanks a lot for helping me . I fell bad that I wasted your time . By the way should I upload my
    code ? It might help someone . Thanks.


  • Moderators

    @Kinesis No need to feel bad - I'm helping here voluntary :-)
    I don't think you need to upload your code. If somebody has questions he/she can ask.



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