QMediaPlayer out of sync
-
Hello,
I am currently developing a program with Qt 5.12.3 and I have come up against an issue with QMediaPlayer.
The QMediaPlayer is created using:
//audio.h QMediaPlayer *player = new QMediaPlayer(); Q_OBJECT Q_PROPERTY(long long audioPosition READ audioPosition WRITE setAudioPosition NOTIFY audioPositionChanged) //audio.cpp Audio::Audio(QObject *parent) : QObject(parent) { player->setMedia((QUrl("qrc:/sounds/test.mp3"))); connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64))); }
It is playing an .mp3 file approximately 43 minutes in length.
The program has a slider which is used to control the position of the player:
Slider { id: slider rotation: 90 minimumValue: 0 maximumValue: 2571833 //audio.getAudioLength() - Both have been tried stepSize: 1000 anchors.centerIn: imageClip height: imageClip.width width: 3310 onPressedChanged: { audio.audioPosition = value } }
The program has a Vector of long long ints which stores timestamps in milliseconds. Once a timestamp is reached, a corresponding item is displayed.
The program has a text component which displays the current position of the audio, done using:
text: new Date(audio.audioPosition).toLocaleTimeString(Qt.locale(), "mm" + ":" + "ss")
The problem is, when I move the slider and update the position of the QMediaPlayer with:
void Audio::setAudioPosition(qint64 position) { qInfo() << "position: " << position; m_audioPosition = position; player->setPosition(position); emit positionChanged(position); }
the audio moves close to this position, but it is slightly behind. The position chosen is correctly shown by the text item, the timestamps are reached at the correct time, and the corresponding items are shown, but the actual audio is behind. This is not noticeable at the start of the file and progressively gets worse as the audio progresses. If I change the position to near the end of the file the audio track is about 7 seconds behind the position shown in the text component.
If I allow the audio to play through without changing the position there is no issue and everything stays in sync.
I have tried pausing the audio before changing the position, setting the position back to 0 then updating it. I have also tried using a QML audio player but the issue remained.
I am developing on macOS Mojave 10.14.6.
I would be very grateful for any help.
Thanks -
In case anyone comes across this question with a similar problem:
I have been unable to resolve the issue with the Qt Media Player. I therefore tried the vlc-qt media player https://vlc-qt.tano.si which solves the issue. The process to get this setup is a bit more involved but once I got it installed, swapping QMediaPlayer for the vlc version works well and the media stays in sync.