我在使用Qt 5.14的ubuntu 20.04。 QtMdeiaPlayer持续时间始终返回0。 文档指出对SetMedia的调用没有阻塞,因此通话后持续时间将为零,但我已按照文档中的说明连接了信号。
connect(player, &QMediaPlayer::durationChanged, this, [&](qint64 dur) {
qDebug() << "duration = " << dur;
});
此lambda从未被调用。此外,音频正常播放,并且positionChanged的信号按预期工作。 我测试了几种不同格式的音频文件,结果是相同的。 任何想法为什么会发生?
-编辑- 复制最少的代码
// somewhere
static QMediaPlayer *player = new QMediaPlayer;
static void DurationChanged(quint64 duration) {
qDebug() << "durration " << 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
答案 0 :(得分:0)
我使用了Ubuntu 20.04码头工人,并获得了音频的持续时间:
#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();
}
输出:
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
要使音频插件正常工作,请安装以下软件包:
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