fix: don't use currentIndexChanged to load song

This commit is contained in:
Gary Wang 2024-09-07 12:14:54 +08:00
parent 413711d073
commit d28108f2e5
No known key found for this signature in database
GPG Key ID: 5D30A4F15EA78760
5 changed files with 58 additions and 51 deletions

View File

@ -39,10 +39,10 @@ jobs:
:: ===== taglib ===== :: ===== taglib =====
git clone --recurse-submodules -q https://github.com/taglib/taglib.git dependencies_src/taglib git clone --recurse-submodules -q https://github.com/taglib/taglib.git dependencies_src/taglib
cmake .\dependencies_src\taglib -Bbuild_dependencies/taglib -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX="dependencies_bin" || goto :error cmake .\dependencies_src\taglib -Bbuild_dependencies/taglib -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX="dependencies_bin" || goto :error
cmake --build build_dependencies/taglib --config Release --target=install || goto :error cmake --build build_dependencies/taglib --config Release --target=install -j || goto :error
:: ------ app ------ :: ------ app ------
cmake -Bbuild . -DCMAKE_INSTALL_PREFIX="%PWD%\build\" || goto :error cmake -Bbuild . -DCMAKE_INSTALL_PREFIX="%PWD%\build\" || goto :error
cmake --build build --config Release || goto :error cmake --build build --config Release -j || goto :error
cmake --build build --config Release --target=install cmake --build build --config Release --target=install
:: ------ pkg ------ :: ------ pkg ------
windeployqt --verbose=2 --no-quick-import --no-translations --no-opengl-sw --no-system-d3d-compiler --no-system-dxc-compiler --multimedia --skip-plugin-types tls,networkinformation build\bin\pmusic.exe windeployqt --verbose=2 --no-quick-import --no-translations --no-opengl-sw --no-system-d3d-compiler --no-system-dxc-compiler --multimedia --skip-plugin-types tls,networkinformation build\bin\pmusic.exe

View File

