I'm on Ubuntu 20.04 using Qt 5.14. QtMdeiaPlayer duration always returns 0.
Documentation states that call to SetMedia is not blocking so duration will be zero right after the call, but I connected a signal as stated in the documentation.
connect(player, &QMediaPlayer::durationChanged, this, [&](qint64 dur) {
qDebug() << "duration = " << dur;
});
This lambda is never called. Furthermore audio is playing normally and signal for positionChanged works as expected. I tested several audio files of different formats and result is the same. Any ideas why it could happen?
-- edit -- Minimal code to reproduce
// somewhere
static QMediaPlayer *player = new QMediaPlayer;
static void DurationChanged(quint64 duration) {
qDebug() << "duration " << duration;
}
static void PositionChaned(quint64 position) {
qDebug() << "position" << position << "duration" << player->duration();
}
// in main
auto path = QUrl::fromLocalFile(QFileInfo("sample.mp3").absoluteFilePath());
QObject::connect(player, &QMediaPlayer::durationChanged, DurationChanged);
QObject::connect(player, &QMediaPlayer::positionChanged, PositionChaned);
player->setMedia(path);
player->play();
// my output
// position 0 duration 0
// position 1032 duration 0
// position 2083 duration 0
I have used a ubuntu 20.04 docker and I get the duration of the audio:
#include <QtMultimedia>
static void DurationChanged(quint64 duration) {
qDebug() << "duration " << duration;
}
static void PositionChaned(quint64 position) {
qDebug() << "position" << position;
}
int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv);
// in main
QDir dir_path = QCoreApplication::applicationDirPath();
auto path = dir_path.filePath("sample.mp3");
auto url = QUrl::fromLocalFile(path);
QMediaPlayer player;
QObject::connect(&player, &QMediaPlayer::durationChanged, DurationChanged);
QObject::connect(&player, &QMediaPlayer::positionChanged, PositionChaned);
player.setMedia(url);
player.play();
return a.exec();
}
Output:
position 0
position 0
position 9
duration 26000
position 987
position 1945
position 2883
position 3821
position 4789
position 5786
position 6784
position 7792
position 8790
position 9797
position 10775
position 11783
position 12780
position 13788
position 14786
position 15794
position 16791
position 17799
position 18797
position 19804
position 20782
position 21790
position 22788
position 23795
position 24793
position 25801
position 26000
For the audio plugin to work install the following packages:
sudo apt-get install -y --no-install-recommends \
libgstreamer1.0-0 \
gstreamer1.0-plugins-base \
gstreamer1.0-plugins-good \
gstreamer1.0-plugins-bad \
gstreamer1.0-plugins-ugly \
gstreamer1.0-libav \
gstreamer1.0-doc \
gstreamer1.0-tools \
libpulse-mainloop-glib0 \
alsa-base \
alsa-utils \
pulseaudio