Skip to content

fix(tts): fix Android background playback stopping#243

Open
codedogQBY wants to merge 1 commit into
mainfrom
fix/tts-android-background
Open

fix(tts): fix Android background playback stopping#243
codedogQBY wants to merge 1 commit into
mainfrom
fix/tts-android-background

Conversation

@codedogQBY
Copy link
Copy Markdown
Owner

Summary

Fixes TTS audio stopping when Android app goes to background or screen locks. Three root causes:

1. Missing Android permissions

Added FOREGROUND_SERVICE and WAKE_LOCK to prevent system from killing the audio process.

2. ExpoSpeechTTSPlayer stops unconditionally on background

The AppState handler called this.stop() on all platforms when going to background. This was only needed for iOS (Apple's TextToSpeech crashes if active when backgrounded). Android handles background speech fine.

3. Silent keep-alive track only on iOS

When the TTS audio queue starves (network delay between chunks), a 1-second silent track keeps the audio session alive. This was iOS-only but Android has the same need — without it, the system reclaims the audio focus.

Changes

File Change
app.config.js Add FOREGROUND_SERVICE + WAKE_LOCK permissions
expo-speech-player.ts Only stop on Platform.OS === "ios"
track-player-edge-player.ts Remove iOS-only gate on silence keep-alive
track-player-dashscope-player.ts Same

Test plan

  • Android: Start TTS → lock screen → audio continues playing
  • Android: Start TTS → switch to another app → audio continues
  • Android: Notification bar shows playback controls that work
  • iOS: TTS still stops when backgrounded (intentional, prevents crash)
  • No TypeScript errors (verified)

Closes #60
Closes #228
Closes #242

🤖 Generated with Claude Code

Three root causes identified and fixed:

1. Missing Android permissions (app.config.js):
   - Add FOREGROUND_SERVICE (parent permission for all foreground services)
   - Add WAKE_LOCK (prevents CPU sleep during background audio)

2. ExpoSpeechTTSPlayer unconditionally stops on background:
   - The AppState handler called this.stop() on ANY platform going to bg
   - Apple's TextToSpeech.framework crashes if active when backgrounded,
     but Android handles it fine
   - Fix: only stop on iOS, let Android continue playing

3. Silent keep-alive track only enabled on iOS:
   - When TTS audio queue starves (network delay between chunks), a 1s
     silent track keeps the audio session alive so the OS doesn't reclaim it
   - This was iOS-only; Android has the same issue
   - Fix: enable on both platforms

Closes #60
Closes #228
Closes #242

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.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.

[Bug] TTS朗读。后台自动停止朗读 [Bug] 无法后台语音朗读。 Android支持背景播放及通知欄播放控制

1 participant