基于Qt实现视频播放器功能,可以按照以下步骤进行:
#include <QtMultimedia>
#include <QtWidgets>
QWidget *mainWindow = new QWidget;
QMediaPlayer *player = new QMediaPlayer;
QVideoWidget *videoWidget = new QVideoWidget;
player->setVideoOutput(videoWidget);
QBoxLayout *layout = new QVBoxLayout;
layout->addWidget(videoWidget);
QPushButton *openButton = new QPushButton("Open");
connect(openButton, &QPushButton::clicked, [=]() {
QString fileName = QFileDialog::getOpenFileName(mainWindow, "Open Video");
player->setMedia(QUrl::fromLocalFile(fileName));
player->play();
});
layout->addWidget(openButton);
mainWindow->setLayout(layout);
mainWindow->setWindowTitle("Video Player");
mainWindow->show();
return app.exec();
这样就实现了一个简单的视频播放器功能。用户可以点击打开按钮选择视频文件,然后视频将在QVideoWidget中播放。