那帮我写一个QT框架中视频播放器的代码示例
时间: 2023-06-05 15:04:48 浏览: 175
Okay,以下是一个基本的QT框架中视频播放器的示例代码,你可以根据你的需求进行修改。
#include <QApplication>
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QMediaPlaylist>
#include <QPushButton>
#include <QSlider>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QStyle>
#include <QFile>
#include <QFileDialog>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
//创建悬浮窗口
QVideoWidget *videoWidget = new QVideoWidget();
QMediaPlayer *player = new QMediaPlayer();
//设置窗口为播放器的输出
player->setVideoOutput(videoWidget);
//创建媒体播放列表
QMediaPlaylist *playlist = new QMediaPlaylist();
playlist->addMedia(QUrl::fromLocalFile("video1.mp4"));
playlist->addMedia(QUrl::fromLocalFile("video2.mp4"));
playlist->addMedia(QUrl::fromLocalFile("video3.mp4"));
playlist->setCurrentIndex(0);
//设置自动播放模式
player->setPlaylist(playlist);
player->play();
//创建控制台
QPushButton *playButton = new QPushButton();
playButton->setIcon(app.style()->standardIcon(QStyle::SP_MediaPlay));
QHBoxLayout *playLayout = new QHBoxLayout;
playLayout->addWidget(playButton);
QSlider *slider = new QSlider();
slider->setOrientation(Qt::Horizontal);
QHBoxLayout *sliderLayout = new QHBoxLayout;
sliderLayout->addWidget(slider);
QPushButton *fullscreenButton = new QPushButton();
fullscreenButton->setIcon(app.style()->standardIcon(QStyle::SP_ArrowFullScreen));
QHBoxLayout *fullscreenLayout = new QHBoxLayout;
fullscreenLayout->addWidget(fullscreenButton);
QVBoxLayout *controlLayout = new QVBoxLayout;
controlLayout->addLayout(playLayout);
controlLayout->addLayout(sliderLayout);
controlLayout->addLayout(fullscreenLayout);
//进度条更新
QObject::connect(player, &QMediaPlayer::durationChanged, slider, &QSlider::setMaximum);
QObject::connect(player, &QMediaPlayer::positionChanged, slider, &QSlider::setValue);
//播放按钮事件
QObject::connect(playButton, &QPushButton::clicked, [player, playButton]() {
if (player->state() == QMediaPlayer::PlayingState) {
player->pause();
playButton->setIcon(
qApp->style()->standardIcon(QStyle::SP_MediaPlay));
} else {
player->play();
playButton->setIcon(
qApp->style()->standardIcon(QStyle::SP_MediaPause));
}
});
//全屏按钮事件
videoWidget->setFullScreen(false);
QObject::connect(fullscreenButton, &QPushButton::clicked, [videoWidget]() {
if (!videoWidget->isFullScreen()) {
videoWidget->setFullScreen(true);
} else {
videoWidget->setFullScreen(false);
}
});
//打开本地视频
QPushButton *openButton = new QPushButton();
openButton->setText("打开本地视频");
QHBoxLayout *openLayout = new QHBoxLayout;
openLayout->addWidget(openButton);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(videoWidget);
mainLayout->addLayout(controlLayout);
mainLayout->addLayout(openLayout);
QWidget *widget = new QWidget();
widget->setLayout(mainLayout);
widget->show();
QObject::connect(openButton, &QPushButton::clicked, [player, videoWidget]() {
QString fileName = QFileDialog::getOpenFileName(nullptr, QStringLiteral("选择文件"), "", QStringLiteral("所有文件(*)"));
if (fileName.isEmpty()) {
return;
}
QFile videoFile(fileName);
if (videoFile.exists()) {
player->setMedia(QUrl::fromLocalFile(fileName));
videoWidget->show();
}
});
return app.exec();
}
阅读全文