Qt视频播放器项目(打开文件,视频的播放与停止,进度条与视频进度绑定,调整音量,设置按钮图标,将槽函数与按钮绑定等功能)完整代码地址在末尾

本文介绍了使用Qt创建视频播放器的步骤,包括项目创建、UI设计、按钮和滑动条的配置,以及视频播放功能的实现。详细讲解了如何设置图标、固定窗口大小、绑定槽函数以及添加必要的包和对象声明。提供完整的项目代码链接以供下载参考。
摘要由CSDN通过智能技术生成

在这里插入图片描述

完整代码地址在文章最后

一.创建项目

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

实现视频播放的基本思路如下: 1. 使用QMediaPlayer类控制视频播放暂停等操作; 2. 使用QVideoWidget类作为视频的显示窗口; 3. 使用QSlider类实现进度条; 4. 使用QLabel类显示总时长和播放进度; 5. 使用QPushButton类实现开始、暂停停止按钮。 下面是一个简单的示例代码: ```cpp #include <QMediaPlayer> #include <QVideoWidget> #include <QSlider> #include <QLabel> #include <QPushButton> #include <QHBoxLayout> #include <QVBoxLayout> #include <QTime> class VideoPlayer : public QWidget { Q_OBJECT public: VideoPlayer(QWidget *parent = nullptr) : QWidget(parent) { // 创建视频播放器 player = new QMediaPlayer(this); videoWidget = new QVideoWidget(this); player->setVideoOutput(videoWidget); // 创建进度条 slider = new QSlider(Qt::Horizontal, this); slider->setRange(0, 0); connect(player, &QMediaPlayer::durationChanged, this, &VideoPlayer::setDuration); connect(player, &QMediaPlayer::positionChanged, this, &VideoPlayer::setPosition); connect(slider, &QSlider::sliderMoved, this, &VideoPlayer::seek); // 创建标签 timeLabel = new QLabel("--:-- / --:--", this); // 创建按钮 playButton = new QPushButton("Play", this); connect(playButton, &QPushButton::clicked, this, &VideoPlayer::play); pauseButton = new QPushButton("Pause", this); connect(pauseButton, &QPushButton::clicked, player, &QMediaPlayer::pause); stopButton = new QPushButton("Stop", this); connect(stopButton, &QPushButton::clicked, player, &QMediaPlayer::stop); // 布局 QHBoxLayout *buttonLayout = new QHBoxLayout; buttonLayout->addWidget(playButton); buttonLayout->addWidget(pauseButton); buttonLayout->addWidget(stopButton); QHBoxLayout *sliderLayout = new QHBoxLayout; sliderLayout->addWidget(slider); sliderLayout->addWidget(timeLabel); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(videoWidget); layout->addLayout(sliderLayout); layout->addLayout(buttonLayout); setLayout(layout); } private slots: void setDuration(qint64 duration) { this->duration = duration; slider->setRange(0, duration); updateTimeLabel(); } void setPosition(qint64 position) { slider->setValue(position); updateTimeLabel(); } void seek(int position) { player->setPosition(position); updateTimeLabel(); } void play() { player->play(); playButton->setEnabled(false); } void updateTimeLabel() { QTime durationTime(0, (duration / 60000) % 60, (duration / 1000) % 60); QTime positionTime(0, (slider->value() / 60000) % 60, (slider->value() / 1000) % 60); timeLabel->setText(positionTime.toString("mm:ss") + " / " + durationTime.toString("mm:ss")); } private: QMediaPlayer *player; QVideoWidget *videoWidget; QSlider *slider; QLabel *timeLabel; QPushButton *playButton, *pauseButton, *stopButton; qint64 duration; }; ``` 使用方法: ```cpp VideoPlayer *player = new VideoPlayer; player->show(); player->player->setMedia(QUrl::fromLocalFile("path/to/video")); ``` 其中,`QUrl::fromLocalFile` 方法可以将本地文件路径转换为 URL。你只需要将路径替换成你自己的视频文件路径即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

computer_vision_chen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值