QtMediaPlayer 持续时间始终为 0
QtMediaPlayer duration is always 0
我在 ubuntu 20.04 使用 Qt 5.14。
QtMdeiaPlayer 持续时间总是 returns 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
我使用了 ubuntu 20.04 docker 我得到了音频的持续时间:
#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
我在 ubuntu 20.04 使用 Qt 5.14。 QtMdeiaPlayer 持续时间总是 returns 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
我使用了 ubuntu 20.04 docker 我得到了音频的持续时间:
#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