QT Multimedia players for multiple video files



  • Is QT cable of creating multiple multimedia players for many video files??
    I want to filter video files from a directory and display all videos on each media players (I use QGraphicView and media player).


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    It's up to you to create as many player as you want/need.



  • I want to know the way how to create media players for each video file


  • Moderators

    @Kinesis Like shown in the documentation: http://doc.qt.io/qt-5/qmediaplayer.html ?



  • Yes I use qmediaplayer . But not like that . Here is my mainwindow.cpp.But it is for only 1 video with 1 player . I want multiple media players for multiple videos.

    //mainwindow.cpp
    MainWindow::MainWindow(QWidget parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    QString directory = QFileDialog::getOpenFileName(this,tr("Open File"),"All files(
    .*)");
    m_mediaPlayer = new QMediaPlayer(this, QMediaPlayer::VideoSurface);
    const QRect screenGeometry = QApplication::desktop()->screenGeometry(this);
    m_videoItem = new QGraphicsVideoItem;
    m_mediaPlayer->setMedia(QUrl::fromLocalFile(directory));
    m_videoItem->setSize(QSizeF(screenGeometry.width() / 3.2, screenGeometry.height() / 2.4));
    QGraphicsScene *scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);
    scene->addItem(m_videoItem);

    ui->pushButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
    connect(ui->pushButton, &QAbstractButton::clicked, this, &MainWindow::play);

    m_mediaPlayer->setVideoOutput(m_videoItem);
    connect(m_mediaPlayer, &QMediaPlayer::stateChanged,
    this, &MainWindow::mediaStateChanged);
    connect(m_mediaPlayer, &QMediaPlayer::positionChanged, this, &MainWindow::positionChanged);
    connect(m_mediaPlayer, &QMediaPlayer::durationChanged, this, &MainWindow::durationChanged);

    ui->horizontalSlider->setRange(0, 0);
    connect(ui->horizontalSlider, &QAbstractSlider::sliderMoved,
    this, &MainWindow::setPosition);
    MainWindow::~MainWindow()
    {
    delete ui;
    }

    QSize MainWindow::sizeHint() const
    {
    return (m_videoItem->size() * qreal(3) / qreal(2)).toSize();
    }

    bool MainWindow::isPlayerAvailable() const
    {
    return m_mediaPlayer->isAvailable();
    }

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

    void MainWindow::mediaStateChanged(QMediaPlayer::State state)
    {
    switch(state) {
    case QMediaPlayer::PlayingState:
    ui->pushButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
    break;
    default:
    ui->pushButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
    break;
    }
    }

    void MainWindow::positionChanged(qint64 position)
    {
    ui->horizontalSlider->setValue(position);
    }

    void MainWindow::durationChanged(qint64 duration)
    {
    ui->horizontalSlider->setRange(0, duration);
    }

    void MainWindow::setPosition(int position)
    {
    m_mediaPlayer->setPosition(position);
    }


  • Moderators

    @Kinesis As @SGaist said you can create as many of them as you need.
    What is the problem?
    If you can create one you can create many in the same way...



  • @jsulm
    The Problem is I don't know how to create multiple media players for multiple videos which are filtered from directory with looping or some QClasses .


  • Moderators

    @Kinesis Please specify what exactly the problem is.
    Is it: how to filter files in a directory?



  • @jsulm
    Filtering the video file is not the problem.
    The problem is I don't know how to show the filtered videos with media players.
    It means that 1 video per 1 player , 5 videos per 5 players.


  • Moderators

    @Kinesis You iterate over all the video files and create a player for each (like you already did above for one).



  • @jsulm
    I know the logic but when it comes to coding I am really weak . please show me how to do it(just 1 example) . please :(


  • Moderators

    @Kinesis Sorry, I will not write the code for you.
    I really don't understand what the problem is as you already managed to create one player for one file...



  • @jsulm
    Thanks for your help.I really appreciate that.


  • Qt Champions 2017

    @Kinesis
    Hi
    It is easier to help if you break down task in steps and ask pr step what we
    would suggest.
    Like getting all files in a folder you could do

    #include <QDir>
    //assume the directory exists and contains some files 
    QDir directory("c:/xx/yyy"); // point to real folder
    // ask for mp4 and avi. adjust to your needs
    QStringList vids= directory.entryList(QStringList() << "*.mp4" << "*.avi",QDir::Files);
    foreach(const QString &filename, vids) {
    // new a player 
    // insert to form or show as window
    // give it file
    // tell it to play
    }
    


  • @mrjj
    Thanks alot.



  • I got the answer . MainWindow.cpp is like that

     ui->listWidget->setFlow(QListView::LeftToRight);
        ui->listWidget->setMinimumSize(1050,800);
        ui->listWidget->setGridSize(QSize(340, 320));
        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()));
            videoItem = new QGraphicsVideoItem;
            videoItem->setSize(QSize(300,240));
            QGraphicsScene *scene = new QGraphicsScene(this);
            QGraphicsView *graphicsView = new QGraphicsView(scene);
            mediaPlayer->setVideoOutput(videoItem);
    
            QPushButton *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;
                }
            });
    
    
    
            QSlider *m_positionSlider = new QSlider(Qt::Horizontal,this);
            m_positionSlider->setRange(0,mediaPlayer->duration() / 1000);
    
    
            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(m_positionSlider ,&QAbstractSlider::sliderMoved, [mediaPlayer ,this] (int position){
    
                mediaPlayer->setPosition(position);
            });
    
    
    
    
            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);
            vb->addWidget(graphicsView);
            vb->addLayout(controlLayout);
            widget->setLayout(vb);
            widget->setMinimumSize(320, 320);
            ui->listWidget->setItemWidget(item,widget);
            player.append(mediaPlayer);
            scene->addItem(videoItem);
    
    
    
        }
    


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