Skip to content

Fix @UiThread crash when audio devices change on Android TV (fixes #188) - #189

Open
under3415 wants to merge 1 commit into
ryanheise:masterfrom
under3415:fix/main-thread-audio-device-callback
Open

Fix @UiThread crash when audio devices change on Android TV (fixes #188)#189
under3415 wants to merge 1 commit into
ryanheise:masterfrom
under3415:fix/main-thread-audio-device-callback

Conversation

@under3415

Copy link
Copy Markdown

PR Description — Fix @UiThread crash on Android TV HDMI devices

Title:

Fix @UiThread crash when audio devices change on Android TV (fixes #188)

Body (paste into the PR description):

Fixes #188

## Problem

On some Android TV boxes, `AudioManager$AudioMonitorHdmiThread` invokes
`AudioDeviceCallback.onAudioDevicesAdded/onAudioDevicesRemoved` on a background
thread, ignoring the main-looper `Handler` passed to
`AudioManager.registerAudioDeviceCallback()`. The callback then calls
`MethodChannel.invokeMethod()` (annotated `@UiThread`) from that thread, causing
a fatal crash:

    java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread.

Affected devices include Skyworth TV boxes (Android 10), Inspur set-top boxes
(Android 11), and low-end Fire TV devices. Phones are unaffected because the
HDMI audio monitor thread does not exist there.

First report: https://github.com/ryanheise/just_audio/issues/1484

## Fix

Guard `AudioManagerSingleton.invokeMethod()` so all MethodChannel invocations
are dispatched on the main looper. This is a single-point fix covering all
callback-originated events (audio devices added/removed, focus change, becoming
noisy, SCO audio state), rather than patching each callback.

If already on the main thread, dispatch directly to avoid an unnecessary hop.

## Verification

- `flutter analyze` passes
- `flutter test` passes (example app)
- Runtime-tested logic: on-main-thread path identical to previous behavior;
  off-main-thread path is now marshalled to the main thread

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.

1 participant