From 118283b748359c3268f47e382762c90cd5c412ac Mon Sep 17 00:00:00 2001 From: Javier Date: Sun, 19 Sep 2021 22:22:23 +0200 Subject: replace Phonon with QtMultimedia --- mainwindow.cc | 102 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 58 insertions(+), 44 deletions(-) (limited to 'mainwindow.cc') diff --git a/mainwindow.cc b/mainwindow.cc index d8a935c..b66423a 100644 --- a/mainwindow.cc +++ b/mainwindow.cc @@ -25,13 +25,14 @@ #include "mainwindow.h" #include "ui_mainwindow.h" +#define PAPER_REPLAY_SLIDER_SCALE 100LL /* in msec */ + MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), _notebooks(new NotebookModel(this)), _manager(new SmartpenManager(this)), - _media(new Phonon::MediaObject(this)), - _mediaOutput(new Phonon::AudioOutput(this)), + _player(new QMediaPlayer(this)), _replay(new PaperReplay(this)), _replayModel(new PaperReplayModel(_replay, this)), _statusLabel(new QLabel) @@ -42,14 +43,15 @@ MainWindow::MainWindow(QWidget *parent) : ui->notebookTree->header()->setSectionResizeMode(1, QHeaderView::Fixed); ui->notebookTree->header()->setSectionResizeMode(2, QHeaderView::Fixed); ui->notebookTree->expandAll(); + ui->replaySlider->setSingleStep(5000 / PAPER_REPLAY_SLIDER_SCALE); + ui->replaySlider->setPageStep(30000 / PAPER_REPLAY_SLIDER_SCALE); ui->paperReplayView->setModel(_replayModel); ui->paperReplayView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); ui->paperReplayView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed); - Phonon::createPath(_media, _mediaOutput); - _media->setTickInterval(500); - ui->replaySlider->setMediaObject(_media); ui->pauseButton->setVisible(false); ui->statusBar->addWidget(_statusLabel, 1); + _player->setAudioRole(QAudio::VideoRole); + connect(_notebooks, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(handleNotebookRowsInserted(QModelIndex,int,int))); connect(_manager, SIGNAL(pensBeingSynchronizedChanged()), @@ -58,12 +60,14 @@ MainWindow::MainWindow(QWidget *parent) : this, SLOT(handlePenSyncComplete(QString))); connect(_manager, SIGNAL(syncFailed(QString)), this, SLOT(handlePenSyncFailed(QString))); - connect(_media, SIGNAL(stateChanged(Phonon::State,Phonon::State)), - this, SLOT(handleMediaStateChange(Phonon::State))); - connect(_media, SIGNAL(totalTimeChanged(qint64)), - this, SLOT(handleMediaTotalTimeChanged(qint64))); - connect(_media, SIGNAL(tick(qint64)), - this, SLOT(handleMediaTick(qint64))); + connect(_player, SIGNAL(stateChanged(QMediaPlayer::State)), + this, SLOT(handlePlayerStateChanged(QMediaPlayer::State))); + connect(_player, SIGNAL(durationChanged(qint64)), + this, SLOT(handlePlayerDurationChanged(qint64))); + connect(_player, SIGNAL(positionChanged(qint64)), + this, SLOT(handlePlayerPositionChanged(qint64))); + connect(_player, SIGNAL(seekableChanged(bool)), + this, SLOT(handlePlayerSeekableChanged(bool))); QSettings settings; settings.beginGroup("mainwindow"); @@ -168,8 +172,8 @@ void MainWindow::exportCurrentPageAsInkML(const QString &file) void MainWindow::exportCurrentPaperReplayAsAac(const QString &file) { - QString src = _media->currentSource().fileName(); - if (src.isEmpty()) { + QString src = currentPlayerMediaPath(); + if (src.isNull()) { QMessageBox::warning(this, tr("Export audio"), tr("No audio file is selected")); return; @@ -229,75 +233,80 @@ void MainWindow::handlePaperReplayRequested(const QString &file, qint64 time) QString filePath = finfo.canonicalFilePath(); - if (_media->currentSource().fileName() != filePath) { + if (currentPlayerMediaPath() != filePath) { qDebug() << "requesting media " << filePath; - _media->setCurrentSource(QUrl::fromLocalFile(filePath)); + _player->setMedia(QUrl::fromLocalFile(filePath)); } - qDebug() << "requesting media seek to" << time << "/" << _media->totalTime(); + qDebug() << "requesting media seek to" << time << "/" << _player->duration(); - switch (_media->state()) { - case Phonon::PlayingState: - case Phonon::BufferingState: - case Phonon::PausedState: + if (_player->isSeekable()) { + // Media is loaded and ready to go _pendingSeek = 0; - _media->seek(time); - break; - default: + _player->setPosition(time); + } else { + // Otherwise delay the seek until after media starts playing _pendingSeek = time; - break; } - _media->play(); + _player->play(); } void MainWindow::handlePaperReplayPlay() { - _media->play(); + _player->play(); } void MainWindow::handlePaperReplayPause() { - _media->pause(); + _player->pause(); } -void MainWindow::handleMediaStateChange(Phonon::State state) +void MainWindow::handlePaperReplaySliderChanged(int value) +{ + _player->setPosition(value * PAPER_REPLAY_SLIDER_SCALE); +} + +void MainWindow::handlePlayerStateChanged(QMediaPlayer::State state) { switch (state) { - case Phonon::PlayingState: + case QMediaPlayer::PlayingState: ui->playButton->setVisible(false); ui->pauseButton->setVisible(true); - if (_pendingSeek) { - qDebug() << "requesting (pending) media seek to" << _pendingSeek << "/" << _media->totalTime(); - _media->seek(_pendingSeek); - _pendingSeek = 0; - } - ui->mediaPosLabel->setText(formatDuration(_media->currentTime())); - ui->mediaLenLabel->setText("/ " + formatDuration(_media->totalTime())); break; - case Phonon::PausedState: + case QMediaPlayer::PausedState: ui->playButton->setVisible(true); ui->pauseButton->setVisible(false); - ui->mediaPosLabel->setText(formatDuration(_media->currentTime())); - ui->mediaLenLabel->setText("/ " + formatDuration(_media->totalTime())); break; default: ui->playButton->setVisible(true); ui->pauseButton->setVisible(false); - ui->mediaPosLabel->setText(QString()); - ui->mediaLenLabel->setText(QString()); break; } } -void MainWindow::handleMediaTotalTimeChanged(qint64 time) +void MainWindow::handlePlayerDurationChanged(qint64 time) { ui->mediaLenLabel->setText("/ " + formatDuration(time)); + ui->replaySlider->setMaximum(time / PAPER_REPLAY_SLIDER_SCALE); } -void MainWindow::handleMediaTick(qint64 time) +void MainWindow::handlePlayerPositionChanged(qint64 time) { ui->mediaPosLabel->setText(formatDuration(time)); + if (!ui->replaySlider->isSliderDown()) { + QSignalBlocker blocker(ui->replaySlider); + ui->replaySlider->setValue(time / PAPER_REPLAY_SLIDER_SCALE); + } +} + +void MainWindow::handlePlayerSeekableChanged(bool seekable) +{ + if (seekable && _pendingSeek) { + qDebug() << "requesting (pending) media seek to" << _pendingSeek << "/" << _player->duration(); + _player->setPosition(_pendingSeek); + _pendingSeek = 0; + } } void MainWindow::handlePensBeingSynchronizedChanged() @@ -431,7 +440,7 @@ void MainWindow::closeEvent(QCloseEvent *event) settings.endGroup(); } -QString MainWindow::formatDuration(qint64 time) +QString MainWindow::formatDuration(qint64 time) const { int secs = time / 1000; int mins = secs / 60; @@ -446,3 +455,8 @@ QString MainWindow::formatDuration(qint64 time) return QString("%2:%3").arg(mins).arg(secs, 2, 10, fill); } } + +QString MainWindow::currentPlayerMediaPath() const +{ + return _player->media().request().url().toLocalFile(); +} -- cgit v1.2.3