close
.h
新增private slots:
1 2 3 4 |
private slots: void getTime(qint64); void currentTime(qint64); |
.cpp
使用connect連結。
durationChanged 開播前執行一次。
positionChanged 每秒執行一次。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#include <QtMultimedia/qmediaplayer> #include <QTime> music_play::music_play(QWidget *parent) : QWidget(parent), ui(new Ui::music_play) { ui->setupUi(this); player = new QMediaPlayer; connect(player, SIGNAL(durationChanged(qint64)),this,SLOT(getTime(qint64))); connect(player, SIGNAL(positionChanged(qint64)),this,SLOT(currentTime(qint64))); player->setMedia(QUrl::fromLocalFile("D:/Qt/project/music_play/song/JC - 說散就散 Lyrics Video.mp3")); player->play(); } void music_play::getTime(qint64 times) //總時間 { qint64 h,m,s; times /=1000; //以毫秒計算所以需要除1000 h = (times)/3600; m = (times-(h*3600))/60; s = times-(h*3600)-(m*60); QTime time; time.setHMS(h,m,s); ui->label->setText(time.toString("hh:mm:ss")); //設置指定格式 } void music_play::currentTime(qint64 times) //目前播放時間 { qint64 h,m,s; times /=1000; h = (times)/3600; m = (times-(h*3600))/60; s = times-(h*3600)-(m*60); QTime time; time.setHMS(h,m,s); ui->label_2->setText(time.toString("hh:mm:ss")); } music_play::~music_play() { delete ui; } |
QMediaPlayer有一個duration(),直接使用是不行的,必須使用connect連結 durationChanged 之後使用才會有東西。
1 |
player->duration() |
文章標籤
全站熱搜