diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6879eebc4..8727c0efc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -161,6 +161,8 @@ add_library(neochat STATIC enums/neochatroomtype.h models/sortfilterroomtreemodel.cpp models/sortfilterroomtreemodel.h + mediamanager.cpp + mediamanager.h ) qt_add_qml_module(neochat URI org.kde.neochat NO_PLUGIN diff --git a/src/mediamanager.cpp b/src/mediamanager.cpp new file mode 100644 index 000000000..e82f3b5f0 --- /dev/null +++ b/src/mediamanager.cpp @@ -0,0 +1,9 @@ +// SPDX-FileCopyrightText: 2023 Tobias Fella +// SPDX-License-Identifier: LGPL-2.0-or-later + +#include "mediamanager.h" + +void MediaManager::startPlayback() +{ + Q_EMIT playbackStarted(); +} diff --git a/src/mediamanager.h b/src/mediamanager.h new file mode 100644 index 000000000..b5386c73f --- /dev/null +++ b/src/mediamanager.h @@ -0,0 +1,42 @@ +// SPDX-FileCopyrightText: 2023 Tobias Fella +// SPDX-License-Identifier: LGPL-2.0-or-later + +#pragma once + +#include +#include + +/** + * @class MediaManager + * + * Manages media playback, like voice/audio messages, videos, etc. + */ +class MediaManager : public QObject +{ + Q_OBJECT + QML_ELEMENT + QML_SINGLETON + +public: + static MediaManager &instance() + { + static MediaManager _instance; + return _instance; + } + static MediaManager *create(QQmlEngine *, QJSEngine *) + { + QQmlEngine::setObjectOwnership(&instance(), QQmlEngine::CppOwnership); + return &instance(); + } + + /** + * @brief Notify other objects that media playback has started. + */ + Q_INVOKABLE void startPlayback(); + +Q_SIGNALS: + /** + * @brief Emitted when any media player starts playing. Other objects should stop / pause playback. + */ + void playbackStarted(); +}; diff --git a/src/qml/AudioComponent.qml b/src/qml/AudioComponent.qml index dbcfb9e07..fee3a6017 100644 --- a/src/qml/AudioComponent.qml +++ b/src/qml/AudioComponent.qml @@ -105,6 +105,7 @@ ColumnLayout { icon.name: "media-playback-start" onClicked: { audio.source = root.fileTransferInfo.localPath; + MediaManager.startPlayback(); audio.play(); } } @@ -123,6 +124,15 @@ ColumnLayout { } ] + Connections { + target: MediaManager + function onPlaybackStarted() { + if (audio.playbackState === MediaPlayer.PlayingState) { + audio.pause(); + } + } + } + RowLayout { QQC2.ToolButton { id: playButton diff --git a/src/qml/VideoComponent.qml b/src/qml/VideoComponent.qml index 025bddb94..d8f5d362c 100644 --- a/src/qml/VideoComponent.qml +++ b/src/qml/VideoComponent.qml @@ -118,7 +118,10 @@ Video { PropertyChanges { target: playButton icon.name: "media-playback-start" - onClicked: root.play() + onClicked: { + MediaManager.startPlayback(); + root.play(); + } } }, State { @@ -136,6 +139,15 @@ Video { } ] + Connections { + target: MediaManager + function onPlaybackStarted() { + if (root.playbackState === MediaPlayer.PlayingState) { + root.pause(); + } + } + } + Image { id: mediaThumbnail anchors.fill: parent @@ -350,6 +362,7 @@ Video { if (root.playbackState == MediaPlayer.PlayingState) { root.pause(); } else { + MediaManager.startPlayback(); root.play(); } } else { @@ -375,6 +388,7 @@ Video { function playSavedFile() { root.stop(); + MediaManager.startPlayback(); root.play(); } }