8000 Fix next sura starting at ayah 2 in gapless continuous playback by m86hussein · Pull Request #4057 · quran/quran_android · GitHub
[go: up one dir, main page]

Skip to content

Fix next sura starting at ayah 2 in gapless continuous playback - #4057

Open
m86hussein wants to merge 1 commit into
quran:mainfrom
m86hussein:fix/next-surah-first-ayah
Open

m86hussein wants to merge 1 commit into
quran:mainfrom
m86hussein:fix/next-surah-first-ayah

Conversation

@m86hussein
@m86hussein m86hussein commented Nov 22, 2026 •
Copy link
Copy Markdown
Contributor

Summary

With a gapless reciter and continuous playback, the next sura can start from ayah 2 instead of ayah 1 (#4055).

The bug

When updateAudioPlayPosition passes the end-of-sura marker (ayahTimings[999]), it moves the queue to sura + 1 : 1 and calls playAudio(true). For gapless reciters playAudio() sets state = Stopped and then loads the next sura's timings in a coroutine (updateGaplessData → TimingRepository.timings, which suspends on Dispatchers.IO). Only after that does it reset the player and start the new file.

The old player was never stopped, so it kept playing the tail of the previous file during that load. The player is built on the service's handler thread, so its callbacks run on the same thread as the coroutine and can run while the coroutine is suspended. If the old file reaches its end first, which is likely for recordings that end right at the 999 marker (see 566fb5a), this happens:

  1. STATE_ENDED → onPlayerCompleted() → audioQueue.playNextAyah(false) moves the queue from N+1:1 to N+1:2.
  2. The sura didn't change and the reciter is gapless, so it tries to seek. getSeekPosition() returns -1 because gaplessSura is still N, so nothing happens.
  3. The coroutine resumes and starts sura N+1 at ayahTimings[2], skipping ayah 1.

The fix

playAudio() now calls player?.stop() before the async timing load. A stopped player can't send a stale STATE_ENDED, and ExoPlayer drops any pending state updates once stop() is called. The STATE_IDLE callback this triggers does nothing, because state is already Stopped. playAudio(flag, timings) already stops and resets the player before starting the new file, so this only moves the stop earlier.

The change is a single line in AudioService.playAudio().

Testing

  • ./gradlew :app:testMadaniDebugUnitTest --tests "*Audio*" passes (25 tests: AudioQueueTest, AudioPresenterTest, AudioUtilsTest, AudioRevisionSettingsTest).
  • I didn't add a unit test: AudioService has no unit tests, and reproducing this would need a mocked ExoPlayer plus controlled coroutine timing. I can add one if you have a preferred approach.

Fixes #4055

AI disclosure: this PR was drafted with GitHub Copilot and opened by @m86hussein, who is responsible for it.

When gapless playback passes the end-of-sura marker, the queue moves to ayah 1 of the next sura and playAudio() loads that sura's timings asynchronously. The old file kept playing meanwhile; if it reached its end first, onPlayerCompleted advanced the queue again, so the next sura started from ayah 2.

Stop the current player before the async load so no stale completion can advance the queue.

Fixes quran#4055

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing the first verse of the next surah on continuous reciting

1 participant

0