Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Media player not working for multiple videos
Forum Updated to NodeBB v4.3 + New Features

Media player not working for multiple videos

Scheduled Pinned Locked Moved Solved General and Desktop
qt 5.5videosurfacemedia playergraphicview
41 Posts 8 Posters 10.4k Views 3 Watching
  • 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.
  • K Kinesis
    3 Jul 2018, 08:17

    @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);
    }
    
    D Offline
    D Offline
    Devopia53
    wrote on 3 Jul 2018, 08:38 last edited by
    #31

    @Kinesis

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

    A 1 Reply Last reply 3 Jul 2018, 08:41
    0
    • D Devopia53
      3 Jul 2018, 08:38

      @Kinesis

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

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 3 Jul 2018, 08:41 last edited by
      #32

      @Devopia53 No, it can:

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

      Qt has to stay free or it will die.

      D 1 Reply Last reply 3 Jul 2018, 11:24
      0
      • K Offline
        K Offline
        Kinesis
        wrote on 3 Jul 2018, 08:49 last edited by Kinesis 7 Mar 2018, 09:01
        #33

        @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

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kinesis
          wrote on 3 Jul 2018, 09:21 last edited by Kinesis 7 Apr 2018, 04:06
          #34

          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);
                  });
          
          
          jsulmJ 1 Reply Last reply 3 Jul 2018, 10:27
          0
          • K Kinesis
            3 Jul 2018, 09:21

            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);
                    });
            
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on 3 Jul 2018, 10:27 last edited by jsulm 7 Mar 2018, 10:30
            #35

            @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.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            K 1 Reply Last reply 4 Jul 2018, 04:18
            1
            • A aha_1980
              3 Jul 2018, 08:41

              @Devopia53 No, it can:

              auto l = [this]() {
                // use members of this here
              }
              
              D Offline
              D Offline
              Devopia53
              wrote on 3 Jul 2018, 11:24 last edited by Devopia53 7 Mar 2018, 11:25
              #36

              @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.

              1 Reply Last reply
              1
              • jsulmJ jsulm
                3 Jul 2018, 10:27

                @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.

                K Offline
                K Offline
                Kinesis
                wrote on 4 Jul 2018, 04:18 last edited by
                #37

                @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 .

                jsulmJ 1 Reply Last reply 4 Jul 2018, 04:21
                0
                • K Kinesis
                  4 Jul 2018, 04:18

                  @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 .

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 4 Jul 2018, 04:21 last edited by
                  #38

                  @Kinesis You don't need

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

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

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  K 1 Reply Last reply 4 Jul 2018, 04:39
                  0
                  • jsulmJ jsulm
                    4 Jul 2018, 04:21

                    @Kinesis You don't need

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

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

                    K Offline
                    K Offline
                    Kinesis
                    wrote on 4 Jul 2018, 04:39 last edited by
                    #39

                    @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.

                    jsulmJ 1 Reply Last reply 4 Jul 2018, 04:45
                    1
                    • K Kinesis
                      4 Jul 2018, 04:39

                      @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.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 4 Jul 2018, 04:45 last edited by
                      #40

                      @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.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      2
                      • W Offline
                        W Offline
                        WoodyLanes
                        Banned
                        wrote on 31 Aug 2022, 18:57 last edited by WoodyLanes
                        #41
                        This post is deleted!
                        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