QMediaPlayer positionChanged not precise



  • 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 this

    void MainWindow::playerOnPositionChanged(qint64 position) {
        if(position%1000i64 == 0)
            display->setText(QTime(0,0,0).addMSecs(position).toString());
    }
    


  • Qt Creator send me this error message:

    error: unable to find numeric literal operator 'operator""i64'
    if (position%1000i64 == 0)
    ^



  • Sorry, I didn't realise i64 is MSVC only. corrected:

    void MainWindow::playerOnPositionChanged(qint64 position) {
        if(position % static_cast<qint64>(1000) == 0)
            display->setText(QTime(0,0,0).addMSecs(position).toString());
    }
    


  • Not work: display text is always "00:00:00"



  • this is strange. try this in your main (just as a test):

    for (qint64 position = 0; position < 50000; ++position) {
            if ((position % static_cast<qint64>(1000)) == 0)
                qDebug() << QTime(0, 0, 0).addMSecs(position).toString();
        }
    

    it works for me



  • Yes, this loop works on MainWindow constructor



  • then

    void MainWindow::playerOnPositionChanged(qint64 position) {
    qDebug() << position;
        if((position % static_cast<qint64>(1000)) == 0)
            display->setText(QTime(0,0,0).addMSecs(position).toString());
    }
    

    should work too...



  • @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"));
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.