这是indexloc提供的服务,不要输入任何密码
Skip to content

Internal calls to the player crash apps that use a non-main applicationLooper #2884

@naveensingh

Description

@naveensingh

Version

Media3 1.8.0

More version details

No response

Devices that reproduce the issue

I can reproduce this on any device.

Devices that do not reproduce the issue

None.

Reproducible in the demo app?

Not tested

Reproduction steps

  1. Configure the player to use a non-main thread (i.e., call setLooper(playerThread.looper))
  2. Ensure onTaskRemoved() implementation is not overridden (or super is called if it is)
  3. Play media
  4. Remove the playing app from recents

Expected result

Internal calls to the player should use the correct thread, and playback should continue after the task is removed.

Actual result

Internal calls to the player use the main/wrong thread, app crashes, and playback stops.

Stack trace:

Exception java.lang.RuntimeException:
  at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:5503)
  at android.app.ActivityThread.-$$Nest$mhandleServiceArgs (Unknown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2716)
  at android.os.Handler.dispatchMessage (Handler.java:114)
  at android.os.Looper.loopOnce (Looper.java:206)
  at android.os.Looper.loop (Looper.java:296)
  at android.app.ActivityThread.main (ActivityThread.java:9205)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:591)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1027)
Caused by java.lang.IllegalStateException: Player is accessed on the wrong thread.
Current thread: 'main'
Expected thread: 'PlayerThread'
See https://developer.android.com/guide/topics/media/issues/player-accessed-on-wrong-thread
  at androidx.media3.exoplayer.ExoPlayerImpl.verifyApplicationThread (ExoPlayerImpl.java:2920)
  at androidx.media3.exoplayer.ExoPlayerImpl.setPlayWhenReady (ExoPlayerImpl.java)
  at androidx.media3.common.ForwardingPlayer.setPlayWhenReady (ForwardingPlayer.java:262)
  at androidx.media3.session.MediaSessionService.pauseAllPlayersAndStopSelf (MediaSessionService.java:579)
  at androidx.media3.session.MediaSessionService.onTaskRemoved (MediaSessionService.java:617)
  at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:5485)

Even if onTaskRemoved() is overridden to call the super implementation on the proper player thread (hacky), the app still crashes, but only in the paused state:

Exception java.lang.IllegalStateException: MediaController method is called from a wrong thread. See javadoc of MediaController for details.
  at androidx.media3.common.util.Assertions.checkState (Assertions.java:100)
  at androidx.media3.session.MediaController.verifyApplicationThread (MediaController.java:2113)
  at androidx.media3.session.MediaController.getPlayWhenReady (MediaController.java)
  at androidx.media3.session.MediaNotificationManager.isAnySessionUserEngaged (MediaNotificationManager.java:246)
  at androidx.media3.session.MediaNotificationManager.shouldRunInForeground (MediaNotificationManager.java:229)
  at androidx.media3.session.MediaSessionService.onUpdateNotificationInternal (MediaSessionService.java:711)
  at androidx.media3.session.MediaNotificationManager.disableUserEngagedTimeout (MediaNotificationManager.java:265)
  at androidx.media3.session.MediaSessionService.pauseAllPlayersAndStopSelf (MediaSessionService.java:576)
  at androidx.media3.session.MediaSessionService.onTaskRemoved (MediaSessionService.java:617)
  at org.fossify.musicplayer.playback.PlaybackService.onTaskRemoved$lambda$0 (PlaybackService.kt)  (here call was posted using player looper)
  at android.os.Handler.handleCallback (Handler.java:978)
  at android.os.Handler.dispatchMessage (Handler.java:104)
  at android.os.Looper.loopOnce (Looper.java:238)
  at android.os.Looper.loop (Looper.java:357)
  at android.os.HandlerThread.run (HandlerThread.java:67)

Media

Any audio file will work fine.

Bug Report

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions