video player

This commit is contained in:
Ivan I. Ovchinnikov 2022-12-07 16:55:19 +03:00
parent 3751edec21
commit c16ecd44e6
3 changed files with 252 additions and 41 deletions

View File

@ -1,4 +1,5 @@
QT += core gui axcontainer QT += core gui axcontainer multimediawidgets
WEBENGINE_CONFIG += use_proprietary_codecs
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = PDF_Reader TARGET = PDF_Reader
TEMPLATE = app TEMPLATE = app

View File

@ -21,6 +21,7 @@ MainWindow::MainWindow(QWidget *parent) :
connect(this->m_quitAction, &QAction::triggered, qApp, &QApplication::quit); connect(this->m_quitAction, &QAction::triggered, qApp, &QApplication::quit);
this->setMenuBar(menuBar); this->setMenuBar(menuBar);
this->resize(800, 600);
show(); show();
} }
@ -30,12 +31,24 @@ MainWindow::~MainWindow()
} }
void MainWindow::delay(int delay) {
QTime dieTime= QTime::currentTime().addSecs(delay);
while (QTime::currentTime() < dieTime)
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
void MainWindow::slotOpenAction() { void MainWindow::slotOpenAction() {
m_filename = QFileDialog::getOpenFileName( m_filename = QFileDialog::getOpenFileName(
this, tr("Выберите файл"), "../", tr("PDF (*.pdf) ;; DOCX (*.docx) ;; XLSX (*.xlsx) ;; PPTX (*.pptx)")); this, tr("Выберите файл"), "../", tr("MPEG (*.mpeg) ;; PDF (*.pdf) ;; DOCX (*.docx) ;; XLSX (*.xlsx) ;; PPTX (*.pptx)"));
if (m_filename != "") { if (m_filename != "") {
if (axObj) {
qDebug() << "Close file!\n"; qDebug() << "Close file!\n";
closeFile(); closeFile();
}
if (m_videoWidget) {
qDebug() << "Close MPEG!\n";
closeMPEG();
}
if (m_filename.endsWith(".pdf")) { if (m_filename.endsWith(".pdf")) {
openPDF(m_filename); openPDF(m_filename);
@ -45,6 +58,8 @@ void MainWindow::slotOpenAction() {
openDOCX(m_filename); openDOCX(m_filename);
} else if (m_filename.endsWith(".xlsx")) { } else if (m_filename.endsWith(".xlsx")) {
openXLSX(m_filename); openXLSX(m_filename);
} else if (m_filename.endsWith(".mpeg")) {
openMPEG(m_filename);
} }
} }
} }
@ -66,6 +81,7 @@ void MainWindow::closeFile() {
axObj->clear(); axObj->clear();
delete axObj; delete axObj;
axObj = nullptr; axObj = nullptr;
qDebug() << "file closed!";
} }
} }
@ -78,46 +94,62 @@ void MainWindow::openPDF(QString &filename) {
axObj->dynamicCall("LoadFile(QString)", filename); axObj->dynamicCall("LoadFile(QString)", filename);
} }
/* run slideshow */
void MainWindow::playPPTX() {
// Open parameter
QList<QVariant> param_list;
axObj->setControl("PowerPoint.Application");
param_list.append(m_filename);
param_list.append(1);
param_list.append(1);
param_list.append(0);
// Open a template file
QAxObject *presentations = axObj->querySubObject("Presentations");
QAxObject *presentation = presentations->querySubObject("Open(const QString&,int,int,int)", param_list);
// Start the slides
QAxObject *slideshow = presentation->querySubObject("SlideShowSettings");
slideshow->setProperty("RangeType", "ppShowAll");
slideshow->dynamicCall("run()");
}
void MainWindow::openPPTX(QString &filename) { void MainWindow::openPPTX(QString &filename) {
axObj = new QAxWidget("Powerpoint.Application", this); /* инициализация окна виджетом */
setCentralWidget(axObj); m_wideWidget = new QWidget;
if (axObj == nullptr) setCentralWidget(m_wideWidget);
{ qDebug() << "1";
/* здесь будут элементы управления и окно проигрывателя (выравнивание по горизонтали) */
m_qvBoxLayout = new QVBoxLayout(m_wideWidget);
axObj = new QAxWidget("PowerPoint.Application", m_wideWidget);
m_btnPlay = new QPushButton("Начать презентацию");
qDebug() << "2";
m_qvBoxLayout->addWidget(axObj);
m_qvBoxLayout->addWidget(m_btnPlay);
if (axObj == nullptr) {
return; return;
} }
if (!axObj->setControl(filename)) if (!axObj->setControl(filename)) {
{
return; return;
} }
axObj->dynamicCall("LoadFile(const QString&)", filename); axObj->dynamicCall("LoadFile(const QString&)", filename);
axObj->dynamicCall("SetVisible (bool Visible)", "true"); axObj->dynamicCall("SetVisible (bool Visible)", "false");
axObj->setProperty("DisplayAlerts", false); axObj->setProperty("DisplayAlerts", false);
axObj->setProperty("DisplayScrollBars", true); axObj->setProperty("DisplayScrollBars", true);
axObj->setGeometry(this->geometry());
delay(1);
axObj->show(); axObj->show();
axObj->raise();
/* run slideshow */ qDebug() << "3";
// // Open parameter connect(this->m_btnPlay, &QPushButton::clicked, this, &MainWindow::playPPTX);
// QList<QVariant> param_list;
// param_list.append(filename);
// param_list.append(1);
// param_list.append(1);
// param_list.append(0);
// // Open a template file
// QAxObject *presentations = axObj->querySubObject("Presentations");
// QAxObject *presentation = presentations->querySubObject("Open(const QString&,int,int,int)", param_list);
// // Start the slides
// QAxObject *slideshow = presentation->querySubObject("SlideShowSettings");
// slideshow->setProperty("RangeType", "ppShowAll");
// slideshow->dynamicCall("run()");
} }
void MainWindow::openXLSX(QString &filename) { void MainWindow::openXLSX(QString &filename) {
axObj = new QAxWidget("Excel.Application", this); axObj = new QAxWidget("Excel.Application", this);
setCentralWidget(axObj); setCentralWidget(axObj);
if (axObj == nullptr) if (axObj == nullptr) {
{
return; return;
} }
axObj->dynamicCall("SetVisible (bool Visible)", "false"); axObj->dynamicCall("SetVisible (bool Visible)", "false");
@ -130,8 +162,7 @@ void MainWindow::openXLSX(QString &filename) {
void MainWindow::openDOCX(QString &filename) { void MainWindow::openDOCX(QString &filename) {
axObj = new QAxWidget("Word.Application", this); axObj = new QAxWidget("Word.Application", this);
setCentralWidget(axObj); setCentralWidget(axObj);
if (axObj == nullptr) if (axObj == nullptr) {
{
return; return;
} }
axObj->dynamicCall("SetVisible (bool Visible)", "false"); axObj->dynamicCall("SetVisible (bool Visible)", "false");
@ -142,3 +173,135 @@ void MainWindow::openDOCX(QString &filename) {
axObj->setControl(filename); axObj->setControl(filename);
axObj->show(); axObj->show();
} }
/* продолжить проигрывать видео */
void MainWindow::playVideo() {
m_player->setPosition(m_position);
m_player->play();
}
/* остановка видео */
void MainWindow::stopVideo() {
m_player->stop();
}
/* пауза видео */
void MainWindow::pauseVideo() {
m_player->pause();
}
/* событие при изменении состояния видеопроигрывателя */
void MainWindow::updateState(int state) {
switch (state) {
case QMediaPlayer::PlayingState:
/* media is played */
break;
case QMediaPlayer::PausedState:
/* media paused */
break;
case QMediaPlayer::StoppedState:
/* media is stopped */
break;
}
}
/* принудительно изменить время проигрывания */
void MainWindow::changePosition() {
m_position = m_sliderVideo->value();
m_player->setPosition(m_position);
}
/* принудительно изменить время проигрывания */
void MainWindow::updatePosition(qint64 position) {
m_position = position;
m_sliderVideo->setValue(m_position);
}
/* изменить величину звука */
void MainWindow::changeVolume(int volume) {
m_player->setVolume(volume);
}
/* слот для иницализации значением слайдера видео */
void MainWindow::updateDuration(qint64 duration) {
m_sliderVideo->setRange(0, m_player->duration());
m_sliderVideo->setEnabled(duration > 0);
m_sliderVideo->setPageStep(duration / 10);
}
/* инициализация медиа файла */
void MainWindow::openMPEG(QString &filename) {
/* инициализация окна виджетом */
m_wideWidget = new QWidget;
setCentralWidget(m_wideWidget);
/* здесь будут элементы управления и окно проигрывателя (выравнивание по горизонтали) */
m_qvBoxLayout = new QVBoxLayout(m_wideWidget);
/* область для отображения видео */
m_videoWidget = new QVideoWidget;
m_player = new QMediaPlayer(m_videoWidget);
m_player->setVideoOutput(m_videoWidget);
m_player->setMedia(QUrl::fromLocalFile(filename));
/* область размещения кнопок управления видео */
m_hBtnsLayout = new QHBoxLayout;
m_play = new QPushButton("Play");
m_stop = new QPushButton("Stop");
m_pause = new QPushButton("Pause");
m_sliderVolume = new QSlider(Qt::Horizontal);
m_sliderVolume->setRange(0, 100); // диапазон значений громкости
m_sliderVolume->setValue(50); // текущая громкость 50%
m_sliderVideo = new QSlider(Qt::Horizontal);
m_hBtnsLayout->addWidget(m_play, 1);
m_hBtnsLayout->addWidget(m_stop, 1);
m_hBtnsLayout->addWidget(m_pause, 1);
m_hBtnsLayout->addWidget(&m_labelVolume);
m_hBtnsLayout->addWidget(m_sliderVolume, 1);
m_hBtnsLayout->addWidget(&m_labelScroll);
m_hBtnsLayout->addWidget(m_sliderVideo, 5);
/* поместить настроенные объекты в центральный виджет */
m_qvBoxLayout->addWidget(m_videoWidget);
m_qvBoxLayout->addLayout(m_hBtnsLayout);
/* связать слоты */
connect(this->m_play, &QPushButton::clicked, this, &MainWindow::playVideo);
connect(this->m_stop, &QPushButton::clicked, this, &MainWindow::stopVideo);
connect(this->m_pause, &QPushButton::clicked, this, &MainWindow::pauseVideo);
connect(this->m_sliderVolume, &QSlider::sliderMoved, this, &MainWindow::changeVolume);
connect(this->m_sliderVideo, &QSlider::sliderMoved, this, &MainWindow::changePosition);
connect(this->m_sliderVideo, &QSlider::valueChanged, this, &MainWindow::changePosition);
connect(this->m_player, &QMediaPlayer::durationChanged, this, &MainWindow::updateDuration);
connect(this->m_player, &QMediaPlayer::positionChanged, this, &MainWindow::updatePosition);
connect(this->m_player, &QMediaPlayer::stateChanged, this, &MainWindow::updateState);
/* начать проигрывать видео */
playVideo();
}
void MainWindow::closeMPEG() {
/* отключение всех слотов */
disconnect(this->m_play, &QPushButton::clicked, this, &MainWindow::playVideo);
disconnect(this->m_stop, &QPushButton::clicked, this, &MainWindow::stopVideo);
disconnect(this->m_pause, &QPushButton::clicked, this, &MainWindow::pauseVideo);
disconnect(this->m_sliderVolume, &QSlider::sliderMoved, this, &MainWindow::changeVolume);
disconnect(this->m_sliderVideo, &QSlider::sliderMoved, this, &MainWindow::changePosition);
disconnect(this->m_sliderVideo, &QSlider::sliderPressed, this, &MainWindow::changePosition);
disconnect(this->m_player, &QMediaPlayer::durationChanged, this, &MainWindow::updateDuration);
disconnect(this->m_player, &QMediaPlayer::positionChanged, this, &MainWindow::updatePosition);
disconnect(this->m_player, &QMediaPlayer::stateChanged, this, &MainWindow::updateState);
/* удаление динамических объектов */
delete m_sliderVideo;
delete m_sliderVolume;
delete m_play;
delete m_stop;
delete m_pause;
delete m_hBtnsLayout;
delete m_player;
delete m_videoWidget;
delete m_qvBoxLayout;
}

View File

@ -5,6 +5,13 @@
#include <QMainWindow> #include <QMainWindow>
#include <ActiveQt> #include <ActiveQt>
#include <QMenuBar> #include <QMenuBar>
#include <qt_windows.h>
#include <QtMultimedia/QMediaPlayer>
#include <QtMultimedia/QMediaPlaylist>
#include <QtMultimediaWidgets/QVideoWidget>
#include <QDebug>
#include <QVBoxLayout>
#include <QHBoxLayout>
namespace Ui { namespace Ui {
class MainWindow; class MainWindow;
@ -19,19 +26,59 @@ public:
~MainWindow(); ~MainWindow();
private: private:
Ui::MainWindow *ui; /* begin common objects */
Ui::MainWindow *ui{nullptr};
QAxWidget *axObj{nullptr}; QWidget *m_wideWidget{nullptr};
QString m_filename; QVBoxLayout *m_qvBoxLayout{nullptr};
void openPDF(QString&);
void openPPTX(QString&);
void openXLSX(QString&);
void openDOCX(QString&);
void closeFile();
QMenuBar *mb{nullptr}; QMenuBar *mb{nullptr};
QMenu *m_menuFile{nullptr}; QMenu *m_menuFile{nullptr};
QAction *m_openAction{nullptr}; QAction *m_openAction{nullptr};
QAction *m_quitAction{nullptr}; QAction *m_quitAction{nullptr};
QString m_filename;
void delay(int);
/* end common objects */
/* begin DOCX, XLSX, PPTX objects */
QAxWidget *axObj{nullptr};
void openPDF(QString&);
void openXLSX(QString&);
void openDOCX(QString&);
void openPPTX(QString&);
void closeFile();
QPushButton *m_btnPlay;
private slots:
void playPPTX();
/* end DOCX, XLSX, PPTX objects */
/* begin MPEG objects */
private:
void openMPEG(QString&);
void closeMPEG();
QVideoWidget *m_videoWidget{nullptr};
QMediaPlayer *m_player{nullptr};
QHBoxLayout *m_hBtnsLayout{nullptr};
QPushButton *m_play{nullptr};
QPushButton *m_stop{nullptr};
QPushButton *m_pause{nullptr};
QSlider *m_sliderVolume{nullptr};
QSlider *m_sliderVideo{nullptr};
QLabel m_labelVolume{"Volume:"};
QLabel m_labelScroll{"Scroll:"};
qint64 m_position{0};
int m_volume;
private slots:
void playVideo();
void stopVideo();
void pauseVideo();
void changeVolume(int);
void changePosition();
void updatePosition(qint64);
void updateDuration(qint64);
void updateState(int);
/* end MPEG objects */
private slots: private slots:
void slotOpenAction(); void slotOpenAction();