基于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中播放。