Skip to content

TTS再生が途中で打ち切られ英語に飛ぶ不具合を修正(iOS早期didJustFinishガード/再生中の時間制限撤廃) - #6282

Merged
TinyKitten merged 3 commits into
devfrom
claude/tts-playback-time-limit-6vwf3c
Jun 19, 2026
Merged

TinyKitten merged 3 commits into
devfrom
claude/tts-playback-time-limit-6vwf3c

Conversation

@TinyKitten

@TinyKitten TinyKitten commented Jun 19, 2026 •

Copy link
Copy Markdown
Member

概要

長い日本語アナウンスがiOSで途中で打ち切られ、英語アナウンスに飛んでしまう不具合を修正する。

調査の結果、JA→ENの切り替えトリガーはJAプレイヤーの didJustFinish(再生完了報告)のみであることが分かった(useTTS.ts)。iOSは長尺音声で、音声セッションの一時中断やデコード境界などにより本来の終端より手前で didJustFinish を誤報することがあり、これをそのまま「完了」と扱うため、日本語が途中で打ち切られて英語へ進んでしまっていた。ttsAudioPlayer.ts に終端到達判定ガードを追加し、終端付近に達していない完了報告は早期完了とみなして再生を継続するようにした。

あわせて、当初の PLAYBACK_TIMEOUT_MS(5分)は再生中でも強制的に打ち切る設計だったため、再生開始時に解除して健全な再生を時間で打ち切らないようにした(フェッチ・トークン取得段階のハング保護としては維持)。

変更の種類

  • バグ修正
  • 新機能
  • リファクタリング
  • ドキュメント
  • CI/CD
  • その他

変更内容

  • src/utils/ttsAudioPlayer.ts:
    • didJustFinish 受信時に再生位置(status.currentTime)と長さ(status.duration)を確認し、終端付近(FINISH_END_EPSILON_SEC 以内)に達していない完了報告を「早期完了」とみなすガードを追加。
    • 早期完了時、位置が取れる(> 0)場合はその位置へ seekTo してから play() で再開し、途中打ち切り(=次言語への早期遷移)を防止。位置が0(リセット報告)や終端付近の場合は従来どおり完了として進めるため、最悪でも現状維持で劣化しない。
    • 無限ループ防止に再開回数の上限 MAX_RESUME_ATTEMPTS を設定。実機での原因特定用に早期完了時の currentTime/duration/playbackState を console.warn に記録。
  • src/hooks/useTTS.ts:
    • PLAYBACK_TIMEOUT_MS(5分)を「再生開始前(フェッチ・トークン取得)のハング保護」専用に役割変更し、実際の再生開始時に clearPlaybackWatchdog で解除。再生中は時間によらず打ち切らない(進行停止検知は ttsAudioPlayer のストール監視に委ねる)。
    • コメント内の関数名参照を修正(armPlaybackWatchdog → clearPlaybackWatchdog、CodeRabbit指摘)。
  • テスト追加・更新:
    • src/utils/ttsAudioPlayer.test.ts: 終端付近の完了は正常完了、終端手前の完了は seekTo で再開、位置0の完了は再開せず完了、上限超過で完了として打ち切る、を検証。
    • src/hooks/useTTS.test.ts: 再生開始後は時間が経過しても打ち切られないことを検証。

テスト

  • npm run lint が通ること
  • npm test が通ること
  • npm run typecheck が通ること

実行結果:

  • npx jest src/utils/ttsAudioPlayer.test.ts src/hooks/useTTS.test.ts → 37 passed
  • npx tsc --noEmit → エラーなし
  • npx biome check(対象3ファイル)→ 問題なし

注: iOSが早期完了時に currentTime を「途中位置」で報告するか「0」で報告するかは実機未検証。前者なら本ガードで打ち切りが解消し、後者でも追加した console.warn のログで実機の実測値を確認でき、必要なら精緻化する。いずれの場合も既存挙動より悪化しない設計。

関連Issue

スクリーンショット(任意)

Summary by CodeRabbit

リリースノート

  • バグ修正
    • TTSの再生完了判定を見直し、終了直前の誤判定や早期完了に起因する不自然な動作を抑制しました。
    • 再生開始前の安全タイムアウトを、再生開始時に確実に解除するよう改善し、長尺コンテンツでの不要な強制リセットを防ぎます。
  • テスト
    • 終端付近・早期完了・長時間経過などの境界ケースを追加/更新して確認しました。

@github-actions github-actions Bot added the react label Jun 19, 2026
@coderabbitai

coderabbitai Bot commented Jun 19, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9fc5684b-d80e-4e84-aca2-4068e7de8be2

📥 Commits

Reviewing files that changed from the base of the PR and between 0720b3c and c87caff.

📒 Files selected for processing (2)
  • src/utils/ttsAudioPlayer.test.ts
  • src/utils/ttsAudioPlayer.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/utils/ttsAudioPlayer.ts
  • src/utils/ttsAudioPlayer.test.ts

📝 Walkthrough

Walkthrough

ttsAudioPlayer で didJustFinish 受信時に終端到達判定を追加し、終端付近でない誤報は seekTo/play で再開を試行するロジックを実装。useTTS は音声再生開始直後に準備段階タイムアウト(clearPlaybackWatchdog)を解除し、以降のストール監視に委譲。テストは新規境界ケースと期待値更新で検証。

