diff --git a/src/core/mainwindow.cpp b/src/core/mainwindow.cpp index e5febbd3b..34b17618e 100644 --- a/src/core/mainwindow.cpp +++ b/src/core/mainwindow.cpp @@ -1432,6 +1432,7 @@ void MainWindow::LoadPlaybackStatus() { if (resume_playback && playback_state != Engine::Empty && playback_state != Engine::Idle) { QObject::connect(app_->playlist_manager(), &PlaylistManager::AllPlaylistsLoaded, this, [this]() { + QObject::disconnect(app_->playlist_manager(), &PlaylistManager::AllPlaylistsLoaded, this, &MainWindow::ResumePlayback); QTimer::singleShot(400, this, &MainWindow::ResumePlayback); }); } @@ -1442,8 +1443,6 @@ void MainWindow::ResumePlayback() { qLog(Debug) << "Resuming playback"; - QObject::disconnect(app_->playlist_manager(), &PlaylistManager::AllPlaylistsLoaded, this, &MainWindow::ResumePlayback); - QSettings s; s.beginGroup(Player::kSettingsGroup); Engine::State playback_state = static_cast(s.value("playback_state", Engine::Empty).toInt()); @@ -1484,7 +1483,7 @@ void MainWindow::PlayIndex(const QModelIndex &idx, Playlist::AutoScroll autoscro } app_->playlist_manager()->SetActiveToCurrent(); - app_->player()->PlayAt(row, Engine::Manual, autoscroll, true); + app_->player()->PlayAt(row, 0, Engine::Manual, autoscroll, true); } @@ -1501,14 +1500,14 @@ void MainWindow::PlaylistDoubleClick(const QModelIndex &idx) { switch (doubleclick_playlist_addmode_) { case BehaviourSettingsPage::PlaylistAddBehaviour_Play: app_->playlist_manager()->SetActiveToCurrent(); - app_->player()->PlayAt(source_idx.row(), Engine::Manual, Playlist::AutoScroll_Never, true, true); + app_->player()->PlayAt(source_idx.row(), 0, Engine::Manual, Playlist::AutoScroll_Never, true, true); break; case BehaviourSettingsPage::PlaylistAddBehaviour_Enqueue: app_->playlist_manager()->current()->queue()->ToggleTracks(QModelIndexList() << source_idx); if (app_->player()->GetState() != Engine::Playing) { app_->playlist_manager()->SetActiveToCurrent(); - app_->player()->PlayAt(app_->playlist_manager()->current()->queue()->TakeNext(), Engine::Manual, Playlist::AutoScroll_Never, true); + app_->player()->PlayAt(app_->playlist_manager()->current()->queue()->TakeNext(), 0, Engine::Manual, Playlist::AutoScroll_Never, true); } break; } @@ -2423,7 +2422,7 @@ void MainWindow::CommandlineOptionsReceived(const CommandlineOptions &options) { app_->player()->SeekTo(app_->player()->engine()->position_nanosec() / kNsecPerSec + options.seek_by()); } - if (options.play_track_at() != -1) app_->player()->PlayAt(options.play_track_at(), Engine::Manual, Playlist::AutoScroll_Maybe, true); + if (options.play_track_at() != -1) app_->player()->PlayAt(options.play_track_at(), 0, Engine::Manual, Playlist::AutoScroll_Maybe, true); if (options.show_osd()) app_->player()->ShowOSD(); diff --git a/src/core/player.cpp b/src/core/player.cpp index 9b0ba3a12..7b91ffbc1 100644 --- a/src/core/player.cpp +++ b/src/core/player.cpp @@ -401,7 +401,7 @@ void Player::NextItem(const Engine::TrackChangeFlags change, const Playlist::Aut return; } - PlayAt(i, change, autoscroll, false, true); + PlayAt(i, 0, change, autoscroll, false, true); } @@ -435,7 +435,7 @@ void Player::PlayPlaylistInternal(const Engine::TrackChangeFlags change, const P if (i == -1) i = app_->playlist_manager()->active()->last_played_row(); if (i == -1) i = 0; - PlayAt(i, change, autoscroll, true); + PlayAt(i, 0, change, autoscroll, true); } @@ -473,8 +473,6 @@ void Player::TrackEnded() { void Player::PlayPause(const quint64 offset_nanosec, const Playlist::AutoScroll autoscroll) { - play_offset_nanosec_ = offset_nanosec; - switch (engine_->state()) { case Engine::Paused: UnPause(); @@ -497,12 +495,13 @@ void Player::PlayPause(const quint64 offset_nanosec, const Playlist::AutoScroll case Engine::Error: case Engine::Idle: { pause_time_ = QDateTime(); + play_offset_nanosec_ = offset_nanosec; app_->playlist_manager()->SetActivePlaylist(app_->playlist_manager()->current_id()); if (app_->playlist_manager()->active()->rowCount() == 0) break; int i = app_->playlist_manager()->active()->current_row(); if (i == -1) i = app_->playlist_manager()->active()->last_played_row(); if (i == -1) i = 0; - PlayAt(i, Engine::First, autoscroll, true); + PlayAt(i, offset_nanosec, Engine::First, autoscroll, true); break; } } @@ -517,6 +516,7 @@ void Player::UnPause() { const quint64 time = QDateTime::currentDateTime().toSecsSinceEpoch() - pause_time_.toSecsSinceEpoch(); if (time >= 30) { // Stream URL might be expired. qLog(Debug) << "Re-requesting stream URL for" << song.url(); + play_offset_nanosec_ = engine_->position_nanosec(); HandleLoadResult(url_handlers_[song.url().scheme()]->StartLoading(song.url())); return; } @@ -579,7 +579,7 @@ void Player::PreviousItem(const Engine::TrackChangeFlags change) { QDateTime now = QDateTime::currentDateTime(); if (last_pressed_previous_.isValid() && last_pressed_previous_.secsTo(now) >= 2) { last_pressed_previous_ = now; - PlayAt(app_->playlist_manager()->active()->current_row(), change, Playlist::AutoScroll_Always, false, true); + PlayAt(app_->playlist_manager()->active()->current_row(), 0, change, Playlist::AutoScroll_Always, false, true); return; } last_pressed_previous_ = now; @@ -589,11 +589,11 @@ void Player::PreviousItem(const Engine::TrackChangeFlags change) { app_->playlist_manager()->active()->set_current_row(i, Playlist::AutoScroll_Always, false); if (i == -1) { Stop(); - PlayAt(i, change, Playlist::AutoScroll_Always, true); + PlayAt(i, 0, change, Playlist::AutoScroll_Always, true); return; } - PlayAt(i, change, Playlist::AutoScroll_Always, false); + PlayAt(i, 0, change, Playlist::AutoScroll_Always, false); } @@ -648,7 +648,10 @@ void Player::SetVolume(const int value) { int Player::GetVolume() const { return engine_->volume(); } -void Player::PlayAt(const int index, Engine::TrackChangeFlags change, const Playlist::AutoScroll autoscroll, const bool reshuffle, const bool force_inform) { +void Player::PlayAt(const int index, const qint64 offset_nanosec, Engine::TrackChangeFlags change, const Playlist::AutoScroll autoscroll, const bool reshuffle, const bool force_inform) { + + pause_time_ = QDateTime(); + play_offset_nanosec_ = offset_nanosec; if (current_item_ && change == Engine::Manual && engine_->position_nanosec() != engine_->length_nanosec()) { emit TrackSkipped(current_item_); @@ -663,8 +666,6 @@ void Player::PlayAt(const int index, Engine::TrackChangeFlags change, const Play app_->playlist_manager()->active()->set_current_row(index, autoscroll, false, force_inform); if (app_->playlist_manager()->active()->current_row() == -1) { // Maybe index didn't exist in the playlist. - pause_time_ = QDateTime(); - play_offset_nanosec_ = 0; return; } @@ -682,10 +683,8 @@ void Player::PlayAt(const int index, Engine::TrackChangeFlags change, const Play HandleLoadResult(url_handlers_[url.scheme()]->StartLoading(url)); } else { - qLog(Debug) << "Playing song" << current_item_->Metadata().title() << url << "position" << play_offset_nanosec_; - engine_->Play(url, current_item_->Url(), change, current_item_->Metadata().has_cue(), current_item_->effective_beginning_nanosec(), current_item_->effective_end_nanosec(), play_offset_nanosec_); - pause_time_ = QDateTime(); - play_offset_nanosec_ = 0; + qLog(Debug) << "Playing song" << current_item_->Metadata().title() << url << "position" << offset_nanosec; + engine_->Play(url, current_item_->Url(), change, current_item_->Metadata().has_cue(), current_item_->effective_beginning_nanosec(), current_item_->effective_end_nanosec(), offset_nanosec); } } diff --git a/src/core/player.h b/src/core/player.h index 5c6078b57..ff4dc0e8d 100644 --- a/src/core/player.h +++ b/src/core/player.h @@ -73,7 +73,7 @@ class PlayerInterface : public QObject { virtual void ReloadSettings() = 0; // Manual track change to the specified track - virtual void PlayAt(const int index, Engine::TrackChangeFlags change, const Playlist::AutoScroll autoscroll, const bool reshuffle, const bool force_inform = false) = 0; + virtual void PlayAt(const int index, const qint64 offset_nanosec, Engine::TrackChangeFlags change, const Playlist::AutoScroll autoscroll, const bool reshuffle, const bool force_inform = false) = 0; // If there's currently a song playing, pause it, otherwise play the track that was playing last, or the first one on the playlist virtual void PlayPause(const quint64 offset_nanosec = 0, const Playlist::AutoScroll autoscroll = Playlist::AutoScroll_Always) = 0; @@ -159,7 +159,7 @@ class Player : public PlayerInterface { public slots: void ReloadSettings() override; - void PlayAt(const int index, Engine::TrackChangeFlags change, const Playlist::AutoScroll autoscroll, const bool reshuffle, const bool force_inform = false) override; + void PlayAt(const int index, const qint64 offset_nanosec, Engine::TrackChangeFlags change, const Playlist::AutoScroll autoscroll, const bool reshuffle, const bool force_inform = false) override; void PlayPause(const quint64 offset_nanosec = 0, const Playlist::AutoScroll autoscroll = Playlist::AutoScroll_Always) override; void PlayPauseHelper() override { PlayPause(play_offset_nanosec_); } void RestartOrPrevious() override;