以下是一个基于Qt实现的视频播放器的完整源码:
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QVideoWidget>
#include <QFileDialog>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QSlider>
#include <QLabel>
#include <QFileIconProvider>
#include <QMessageBox>
#include <QFileInfo>
#include <QDebug>
#include <QApplication>
class VideoPlayer : public QWidget
{
Q_OBJECT
public:
VideoPlayer(QWidget *parent = nullptr)
: QWidget(parent)
{
// 创建媒体播放器和视频展示窗口
mediaPlayer = new QMediaPlayer(this);
videoWidget = new QVideoWidget(this);
mediaPlayer->setVideoOutput(videoWidget);
// 创建播放列表
playlist = new QMediaPlaylist(this);
mediaPlayer->setPlaylist(playlist);
// 创建控制按钮
playButton = new QPushButton(this);
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
connect(playButton, &QPushButton::clicked, this, &VideoPlayer::togglePlayback);
stopButton = new QPushButton(this);
stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
connect(stopButton, &QPushButton::clicked, mediaPlayer, &QMediaPlayer::stop);
// 创建进度条
positionSlider = new QSlider(Qt::Horizontal, this);
positionSlider->setRange(0, 0);
connect(positionSlider, &QSlider::sliderMoved, this, &VideoPlayer::setPosition);
// 创建标签显示视频信息
infoLabel = new QLabel(this);
infoLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
// 创建布局
QVBoxLayout *mainLayout = new QVBoxLayout;
QHBoxLayout *controlLayout = new QHBoxLayout;
controlLayout->addWidget(playButton);
controlLayout->addWidget(stopButton);
controlLayout->addWidget(positionSlider);
mainLayout->addWidget(videoWidget);
mainLayout->addWidget(infoLabel);
mainLayout->addLayout(controlLayout);
setLayout(mainLayout);
}
private slots:
void openFile()
{
QStringList fileNames = QFileDialog::getOpenFileNames(this, "选择视频文件", QString(), "视频文件 (*.mp4 *.avi)");
if (!fileNames.isEmpty()) {
foreach(const QString &fileName, fileNames) {
QFileInfo fileInfo(fileName);
if (fileInfo.exists() && fileInfo.isFile()) {
playlist->addMedia(QUrl::fromLocalFile(fileName));
} else {
QMessageBox::warning(this, "错误", QString("无法打开文件: %1").arg(fileName));
}
}
mediaPlayer->play();
}
}
void togglePlayback()
{
if (mediaPlayer->state() == QMediaPlayer::PlayingState) {
mediaPlayer->pause();
} else {
mediaPlayer->play();
}
}
void setPosition(int position)
{
mediaPlayer->setPosition(position);
}
void updatePosition(qint64 position)
{
positionSlider->setValue(position);
}
void updateDuration(qint64 duration)
{
positionSlider->setRange(0, duration);
}
void handleError()
{
QMessageBox::critical(this, "错误", QString("发生错误: %1").arg(mediaPlayer->errorString()));
}
private:
QMediaPlayer *mediaPlayer;
QVideoWidget *videoWidget;
QMediaPlaylist *playlist;
QPushButton *playButton;
QPushButton *stopButton;
QSlider *positionSlider;
QLabel *infoLabel;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
VideoPlayer player;
player.setWindowTitle("视频播放器");
player.resize(640, 480);
player.show();
return app.exec();
}
#include "main.moc"
该视频播放器使用了QMediaPlayer
、QMediaPlaylist
和QVideoWidget
等Qt类来实现视频播放功能。界面上有一个视频展示窗口、播放按钮、停止按钮和进度条。用户可以通过点击播放按钮来开始/暂停视频播放,通过点击停止按钮来停止视频播放,通过拖动进度条来调整视频播放位置。
使用方法:
在Qt项目中添加以上源码,例如将其保存为main.cpp
。
编译并运行项目。
注意:这段代码使用的是Qt 5的C++ API,可能需要根据您的Qt版本进行适当调整。