QMediaPlayer positionChanged not precise
Solved
General and Desktop
-
In my player I implemented a QLabel which display the progress of the plaiyng file
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { //… display = new QLabel(this); display->setGeometry(4,4,88,23); display->setText("00:00:00"); //… player = new QMediaPlayer; connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(playerOnPositionChanged(qint64))); //… }
void MainWindow::playerOnPositionChanged(qint64 position) { QTime time(0,0,0); time = time.addMSecs(position); display->setText(time.toString()); }
The problem is that this method is not very accurate, because it often happens may skip seconds: for example, QLabels throw from 1:05 to 1:07. Is there a more accurate way to display the progress of the playing file?
-
It looks like just a "called too many times" problem.
Since your resolution is seconds try something like thisvoid MainWindow::playerOnPositionChanged(qint64 position) { if(position%1000i64 == 0) display->setText(QTime(0,0,0).addMSecs(position).toString()); }
-
-
@VRonin tried this:
void MainWindow::playerOnPositionChanged(qint64 position) { if((position % static_cast<qint64>(1000)) == 0) display->setText(QTime(0,0,0).addMSecs(position).toString()); qDebug() << position % static_cast<qint64>(1000); }
This is the output:
0
859
873
887
901
916
930
944
958
972
…So, the condition
position % static_cast<qint64>(1000)) == 0
is true only once
-
do you have the same if you run this example? http://doc.qt.io/qt-5/qtmultimedia-multimediawidgets-player-example.html
-
[SOLVED]
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { //… display = new QLabel(this); display->setGeometry(4,4,88,23); display->setText("00:00:00"); //… player = new QMediaPlayer; connect(player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), this, SLOT(playerOnMediaStatusChanged(QMediaPlayer::MediaStatus))); //… timer = new QTimer; timer->setTimerType(Qt::PreciseTimer); connect(timer, SIGNAL(timeout()), this, SLOT(timerSlot())); //… } void MainWindow::playerOnMediaStatusChanged(QMediaPlayer::MediaStatus status) { if (status == QMediaPlayer::BufferedMedia) { pos = -1; //int pos declared on .h file timer->start(1000); } } void MainWindow::timerSlot() { pos = pos + 1; QTime time((pos/3600)%60, (pos/60)%60, pos%60); display->setText(time.toString("hh:mm:ss")); }