//开始播放 void Widget::on_start_play_btn_clicked() { //播放视频 this->player->play(); //播放滑块的初始值 this->ui->slider->setValue(0); //启动定时器 this->timer->start(1000); } //更新数据 void Widget::update_data() { //显示视频总时长以及播放进度 qint64 duration = this->player->duration(); qint64 position = this->player->position(); QTime time_d(0, 0, 0, 0); QTime time_p(0, 0, 0, 0); //给时间标签设置文本内容 this->ui->time1_lab->setText(time_p.addMSecs(position).toString("hh:mm:ss")); this->ui->time2_lab->setText(time_d.addMSecs(duration).toString("hh:mm:ss")); if(duration == position)//判断歌曲是否播放完成 { //循环播放:先停止 this->player->stop();// reset this->player->play(); } }时间进度不变化
时间: 2023-06-26 10:06:38 浏览: 44
在你的代码中,定时器每隔一秒钟会调用 update_data() 函数更新数据,其中包括更新视频播放进度。如果视频播放进度不变化,可能是以下原因:
1. 定时器没有正常启动。你可以在 on_start_play_btn_clicked() 函数中添加一个 qDebug() 语句来确认定时器是否启动了。
2. 视频播放器没有正常工作。你可以在 update_data() 函数中添加一个 qDebug() 语句来确认 duration 和 position 是否正确获取到了视频的总时长和当前播放进度。
3. 视频文件本身有问题。你可以换一个视频文件测试一下,看是否仍然出现播放进度不变化的情况。
如果以上三个方面都没有问题,那么可以考虑检查你的代码逻辑是否有误。
相关问题
//选择视频 void Widget::on_choose_btn_clicked() { //获取视频的完整路径 // QString str="D:\Qt\Qt5.12.12\QT\QT_project\output.avi"; //QString path = "D:\Qt\Qt5.12.12\QT\QT_project\output.avi"; QString path = QFileDialog::getOpenFileName(nullptr, "Open Image", QDir::homePath(), "Images (*.avi *.mp4 *.wmv)"); //加载资源文件 this->player->setMedia(QMediaContent(QUrl(path))); //设置画面的输出 this->player->setVideoOutput(video_widget); // 将视频画面输出到pictrue_lab } //开始播放 void Widget::on_start_play_btn_clicked() { //播放视频 this->player->play(); //播放滑块的初始值 this->ui->slider->setValue(0); //启动定时器 this->timer->start(1000); }将画面显示在label标签中
要将视频画面显示在label标签中,可以使用QLabel类中的setPixmap()方法来设置标签的显示内容。具体实现可以参考下面的代码:
//选择视频
void Widget::on_choose_btn_clicked() {
QString path = QFileDialog::getOpenFileName(nullptr, "Open Video", QDir::homePath(), "Videos (*.avi *.mp4 *.wmv)");
this->player->setMedia(QMediaContent(QUrl(path)));
this->player->setVideoOutput(video_widget);
}
//开始播放
void Widget::on_start_play_btn_clicked() {
this->player->play();
this->ui->slider->setValue(0);
this->timer->start(1000);
}
//更新标签显示内容
void Widget::updateLabel() {
//获取当前帧的图像
QImage frame = video_widget->grab().toImage();
//将图像缩放到标签的大小
QPixmap pixmap = QPixmap::fromImage(frame).scaled(ui->label->size(), Qt::KeepAspectRatio);
//设置标签的显示内容
ui->label->setPixmap(pixmap);
}
//定时器事件
void Widget::timerEvent(QTimerEvent *event) {
//更新标签显示内容
updateLabel();
}
其中,updateLabel()方法用来更新标签的显示内容,它首先调用video_widget->grab().toImage()方法获取当前帧的图像,然后将图像缩放到标签的大小,并使用setPixmap()方法设置标签的显示内容。定时器事件中,每隔一定时间调用updateLabel()方法更新标签的显示内容。
#include "widget.h" #include "ui_widget.h" #include <QTime> Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); //显示系统时间 //实例化对象 this->timer=new QTimer(this); QString curtime=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); //信号和槽函数关联 connect(this->timer,&QTimer::timeout,this,&Widget::update_time); //启动定时器 this->timer->start(1000); //计时 this->timer->start(1000); this->count=0; QTime time(0,0,0,0); //QTime转换成QString QString sec=time.addSecs(this->count).toString("mm:ss"); //更新到LCDNumber显示 this->ui->lcdNumber->display(sec); //实例化 this->timer=new QTimer(this); //信号和参函数关联 connect(this->timer,&QTimer::timeout,this,&Widget::update_time); } Widget::~Widget() { delete ui; } void Widget::on_home_btn_clicked() { //首页 } void Widget::on_per_btn_clicked() { //上一页 } void Widget::on_next_btn_clicked() { //下一页 } void Widget::on_last_btn_clicked() { //尾页 } void Widget::update_time() { //获取系统时间 QString curtime=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); //显示 this->ui->time_lab->setText(curtime); } void Widget::update_time2() { //计时 this->count++; QTime time(0,0,0,0); //QTime转换成QString QString sec=time.addSecs(this->count).toString("mm:ss"); //更新到LCDNumber显示 this->ui->lcdNumber->display(sec); }修改代码让程序运行后直接启动倒计时
你可以将启动计时器和更新LCDNumber的代码放到Widget构造函数中,然后将update_time2()函数删除,这样程序运行后就直接启动倒计时了。
修改后的代码如下:
```
#include "widget.h"
#include "ui_widget.h"
#include <QTime>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//显示系统时间
QString curtime=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
this->ui->time_lab->setText(curtime);
//启动定时器
this->timer=new QTimer(this);
connect(this->timer,&QTimer::timeout,this,&Widget::update_time);
this->timer->start(1000);
//启动倒计时
this->count=0;
this->timer2=new QTimer(this);
connect(this->timer2,&QTimer::timeout,this,&Widget::update_time2);
this->timer2->start(1000);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_home_btn_clicked()
{
//首页
}
void Widget::on_per_btn_clicked()
{
//上一页
}
void Widget::on_next_btn_clicked()
{
//下一页
}
void Widget::on_last_btn_clicked()
{
//尾页
}
void Widget::update_time()
{
//获取系统时间
QString curtime=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
//显示
this->ui->time_lab->setText(curtime);
}
void Widget::update_time2()
{
//计时
this->count++;
QTime time(0,0,0,0);
//QTime转换成QString
QString sec=time.addSecs(this->count).toString("mm:ss");
//更新到LCDNumber显示
this->ui->lcdNumber->display(sec);
}
```
注意,需要在Widget类中添加一个私有成员变量QTimer *timer2;来代表倒计时用的定时器。