使用Qt显示视频总时长

158 篇文章 22 订阅 ¥59.90 ¥99.00

在Qt应用程序中,可以使用多种方法来显示视频的总时长。一种常见的方法是使用Qt的多媒体模块来加载视频文件并获取其总时长。下面是一个示例代码,演示了如何使用Qt显示视频的总时长。

首先,在Qt项目中添加多媒体模块的依赖。在项目的.pro文件中添加以下行:

QT += multimedia

接下来,可以创建一个Qt窗口应用程序,并添加一个按钮和一个标签控件。按钮用于打开视频文件,标签控件用于显示视频的总时长。以下是一个简单的示例界面布局:

#include <QApplication>
#include <QPushButton>
#include <QLabel>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现视频播放的基本思路如下: 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值