From 129707009c6e4b3ff3beb2077495399e3c33899d Mon Sep 17 00:00:00 2001 From: Bart De Vries Date: Sun, 11 Apr 2021 18:55:09 +0200 Subject: [PATCH] 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. --- src/audiomanager.cpp | 25 +++++++++++++++++++++++++ src/qml/main.qml | 36 ++++-------------------------------- 2 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/audiomanager.cpp b/src/audiomanager.cpp index d91fb78e..d6648354 100644 --- a/src/audiomanager.cpp +++ b/src/audiomanager.cpp @@ -10,6 +10,7 @@ #include #include +#include class AudioManagerPrivate { @@ -120,6 +121,30 @@ void AudioManager::setEntry(Entry* entry) { if (entry != nullptr) { 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); } } diff --git a/src/qml/main.qml b/src/qml/main.qml index badc0d6f..5a7327a8 100644 --- a/src/qml/main.qml +++ b/src/qml/main.qml @@ -60,12 +60,6 @@ Kirigami.ApplicationWindow { ] } - AudioManager { - id: audio - playerOpen: false - source: entry ? (entry.enclosure ? "file://" + entry.enclosure.path : "") : "" - } - Component { id: aboutPage Kirigami.AboutPage { @@ -85,34 +79,12 @@ Kirigami.ApplicationWindow { id: queueList } - /* - Audio { + AudioManager { id: audio - - property var entry: SettingsManager.lastPlayingEntry !== "none" ? DataManager.getEntry(SettingsManager.lastPlayingEntry) : undefined - property bool playerOpen: false - - 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\"" + playerOpen: false + //onPositionChanged: console.log("position changed to", position) + //onSeekableChanged: console.log("seekable changed to", seekable) } - */ footer: Loader { active: (audio.entry != undefined) && !audio.playerOpen