Implement the framework for custom audio starting position

What's still needed is the backend for saving the current position
and making the audio player aware of that position when the entry
is changed.
This commit is contained in:
Bart De Vries 2021-04-11 18:55:09 +02:00
parent 527ed67d4a
commit 129707009c
2 changed files with 29 additions and 32 deletions

View File

@ -10,6 +10,7 @@
#include <QTimer> #include <QTimer>
#include <QAudio> #include <QAudio>
#include <QEventLoop>
class AudioManagerPrivate class AudioManagerPrivate
{ {
@ -120,6 +121,30 @@ void AudioManager::setEntry(Entry* entry)
{ {
if (entry != nullptr) { if (entry != nullptr) {
d->m_entry = entry; d->m_entry = entry;
d->m_player.setMedia(QUrl(QStringLiteral("file://")+d->m_entry->enclosure()->path()));
qint64 startingPosition = 0;
// What follows is a dirty hack to get the player positioned at the
// correct spot. The audio only becomes seekable when the player is
// actually playing. So we start the playback and then set a timer to
// wait until the stream becomes seekable; then switch position and
// immediately pause the playback.
// Unfortunately, this will produce an audible glitch with the current
// QMediaPlayer backend.
d->m_player.play();
if(!d->m_player.isSeekable()) {
QEventLoop loop;
QTimer timer;
timer.setSingleShot(true);
timer.setInterval(2000);
loop.connect(&timer, SIGNAL (timeout()), &loop, SLOT (quit()) );
loop.connect(&d->m_player, SIGNAL (seekableChanged(bool)), &loop, SLOT (quit()));
qDebug() << "Starting waiting loop";
loop.exec();
}
qDebug() << "Changing position";
if (startingPosition > 1000) d->m_player.setPosition(startingPosition);
d->m_player.pause();
Q_EMIT entryChanged(entry); Q_EMIT entryChanged(entry);
} }
} }

View File

@ -60,12 +60,6 @@ Kirigami.ApplicationWindow {
] ]
} }
AudioManager {
id: audio
playerOpen: false
source: entry ? (entry.enclosure ? "file://" + entry.enclosure.path : "") : ""
}
Component { Component {
id: aboutPage id: aboutPage
Kirigami.AboutPage { Kirigami.AboutPage {
@ -85,34 +79,12 @@ Kirigami.ApplicationWindow {
id: queueList id: queueList
} }
/* AudioManager {
Audio {
id: audio id: audio
playerOpen: false
property var entry: SettingsManager.lastPlayingEntry !== "none" ? DataManager.getEntry(SettingsManager.lastPlayingEntry) : undefined //onPositionChanged: console.log("position changed to", position)
property bool playerOpen: false //onSeekableChanged: console.log("seekable changed to", seekable)
onSourceChanged: {
if (entry !== undefined) {
console.log("Changed track")
SettingsManager.lastPlayingEntry = entry.id
console.log("Between pause and seek", entry.title)
seek(entry.enclosure.playPosition)
}
}
onPositionChanged: {
if (playbackState == Audio.PlayingState) {
entry.enclosure.playPosition = position
}
console.log(entry.title, position, playbackState)
}
//source: entry ? (entry.enclosure ? "file://" + entry.enclosure.path : "") : ""
//source: entry.enclosure.url
onError: console.debug(errorString)
source: "gst-pipeline: playbin uri=file://" + entry.enclosure.path + " audio_sink=\"scaletempo ! audioconvert ! audioresample ! autoaudiosink\" video_sink=\"fakevideosink\""
} }
*/
footer: Loader { footer: Loader {
active: (audio.entry != undefined) && !audio.playerOpen active: (audio.entry != undefined) && !audio.playerOpen