diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/MediaPlayer.qml | 1 | ||||
-rw-r--r-- | app/app.pro | 8 | ||||
-rw-r--r-- | app/main.cpp | 4 | ||||
-rw-r--r-- | app/playlistwithmetadata.cpp | 227 | ||||
-rw-r--r-- | app/playlistwithmetadata.h | 55 |
5 files changed, 2 insertions, 293 deletions
diff --git a/app/MediaPlayer.qml b/app/MediaPlayer.qml index bcf09cf..6c4a185 100644 --- a/app/MediaPlayer.qml +++ b/app/MediaPlayer.qml @@ -19,7 +19,6 @@ import QtQuick.Layouts 1.1 import QtQuick.Controls 2.0 import QtMultimedia 5.6 import AGL.Demo.Controls 1.0 -import MediaPlayer 1.0 import 'api' as API ApplicationWindow { diff --git a/app/app.pro b/app/app.pro index 23ecfea..52bd496 100644 --- a/app/app.pro +++ b/app/app.pro @@ -1,11 +1,7 @@ TARGET = mediaplayer -QT = quickcontrols2 multimedia +QT = quickcontrols2 -HEADERS += \ - playlistwithmetadata.h - -SOURCES = main.cpp \ - playlistwithmetadata.cpp +SOURCES = main.cpp RESOURCES += \ mediaplayer.qrc \ diff --git a/app/main.cpp b/app/main.cpp index 63e408a..c3e0fb0 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -29,8 +29,6 @@ #include <libhomescreen.hpp> #endif -#include "playlistwithmetadata.h" - int main(int argc, char *argv[]) { #ifdef HAVE_LIBHOMESCREEN @@ -46,8 +44,6 @@ int main(int argc, char *argv[]) QQuickStyle::setStyle("AGL"); - qmlRegisterType<PlaylistWithMetadata>("MediaPlayer", 1, 0, "PlaylistWithMetadata"); - QQmlApplicationEngine engine; QQmlContext *context = engine.rootContext(); diff --git a/app/playlistwithmetadata.cpp b/app/playlistwithmetadata.cpp deleted file mode 100644 index 9e2e614..0000000 --- a/app/playlistwithmetadata.cpp +++ /dev/null @@ -1,227 +0,0 @@ -/* - * Copyright (C) 2016 The Qt Company Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "playlistwithmetadata.h" - -#include <QtCore/QDebug> -#include <QtCore/QBuffer> -#include <QtGui/QImage> -#include <QtGui/QImageWriter> -#include <QtMultimedia/QMediaPlayer> -#include <QtMultimedia/QMediaMetaData> - -class PlaylistWithMetadata::Private -{ -public: - Private(PlaylistWithMetadata *parent); - - void disconnect(); - void connect(); - -private: - void loadMetadata(int row); - -private: - PlaylistWithMetadata *q; - -public: - QAbstractListModel *source; - QList<QMetaObject::Connection> connections; - QList<QUrl> urls; - QHash<QUrl, QString> title; - QHash<QUrl, QString> artist; - QHash<QUrl, QUrl> coverArt; - QHash<QUrl, qint64> duration; - QHash<QUrl, QMediaPlayer *> players; -}; - -PlaylistWithMetadata::Private::Private(PlaylistWithMetadata *parent) - : q(parent) - , source(nullptr) -{ -} - -void PlaylistWithMetadata::Private::disconnect() -{ - if (source) { - for (const auto &connection : connections) - q->disconnect(connection); - connections.clear(); - } -} - -void PlaylistWithMetadata::Private::connect() -{ - if (source) { - connections.append(q->connect(source, &QAbstractListModel::rowsAboutToBeInserted, [&](const QModelIndex &parent, int first, int last) { - Q_UNUSED(parent) - q->beginInsertRows(QModelIndex(), first, last); - })); - connections.append(q->connect(source, &QAbstractListModel::rowsInserted, [&](const QModelIndex &parent, int first, int last) { - Q_UNUSED(parent) - for (int i = first; i <= last; i++) { - loadMetadata(i); - } - q->endInsertRows(); - })); - - connections.append(q->connect(source, &QAbstractListModel::rowsAboutToBeRemoved, [&](const QModelIndex &parent, int first, int last) { - Q_UNUSED(parent) - q->beginRemoveRows(QModelIndex(), first, last); - })); - connections.append(q->connect(source, &QAbstractListModel::rowsRemoved, [&](const QModelIndex &parent, int first, int last) { - Q_UNUSED(parent) - for (int i = last; i >= first; --i) { - QUrl url = urls.at(i); - urls.removeAt(i); - - players.remove(url); - title.remove(url); - artist.remove(url); - coverArt.remove(url); - duration.remove(url); - } - q->endRemoveRows(); - })); - - int count = source->rowCount(); - if (count > 0) { - q->beginInsertRows(QModelIndex(), 0, count); - for (int i = 0; i < count; i++) { - loadMetadata(i); - } - q->endInsertRows(); - } - } -} - -void PlaylistWithMetadata::Private::loadMetadata(int row) -{ - QUrl url = source->data(source->index(row), Qt::UserRole + 1).toUrl(); - QMediaPlayer *player = new QMediaPlayer(q); - urls.append(url); - players.insert(url, player); - q->connect(player, &QMediaPlayer::mediaStatusChanged, [this, url](QMediaPlayer::MediaStatus mediaStatus) { - switch (mediaStatus) { - case QMediaPlayer::NoMedia: - case QMediaPlayer::LoadedMedia: { - QMediaPlayer *player = players.take(url); - title.insert(url, player->metaData(QMediaMetaData::Title).toString()); - artist.insert(url, player->metaData(QMediaMetaData::ContributingArtist).toString()); - QVariant coverArtImage = player->metaData(QMediaMetaData::CoverArtImage); - if (coverArtImage.type() == QVariant::Image) { - QImage image = coverArtImage.value<QImage>(); - QByteArray data; - QBuffer buffer(&data); - buffer.open(QBuffer::WriteOnly); - QImageWriter png(&buffer, "png"); - if (png.write(image)) { - buffer.close(); - coverArt.insert(url, QUrl(QStringLiteral("data:image/png;base64,") + data.toBase64())); - } - } - duration.insert(url, player->duration()); - QModelIndex index = q->index(urls.indexOf(url)); - q->dataChanged(index, index, QVector<int>() << TitleRole << ArtistRole << CoverArtRole << DurationRole); - player->deleteLater(); - break; } - default: - break; - } - - }); - player->setMedia(url); -} - -PlaylistWithMetadata::PlaylistWithMetadata(QObject *parent) - : QAbstractListModel(parent) - , d(new Private(this)) -{ -} - -PlaylistWithMetadata::~PlaylistWithMetadata() -{ - delete d; -} - -int PlaylistWithMetadata::rowCount(const QModelIndex &parent) const -{ - int ret = 0; - if (parent.isValid()) - return ret; - if (d->source) - ret = d->source->rowCount(QModelIndex()); - return ret; -} - -QVariant PlaylistWithMetadata::data(const QModelIndex &index, int role) const -{ - QVariant ret; - if (!index.isValid()) - return ret; - int row = index.row(); - if (row < 0 || rowCount() <= row) - return ret; - QUrl url = d->urls.at(row); - switch (role) { - case TitleRole: - ret = d->title.value(url); - if (ret.toString().isEmpty()) - ret = QVariant(url.fileName()); - break; - case ArtistRole: - ret = d->artist.value(url); - break; - case CoverArtRole: - ret = d->coverArt.value(url); - break; - case SourceRole: - ret = url; - break; - case DurationRole: - ret = d->duration.value(url); - break; - default: - qWarning() << role; - } - - return ret; -} - -QHash<int, QByteArray> PlaylistWithMetadata::roleNames() const -{ - return { - {TitleRole, "title"}, - {ArtistRole, "artist"}, - {CoverArtRole, "coverArt"}, - {SourceRole, "source"}, - {DurationRole, "duration"} - }; -} - -QAbstractListModel *PlaylistWithMetadata::source() const -{ - return d->source; -} - -void PlaylistWithMetadata::setSource(QAbstractListModel *source) -{ - if (d->source == source) return; - d->disconnect(); - d->source = source; - d->connect(); - emit sourceChanged(source); -} diff --git a/app/playlistwithmetadata.h b/app/playlistwithmetadata.h deleted file mode 100644 index 74cf6f5..0000000 --- a/app/playlistwithmetadata.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (C) 2016 The Qt Company Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PLAYLISTWITHMETADATA_H -#define PLAYLISTWITHMETADATA_H - -#include <QtCore/QAbstractListModel> - -class PlaylistWithMetadata : public QAbstractListModel -{ - Q_OBJECT - Q_PROPERTY(QAbstractListModel *source READ source WRITE setSource NOTIFY sourceChanged) -public: - PlaylistWithMetadata(QObject *parent = nullptr); - ~PlaylistWithMetadata(); - - enum { - TitleRole = Qt::DisplayRole - , ArtistRole = Qt::UserRole + 1 - , CoverArtRole - , SourceRole - , DurationRole - }; - - int rowCount(const QModelIndex &parent = QModelIndex()) const override; - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - QHash<int, QByteArray> roleNames() const override; - - QAbstractListModel *source() const; - -public slots: - void setSource(QAbstractListModel *source); - -signals: - void sourceChanged(QAbstractListModel *source); - -private: - class Private; - Private *d; -}; - -#endif // PLAYLISTWITHMETADATA_H |