From 010e7162fd86ee61404d79061eb8d58dffa039b1 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Sun, 20 Jul 2025 00:34:04 +0800 Subject: [PATCH] playlist: add basic m3u8 support --- mainwindow.cpp | 14 +++++++++++--- playlistmanager.cpp | 34 ++++++++++++++++++++++++++++++++-- playlistmanager.h | 3 +++ 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 6e6027e..352333a 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -261,6 +261,15 @@ void MainWindow::dropEvent(QDropEvent *e) return; } + if (fileName.endsWith(".m3u") || fileName.endsWith(".m3u8")) { + const QModelIndex & modelIndex = m_playlistManager->loadM3U8Playlist(urls.constFirst()); + if (modelIndex.isValid()) { + loadByModelIndex(modelIndex); + play(); + } + return; + } + const QModelIndex & modelIndex = m_playlistManager->loadPlaylist(urls); if (modelIndex.isValid()) { loadByModelIndex(modelIndex); @@ -292,9 +301,8 @@ void MainWindow::loadFile() urlList.append(QUrl::fromLocalFile(fileName)); } - m_playlistManager->loadPlaylist(urlList); - const QUrl & firstUrl = urlList.first(); - loadFile(firstUrl); + const QModelIndex & modelIndex = m_playlistManager->loadPlaylist(urlList); + loadByModelIndex(modelIndex); } void MainWindow::loadFile(const QUrl &url) diff --git a/playlistmanager.cpp b/playlistmanager.cpp index c3a17ac..e3dafd7 100644 --- a/playlistmanager.cpp +++ b/playlistmanager.cpp @@ -186,6 +186,26 @@ QModelIndex PlaylistManager::loadPlaylist(const QUrl &url) return idx; } +QModelIndex PlaylistManager::loadM3U8Playlist(const QUrl &url) +{ + QFile file(url.toLocalFile()); + if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { + QList urls; + while (!file.atEnd()) { + QString line = file.readLine(); + if (line.startsWith('#')) { + continue; + } + QFileInfo fileInfo(file); + QUrl item = QUrl::fromUserInput(line, fileInfo.absolutePath()); + urls.append(item); + } + return loadPlaylist(urls); + } else { + return {}; + } +} + int PlaylistManager::totalCount() const { return m_model.rowCount(); @@ -196,7 +216,7 @@ QModelIndex PlaylistManager::previousIndex() const int count = totalCount(); if (count == 0) return {}; - return m_model.index(m_currentIndex - 1 < 0 ? count - 1 : m_currentIndex - 1); + return m_model.index(isFirstIndex() ? count - 1 : m_currentIndex - 1); } QModelIndex PlaylistManager::nextIndex() const @@ -204,7 +224,7 @@ QModelIndex PlaylistManager::nextIndex() const int count = totalCount(); if (count == 0) return {}; - return m_model.index(m_currentIndex + 1 == count ? 0 : m_currentIndex + 1); + return m_model.index(isLastIndex() ? 0 : m_currentIndex + 1); } QModelIndex PlaylistManager::curIndex() const @@ -212,6 +232,16 @@ QModelIndex PlaylistManager::curIndex() const return m_model.index(m_currentIndex); } +bool PlaylistManager::isFirstIndex() const +{ + return m_currentIndex == 0; +} + +bool PlaylistManager::isLastIndex() const +{ + return m_currentIndex + 1 == totalCount(); +} + void PlaylistManager::setCurrentIndex(const QModelIndex &index) { if (index.isValid() && index.row() >= 0 && index.row() < totalCount()) { diff --git a/playlistmanager.h b/playlistmanager.h index 9fe8b5b..6d496ef 100644 --- a/playlistmanager.h +++ b/playlistmanager.h @@ -61,11 +61,14 @@ public: void setPlaylist(const QList & url); Q_INVOKABLE QModelIndex loadPlaylist(const QList & urls); Q_INVOKABLE QModelIndex loadPlaylist(const QUrl & url); + Q_INVOKABLE QModelIndex loadM3U8Playlist(const QUrl & url); int totalCount() const; QModelIndex previousIndex() const; QModelIndex nextIndex() const; QModelIndex curIndex() const; + bool isFirstIndex() const; + bool isLastIndex() const; void setCurrentIndex(const QModelIndex & index); QUrl urlByIndex(const QModelIndex & index); QString localFileByIndex(const QModelIndex & index);