From 34858d0a6cf750ec53bc160e75fcc29dbeed5e83 Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Mon, 28 Mar 2022 17:00:23 +0300 Subject: Update track progress in MusicService. Fix #127 --- src/components/ble/MusicService.cpp | 13 ++++++++++++- src/components/ble/MusicService.h | 3 ++- 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'src/components/ble') diff --git a/src/components/ble/MusicService.cpp b/src/components/ble/MusicService.cpp index c99aa1e3..83b26a78 100644 --- a/src/components/ble/MusicService.cpp +++ b/src/components/ble/MusicService.cpp @@ -152,12 +152,20 @@ int Pinetime::Controllers::MusicService::OnCommand(uint16_t conn_handle, uint16_ albumName = s; } else if (ble_uuid_cmp(ctxt->chr->uuid, &msStatusCharUuid.u) == 0) { playing = s[0]; + // These variables need to be updated, because the progress may not be updated immediately, + // leading to getProgress() returning an incorrect position. + if (playing) { + trackProgressUpdateTime = xTaskGetTickCount(); + } else { + trackProgress += static_cast((static_cast(xTaskGetTickCount() - trackProgressUpdateTime) / 1024.0f) * getPlaybackSpeed()); + } } else if (ble_uuid_cmp(ctxt->chr->uuid, &msRepeatCharUuid.u) == 0) { repeat = s[0]; } else if (ble_uuid_cmp(ctxt->chr->uuid, &msShuffleCharUuid.u) == 0) { shuffle = s[0]; } else if (ble_uuid_cmp(ctxt->chr->uuid, &msPositionCharUuid.u) == 0) { trackProgress = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; + trackProgressUpdateTime = xTaskGetTickCount(); } else if (ble_uuid_cmp(ctxt->chr->uuid, &msTotalLengthCharUuid.u) == 0) { trackLength = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]; } else if (ble_uuid_cmp(ctxt->chr->uuid, &msTrackNumberCharUuid.u) == 0) { @@ -191,7 +199,10 @@ float Pinetime::Controllers::MusicService::getPlaybackSpeed() const { return playbackSpeed; } -int Pinetime::Controllers::MusicService::getProgress() const { +int Pinetime::Controllers::MusicService::getProgress() { + if (isPlaying()) { + return trackProgress + static_cast((static_cast(xTaskGetTickCount() - trackProgressUpdateTime) / 1024.0f) * getPlaybackSpeed()); + } return trackProgress; } diff --git a/src/components/ble/MusicService.h b/src/components/ble/MusicService.h index 1ad9a420..6c1a6edb 100644 --- a/src/components/ble/MusicService.h +++ b/src/components/ble/MusicService.h @@ -47,7 +47,7 @@ namespace Pinetime { std::string getAlbum() const; - int getProgress() const; + int getProgress(); int getTrackLength() const; @@ -81,6 +81,7 @@ namespace Pinetime { int trackLength {0}; int trackNumber {}; int tracksTotal {}; + TickType_t trackProgressUpdateTime {0}; float playbackSpeed {1.0f}; -- cgit v1.2.3