@ -45,15 +45,16 @@ set (EXE_NAME pmusic)
file (GLOB PMUSIC_TS_FILES languages/*.ts) file (GLOB PMUSIC_TS_FILES languages/*.ts)
set (PMUSIC_CPP_FILES_FOR_I18N ${PMUSIC_CPP_FILES} ${PMUSIC_UI_FILES}) set (PMUSIC_CPP_FILES_FOR_I18N ${PMUSIC_CPP_FILES} ${PMUSIC_UI_FILES})
qt_create_translation(PMUSIC_QM_FILES ${PMUSIC_CPP_FILES_FOR_I18N} ${PMUSIC_TS_FILES})
add_executable(${EXE_NAME} add_executable(${EXE_NAME}
${PMUSIC_HEADER_FILES} ${PMUSIC_HEADER_FILES}
${PMUSIC_CPP_FILES} ${PMUSIC_CPP_FILES}
${PMUSIC_UI_FILES} ${PMUSIC_UI_FILES}
resources.qrc resources.qrc
)
${PMUSIC_QM_FILES} qt_add_translations(${EXE_NAME}
TS_FILES
${PMUSIC_TS_FILES}
) )
if (NOT TagLib_FOUND) if (NOT TagLib_FOUND)
@ -108,17 +109,3 @@ install (
TARGETS ${EXE_NAME} TARGETS ${EXE_NAME}
${INSTALL_TARGETS_DEFAULT_ARGS} ${INSTALL_TARGETS_DEFAULT_ARGS}
) )
if (WIN32)
set (QM_FILE_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}/translations")
else ()
set (QM_FILE_INSTALL_DIR "${CMAKE_INSTALL_FULL_DATADIR}/pineapple-music/translations")
target_compile_definitions(${EXE_NAME}
PRIVATE QM_FILE_INSTALL_DIR=${QM_FILE_INSTALL_DIR}
)
endif ()
install (
FILES ${PMUSIC_QM_FILES}
DESTINATION ${QM_FILE_INSTALL_DIR}
)

View File

@ -14,13 +14,9 @@ int main(int argc, char *argv[])
QApplication a(argc, argv); QApplication a(argc, argv);
QTranslator translator; QTranslator translator;
QString qmDir; if (translator.load(QLocale(), QLatin1String("pineapple-music"), QLatin1String("_"), QLatin1String(":/i18n"))) {
#ifdef _WIN32 a.installTranslator(&translator);
qmDir = QDir(QCoreApplication::applicationDirPath()).absoluteFilePath("translations"); }
#else
qmDir = QT_STRINGIFY(QM_FILE_INSTALL_DIR);
#endif
translator.load(QString("pineapple-music_%1").arg(QLocale::system().name()), qmDir);
a.installTranslator(&translator); a.installTranslator(&translator);
// parse commandline arguments // parse commandline arguments

View File

@ -58,6 +58,10 @@ void MainWindow::commandlinePlayAudioFiles(QStringList audioFiles)
if (!audioFileUrls.isEmpty()) { if (!audioFileUrls.isEmpty()) {
QModelIndex modelIndex = m_playlistManager->loadPlaylist(audioFileUrls); QModelIndex modelIndex = m_playlistManager->loadPlaylist(audioFileUrls);
if (modelIndex.isValid()) {
m_mediaPlayer->setSource(m_playlistManager->urlByIndex(modelIndex));
play();
}
} }
} }
@ -190,7 +194,11 @@ void MainWindow::dropEvent(QDropEvent *e)
return; return;
} }
m_playlistManager->loadPlaylist(urls); QModelIndex modelIndex = m_playlistManager->loadPlaylist(urls);
if (modelIndex.isValid()) {
m_mediaPlayer->setSource(m_playlistManager->urlByIndex(modelIndex));
play();
}
} }
void MainWindow::loadFile() void MainWindow::loadFile()
@ -205,6 +213,32 @@ void MainWindow::loadFile()
} }
m_playlistManager->loadPlaylist(urlList); m_playlistManager->loadPlaylist(urlList);
m_mediaPlayer->setSource(urlList.first());
}
void MainWindow::play()
{
QUrl fileUrl(m_mediaPlayer->source());
m_mediaPlayer->play();
ui->titleLabel->setText(fileUrl.fileName());
ui->titleLabel->setToolTip(fileUrl.fileName());
if (fileUrl.isLocalFile()) {
QString filePath(fileUrl.toLocalFile());
QString suffix(filePath.mid(filePath.lastIndexOf('.') + 1));
suffix = suffix.toUpper();
#ifndef NO_TAGLIB
TagLib::FileRef fileRef(filePath.toLocal8Bit().data());
if (!fileRef.isNull() && fileRef.audioProperties()) {
TagLib::AudioProperties *prop = fileRef.audioProperties();
setAudioPropertyInfoForDisplay(prop->sampleRate(), prop->bitrate(), prop->channels(), suffix);
}
#endif // NO_TAGLIB
}
} }
void MainWindow::centerWindow() void MainWindow::centerWindow()
@ -228,7 +262,7 @@ void MainWindow::on_playBtn_clicked()
{ {
if (m_mediaPlayer->mediaStatus() == QMediaPlayer::NoMedia) { if (m_mediaPlayer->mediaStatus() == QMediaPlayer::NoMedia) {
loadFile(); loadFile();
m_mediaPlayer->play(); play();
} else if (m_mediaPlayer->mediaStatus() == QMediaPlayer::InvalidMedia) { } else if (m_mediaPlayer->mediaStatus() == QMediaPlayer::InvalidMedia) {
ui->propLabel->setText("Error: InvalidMedia" + m_mediaPlayer->errorString()); ui->propLabel->setText("Error: InvalidMedia" + m_mediaPlayer->errorString());
} else { } else {
@ -287,12 +321,18 @@ void MainWindow::on_playbackSlider_valueChanged(int value)
void MainWindow::on_prevBtn_clicked() void MainWindow::on_prevBtn_clicked()
{ {
m_playlistManager->setCurrentIndex(m_playlistManager->previousIndex()); QModelIndex index(m_playlistManager->previousIndex());
m_playlistManager->setCurrentIndex(index);
m_mediaPlayer->setSource(m_playlistManager->urlByIndex(index));
play();
} }
void MainWindow::on_nextBtn_clicked() void MainWindow::on_nextBtn_clicked()
{ {
m_playlistManager->setCurrentIndex(m_playlistManager->nextIndex()); QModelIndex index(m_playlistManager->nextIndex());
m_playlistManager->setCurrentIndex(index);
m_mediaPlayer->setSource(m_playlistManager->urlByIndex(index));
play();
} }
void MainWindow::on_volumeBtn_clicked() void MainWindow::on_volumeBtn_clicked()
@ -336,27 +376,7 @@ void MainWindow::initConnections()
} }
}); });
connect(m_playlistManager, &PlaylistManager::currentIndexChanged, this, [=](int index){ connect(m_playlistManager, &PlaylistManager::currentIndexChanged, this, [=](int index){
QUrl fileUrl(m_playlistManager->model()->urlByIndex(index)); ui->playlistView->setCurrentIndex(m_playlistManager->model()->index(index));
m_mediaPlayer->setSource(fileUrl);
m_mediaPlayer->play();
ui->titleLabel->setText(fileUrl.fileName());
ui->titleLabel->setToolTip(fileUrl.fileName());
if (fileUrl.isLocalFile()) {
QString filePath(fileUrl.toLocalFile());
QString suffix(filePath.mid(filePath.lastIndexOf('.') + 1));
suffix = suffix.toUpper();
#ifndef NO_TAGLIB
TagLib::FileRef fileRef(filePath.toLocal8Bit().data());
if (!fileRef.isNull() && fileRef.audioProperties()) {
TagLib::AudioProperties *prop = fileRef.audioProperties();
setAudioPropertyInfoForDisplay(prop->sampleRate(), prop->bitrate(), prop->channels(), suffix);
}
#endif // NO_TAGLIB
}
}); });
connect(m_mediaPlayer, &QMediaPlayer::positionChanged, this, [=](qint64 pos) { connect(m_mediaPlayer, &QMediaPlayer::positionChanged, this, [=](qint64 pos) {
@ -458,5 +478,7 @@ void MainWindow::on_playListBtn_clicked()
void MainWindow::on_playlistView_activated(const QModelIndex &index) void MainWindow::on_playlistView_activated(const QModelIndex &index)
{ {
m_playlistManager->setCurrentIndex(index); m_playlistManager->setCurrentIndex(index);
m_mediaPlayer->setSource(m_playlistManager->urlByIndex(index));
play();
} }

View File

@ -47,6 +47,8 @@ protected:
void dropEvent(QDropEvent *e) override; void dropEvent(QDropEvent *e) override;
void loadFile(); void loadFile();
void play();
void centerWindow(); void centerWindow();
private slots: private slots: