From 3aa4b836d267718aeaeae8dc54cdc0a2d597b978 Mon Sep 17 00:00:00 2001 From: Bart De Vries Date: Sun, 11 Apr 2021 16:19:58 +0200 Subject: [PATCH] Make PlayerManager work Still one issue with the minimized player opening on startup --- src/audiomanager.cpp | 12 ++++++++++++ src/audiomanager.h | 16 +++++++++++----- src/main.cpp | 8 ++++++-- src/qml/main.qml | 29 +++++++++++++++++++++++++---- 4 files changed, 54 insertions(+), 11 deletions(-) diff --git a/src/audiomanager.cpp b/src/audiomanager.cpp index 81e2f81a..dba73044 100644 --- a/src/audiomanager.cpp +++ b/src/audiomanager.cpp @@ -20,6 +20,7 @@ private: QMediaPlayer mPlayer; Entry* entry = nullptr; + bool playerOpen = false; friend class AudioManager; @@ -50,6 +51,11 @@ Entry* AudioManager::entry () const return d->entry; } +bool AudioManager::playerOpen() const +{ + return d->playerOpen; +} + bool AudioManager::muted() const { return d->mPlayer.isMuted(); @@ -108,6 +114,12 @@ void AudioManager::setEntry(Entry* entry) Q_EMIT entryChanged(); } +void AudioManager::setPlayerOpen(bool state) +{ + d->playerOpen = state; + Q_EMIT playerOpenChanged(); +} + void AudioManager::setMuted(bool muted) { d->mPlayer.setMuted(muted); diff --git a/src/audiomanager.h b/src/audiomanager.h index 3a15188d..6f876f0c 100644 --- a/src/audiomanager.h +++ b/src/audiomanager.h @@ -22,7 +22,9 @@ class AudioManager : public QObject Q_OBJECT Q_PROPERTY(bool playerOpen - MEMBER playerOpen) + READ playerOpen + WRITE setPlayerOpen + NOTIFY playerOpenChanged) Q_PROPERTY(Entry* entry READ entry @@ -71,6 +73,8 @@ class AudioManager : public QObject public: + explicit AudioManager(QObject *parent = nullptr); + static AudioManager &instance() { static AudioManager _instance; @@ -81,6 +85,8 @@ public: [[nodiscard]] Entry* entry() const; + [[nodiscard]] bool playerOpen() const; + [[nodiscard]] bool muted() const; [[nodiscard]] qreal volume() const; @@ -101,6 +107,8 @@ public: Q_SIGNALS: + void playerOpenChanged(); + void entryChanged(); void mutedChanged(bool muted); @@ -131,6 +139,8 @@ public Q_SLOTS: void setEntry(Entry* entry); + void setPlayerOpen(bool state); + void setMuted(bool muted); void setVolume(qreal volume); @@ -159,12 +169,8 @@ private Q_SLOTS: private: - explicit AudioManager(QObject *parent = nullptr); - friend class AudioManagerPrivate; std::unique_ptr d; - bool playerOpen; - }; diff --git a/src/main.cpp b/src/main.cpp index afa1ab2c..1a87112f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,10 +68,12 @@ int main(int argc, char *argv[]) engine->setObjectOwnership(SettingsManager::self(), QQmlEngine::CppOwnership); return SettingsManager::self(); }); - qmlRegisterSingletonType("org.kde.alligator", 1, 0, "AudioManager", [](QQmlEngine *engine, QJSEngine *) -> QObject * { + /*qmlRegisterSingletonType("org.kde.alligator", 1, 0, "AudioManager", [](QQmlEngine *engine, QJSEngine *) -> QObject * { engine->setObjectOwnership(&AudioManager::instance(), QQmlEngine::CppOwnership); return &AudioManager::instance(); - }); + });*/ + qmlRegisterType("org.kde.alligator", 1, 0, "AudioManager"); + qRegisterMetaType("const Entry*"); // "hack" to make qml understand Entry* QQmlApplicationEngine engine; @@ -98,6 +100,8 @@ int main(int argc, char *argv[]) DataManager::instance(); + //AudioManager::instance(); + engine.load(QUrl(QStringLiteral("qrc:///main.qml"))); if (engine.rootObjects().isEmpty()) { diff --git a/src/qml/main.qml b/src/qml/main.qml index 49bfe05b..c91e2062 100644 --- a/src/qml/main.qml +++ b/src/qml/main.qml @@ -60,6 +60,12 @@ Kirigami.ApplicationWindow { ] } + AudioManager { + id: audio + playerOpen: false + source: entry ? (entry.enclosure ? "file://" + entry.enclosure.path : "") : "" + } + Component { id: aboutPage Kirigami.AboutPage { @@ -79,19 +85,34 @@ Kirigami.ApplicationWindow { id: queueList } + /* Audio { id: audio property var entry: SettingsManager.lastPlayingEntry !== "none" ? DataManager.getEntry(SettingsManager.lastPlayingEntry) : undefined property bool playerOpen: false - onEntryChanged: SettingsManager.lastPlayingEntry = entry.id - source: entry ? (entry.enclosure ? "file://" + entry.enclosure.path : "") : "" + 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\"" + source: "gst-pipeline: playbin uri=file://" + entry.enclosure.path + " audio_sink=\"scaletempo ! audioconvert ! audioresample ! autoaudiosink\" video_sink=\"fakevideosink\"" } - + */ footer: Loader { active: (audio.entry !== undefined) && !audio.playerOpen