QT Multimedia players for multiple video files
-
Hi and welcome to devnet,
It's up to you to create as many player as you want/need.
-
@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);
} -
@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 }
-
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); }