Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

Two durations of QMediaPlayer file



  • Hello. I'm creating a music player and I have come across a strange problem. From what I know there are two possible ways of getting the duration of the QMediaPlayer file, by the duration() function and the metadata. They are varying in the duration by small numbers but my case is different. The duration of the file (from QMediaPlayer) is almost 4 times longer than the real one. For example:

    Duration by the function: 907826
    Duration by the metadata: 218567

    But still, the music ends after 3:38, not 15:07. This creates problems with the progress slider and the timer. I also checked is the same problem appears in the qt music player example and it's there too. I actually have the duration problem with the single mp3 file (rest of the mp3 files with metadata are working fine).

    Here comes part of the code if it would help anything:

    // progress - QSlider
    // musicMedia - QMediaPlayer
    
    connect(musicMedia, &QMediaPlayer::durationChanged, this, &MainWindow::on_durationChanged);
    connect(musicMedia, &QMediaPlayer::positionChanged, this, &MainWindow::on_positionChanged);
    
    void  MainWindow::on_positionChanged(int position)
    {
          ui->progress->setValue(position);
    }
    
    void  MainWindow::on_durationChanged(int duration)
    {
        ui->progress->setRange(0, duration);
    
        //or
    
       // ui->progress->setRange(0, musicMedia->metaData("Duration").toInt());
    }
    
    void MainWindow::on_progress_sliderMoved(int position)
    {
       musicMedia->setPosition(position);
    }
    
    

    If I will use the metadata time and move the slider, the music will end too soon. If I will use the function duration then the music will end before the slider will finish. What should I do to avoid this and still get the real time for the timer?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Did you check what you get with that same file with other players likes VLC ?



  • Thank you for the greeting.

    The file is working fine with the other players (VLC, Windows 10 player).


Log in to reply