Changes

TTS再生完了判定と準備段階ウォッチドッグの改善

Layer / File(s) Summary
ttsAudioPlayer の早期didJustFinish再開ロジック実装
src/utils/ttsAudioPlayer.ts
FINISH_END_EPSILON_SEC と MAX_RESUME_ATTEMPTS を新規 export 定数として追加。didJustFinish 受信時に currentTime/duration から終端到達判定を行い、終端付近でなければ seekTo/play により再開を最大回数まで試行する handleDidJustFinish ヘルパを実装。
ttsAudioPlayer.test.ts のテスト基盤拡張と新規ケース追加
src/utils/ttsAudioPlayer.test.ts
EmittedStatus 型を導入し、mock で currentTime/duration など追加ステータス項目を取り扱えるよう拡張。終端付近・手前・currentTime=0・連続早期 didJustFinish の各ケースでの完了・再開・打ち切り挙動と FINISH_END_EPSILON_SEC の値検証テストを追加。
useTTS の準備段階ウォッチドッグ解除ロジック実装
src/hooks/useTTS.ts
準備段階タイムアウトの設計コメントを整理し、clearPlaybackWatchdog 関数を追加。armPlaybackWatchdog 呼び出しを削除し、playAudio 実行直後に clearPlaybackWatchdog を呼んで準備段階タイムアウトを解除。useCallback 依存配列を更新。
useTTS.test.ts のテスト期待値更新
src/hooks/useTTS.test.ts
テストケース名を「再生開始後は打ち切られない」に変更。300 秒経過後の console.warn 検証を「呼ばれない」期待へ反転し、長尺再生中にウォッチドッグが機能しないことを検証。

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Possibly related PRs

  • TrainLCD/MobileApp#6235: armPlaybackWatchdog の導入など useTTS の fetch/token 段階ハング対策を追加しており、本 PR の clearPlaybackWatchdog 追加と対をなす変更。

  • TrainLCD/MobileApp#6163: ttsAudioPlayer でストール検知タイムアウト setInterval ウォッチドッグを導入する変更であり、本 PR の準備段階タイムアウト解除ロジックと相補的。

🐇 誤報よさらば、あるべき終へ!
再開ロジック、何度も試みて、
ウォッチドッグ解除、時をみて。
TTS の音、ずっと流れよ,
長き道のり、ホップホップ🎵

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed PRタイトルはiOS早期didJustFinishガードと再生中の時間制限撤廃という2つの主要な変更を的確に要約しており、修正内容を明確に伝えている。
Description check ✅ Passed PR説明は概要、変更の種類、詳細な変更内容、テスト実行結果、スクリーンショットセクションを含む全ての必須項目が記入されており、テンプレート要件を満たしている。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/tts-playback-time-limit-6vwf3c

Comment @coderabbitai help to get the list of available commands and usage tips.

@TinyKitten TinyKitten self-assigned this Jun 19, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/hooks/useTTS.ts`:
- Around line 26-31: The comment in the useTTS hook contains an incorrect
function reference. In the comment block explaining the playback watchdog
timeout behavior, change the reference from "armPlaybackWatchdog" to
"clearPlaybackWatchdog" on the line that mentions when the timeout is released
once actual audio playback begins. This will align the comment with the actual
function name that clears the timeout.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 66feae5d-2350-449a-add3-83cc76bc9e15

📥 Commits

Reviewing files that changed from the base of the PR and between f9c325b and 3931c22.

📒 Files selected for processing (2)
  • src/hooks/useTTS.test.ts
  • src/hooks/useTTS.ts

Comment thread src/hooks/useTTS.ts Resolved
@TinyKitten TinyKitten changed the title TTSの再生時間制限を撤廃し再生開始時に準備段階の安全タイムアウトを解除する TTS再生が途中で打ち切られ英語に飛ぶ不具合を修正(iOS早期didJustFinishガード/再生中の時間制限撤廃) Jun 19, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/utils/ttsAudioPlayer.ts`:
- Around line 61-100: In the handleDidJustFinish function, the
player.seekTo(position) call returns a Promise but player.play() is called
immediately afterward without awaiting the seek operation to complete. This can
cause playback to start from the wrong position before the seek finishes. Modify
the code to await the Promise returned by seekTo before calling play(), ensuring
the seek operation completes before playback begins. The try-catch block should
be updated to handle the async flow correctly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 843de881-c30e-400c-acfe-62b178fc1b4f

📥 Commits

Reviewing files that changed from the base of the PR and between 3931c22 and 0720b3c.

📒 Files selected for processing (3)
  • src/hooks/useTTS.ts
  • src/utils/ttsAudioPlayer.test.ts
  • src/utils/ttsAudioPlayer.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/hooks/useTTS.ts

Comment thread src/utils/ttsAudioPlayer.ts Resolved
@TinyKitten
TinyKitten merged commit 0e387c7 into dev Jun 19, 2026
5 checks passed
@TinyKitten
TinyKitten deleted the claude/tts-playback-time-limit-6vwf3c branch June 19, 2026 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants