Skip to content

iOSのバックグラウンドTTS停止を修正しプレイヤー再利用をAndroid限定にする - #6252

Merged
TinyKitten merged 2 commits into
devfrom
claude/tts-background-playback-ios-rkle70
Jun 17, 2026
Merged

TinyKitten merged 2 commits into
devfrom
claude/tts-background-playback-ios-rkle70

Conversation

@TinyKitten

@TinyKitten TinyKitten commented Jun 17, 2026 •

Copy link
Copy Markdown
Member

概要

iPhone (iOS 26) でバックグラウンド時に TTS が再生されなくなる不具合を修正します。原因は #6235 で導入したプレイヤーの永続再利用 (replace()) で、iOS のバックグラウンドでは再生を開始できないためです。プレイヤー再利用を Android 限定にし、iOS は従来どおり発話ごとに生成・破棄して背景再生を維持します。

expo-audio ネイティブ実装 (AudioPlayer.swift の replaceCurrentSource) は「差し替え時点で再生中だった場合のみ」ロード完了後 (onReady) に再生を再開します。JS (ttsAudioPlayer.ts) は replace() の直前に pause() するため wasPlaying = false となり自動再開されず、直後に呼ぶ play() はまだ readyToPlay でない差し替え後アイテムへの呼び出しになります。フォアグラウンドでは間に合うものの、バックグラウンドの iOS では再生が始まりません。新規 createAudioPlayer(従来の iOS 挙動)はこの問題が無いため、iOS では発話ごとの生成に戻します。AudioTrack 枯渇は Android 固有の問題のため、Android は #6235 の再利用パスを維持します。

変更の種類

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

変更内容

  • useTTS.ts に reusePlayers = Platform.OS === 'android' を導入し、プレイヤー再利用を Android 限定に変更。
  • iOS では cleanupAllPlayers / stopJa / stopEn でプレイヤーをネイティブごと破棄 (remove) し、発話ごとに createAudioPlayer で新規生成する従来挙動へ復帰。Android は永続インスタンスを一時停止して replace() で使い回す挙動を維持。
  • releaseAllPlayers(アンマウント時)で永続プレイヤーと再生中ハンドルのプレイヤー双方を確実に解放するよう調整。
  • テストを Android/iOS 双方の挙動向けに整理(再利用・ストール時の破棄/一時停止の差を検証)し、iOS で 2 回目の発話が replace ではなく新規生成されること、ストール時にプレイヤーが破棄されることを追加検証。

テスト

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

根本原因はネイティブ音声リソースの挙動のため、最終確認は実機 iOS (iOS 26) でのバックグラウンド・オートモード長時間再生での検証を推奨します。

関連Issue

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


Generated by Claude Code

Summary by CodeRabbit

リリース ノート

  • Bug Fixes

    • 音声再生の安定性を向上させました。AndroidとiOSでのプレイヤー制御を最適化し、停止検知や再生中の挙動をより安定させました。
  • Tests

    • プラットフォーム別(Android/iOS)の音声再生・停止時の呼び出し挙動や、2回目以降の発話でのプレイヤー動作を検証するテストを追加・更新しました。

@TinyKitten TinyKitten self-assigned this Jun 17, 2026
@github-actions github-actions Bot added the react label Jun 17, 2026
@coderabbitai

coderabbitai Bot commented Jun 17, 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: 3319be26-cffd-4fd0-af29-d149a590a38d

📥 Commits

Reviewing files that changed from the base of the PR and between ba78ec5 and ae3bf32.

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

📝 Walkthrough

Walkthrough

useTTS に reusePlayers フラグ(Android のみ true)を導入し、Android ではプレイヤーを pause() で保持して replace() で差し替え、iOS では従来通り safeRemovePlayer で破棄する分岐を実装した。テストには Platform.OS 切り替えヘルパーを追加し、ストール検知・2 回目以降の発話についてプラットフォーム別の期待値を検証するケースを整備した。

Changes

TTS プレイヤープラットフォーム別再利用

Layer / File(s) Summary
reusePlayers 導入・cleanupAllPlayers/stopEn/stopJa 分岐
src/hooks/useTTS.ts
Platform インポートを追加し reusePlayers フラグを導入。cleanupAllPlayers/releaseAllPlayers・stopEn・stopJa の停止処理を Android(pause() + リスナー解放)と iOS(safeRemovePlayer + null 化)に分岐した。
playAudio 呼び出しパスの reusePlayers 分岐と useCallback 依存追加
src/hooks/useTTS.ts
EN のみ再生・JA 再生・JA 完了後 EN 開始の各パスで playAudio に渡す player を reusePlayers で条件分岐し、enPlayerRef/jaPlayerRef の更新タイミングを調整。speakFromPath の useCallback 依存関係に reusePlayers を追加した。
テストヘルパー: Platform.OS 切り替えと復元
src/hooks/useTTS.test.ts
Platform を追加インポートし、originalPlatformOS と setPlatformOS ヘルパーを追加。afterEach で Platform.OS を元の値へ復元する処理を組み込んだ。
ストール検知テスト: Android(pause) / iOS(remove) 期待値分岐
src/hooks/useTTS.test.ts
ストール検知時の挙動を Android 用(pause 呼出・remove 非呼出)と iOS 用(remove 呼出)に分離してテストを追加・更新した。
2 回目以降の発話テスト: iOS では replace 非呼出・毎回新規生成
src/hooks/useTTS.test.ts
iOS では 2 回目以降の発話でもプレイヤーを使い回さず毎回新規生成し replace が呼ばれないことを検証するテストを追加した。

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • TrainLCD/MobileApp#6163: useTTS.test.ts のストール検知テストにおける pause/remove の期待値と同一シナリオを扱っており、今回変更したストール検知テストの直接の前身となっている。
  • TrainLCD/MobileApp#6235: Android TTS プレイヤーを remove() から pause() に切り替え・replace() で音源差し替えを行う同一戦略を useTTS.ts/useTTS.test.ts に実装しており、今回の変更と実装方針が一致している。

Poem

🐰 Android は pause でひと休み、また鳴らすよ
iOS は remove でスッキリ片付け
reusePlayers がふたつの道を分けて
プラットフォームごとに最適な旅を
ウサギも感心、コードの住み分け ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed PRタイトルは、iOS のバックグラウンド TTS 停止の修正とプレイヤー再利用を Android に限定するという主な変更内容を明確に反映しており、変更セットの中心的な目的と一致している。
Description check ✅ Passed PR説明は、テンプレートの全必須セクション(概要、変更の種類、変更内容、テスト、関連Issue)を含み、詳細かつ技術的に正確に記述されており、テストもすべて実施済み(チェック済み)である。
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-background-playback-ios-rkle70

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

@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.test.ts`:
- Around line 527-529: The test for iOS "generate fresh each time" functionality
uses mockReturnValue which returns the same player instance for all calls to
createAudioPlayer, making it impossible to verify that new instances are
actually created on each call. Replace mockReturnValue with
mockImplementationOnce to return different mock player instances for the 1st and
2nd calls to createAudioPlayer. Additionally, verify that the first player
instance's remove method is called to ensure proper cleanup and confirm that
instances are not being reused across multiple invocations.
🪄 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: 80b5e9d7-c35b-4148-962a-92c4c7ea6c3d

📥 Commits

Reviewing files that changed from the base of the PR and between e3d13f8 and ba78ec5.

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

Comment thread src/hooks/useTTS.test.ts Resolved
@TinyKitten
TinyKitten merged commit 7b801f7 into dev Jun 17, 2026
5 checks passed
@TinyKitten
TinyKitten deleted the claude/tts-background-playback-ios-rkle70 branch June 17, 2026 11:39
@TinyKitten TinyKitten mentioned this pull request Jun 17, 2026
4 of 9 tasks
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