Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QMediaPlayer positionChanged not precise
Forum Updated to NodeBB v4.3 + New Features

QMediaPlayer positionChanged not precise

Scheduled Pinned Locked Moved Solved General and Desktop
qmediaplayerqtime
11 Posts 2 Posters 4.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    UnitScan
    wrote on 14 Sept 2016, 13:47 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 14 Sept 2016, 14:54 last edited by VRonin
      #2

      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());
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • U Offline
        U Offline
        UnitScan
        wrote on 14 Sept 2016, 15:01 last edited by
        #3

        Qt Creator send me this error message:

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

        1 Reply Last reply
        0
        • V Offline
          V Offline
          VRonin
          wrote on 14 Sept 2016, 15:21 last edited by
          #4

          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());
          }
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0
          • U Offline
            U Offline
            UnitScan
            wrote on 14 Sept 2016, 15:28 last edited by
            #5

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

            1 Reply Last reply
            0
            • V Offline
              V Offline
              VRonin
              wrote on 14 Sept 2016, 15:34 last edited by VRonin
              #6

              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

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • U Offline
                U Offline
                UnitScan
                wrote on 14 Sept 2016, 15:37 last edited by
                #7

                Yes, this loop works on MainWindow constructor

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  VRonin
                  wrote on 14 Sept 2016, 15:45 last edited by
                  #8

                  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...

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  0
                  • U Offline
                    U Offline
                    UnitScan
                    wrote on 14 Sept 2016, 20:43 last edited by UnitScan
                    #9

                    @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

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      VRonin
                      wrote on 15 Sept 2016, 11:49 last edited by
                      #10

                      do you have the same if you run this example? http://doc.qt.io/qt-5/qtmultimedia-multimediawidgets-player-example.html

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      0
                      • U Offline
                        U Offline
                        UnitScan
                        wrote on 16 Sept 2016, 10:08 last edited by UnitScan
                        #11

                        [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"));
                        }
                        
                        1 Reply Last reply
                        1

                        6/11

                        14 Sept 2016, 15:34

                        • Login

                        • Login or register to search.
                        6 out of 11
                        • First post
                          6/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved