playlist: add basic m3u8 support

This commit is contained in:
2025-07-20 00:34:04 +08:00
parent 097b32c70d
commit 010e7162fd
3 changed files with 46 additions and 5 deletions

View File

@ -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)

View File

@ -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<QUrl> 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()) {

View File

@ -61,11 +61,14 @@ public:
void setPlaylist(const QList<QUrl> & url);
Q_INVOKABLE QModelIndex loadPlaylist(const QList<QUrl> & 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);