Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MediaSessionService onCreate ANR error #2030

Open
xuwang-yuewen opened this issue Jan 9, 2025 · 11 comments
Open

MediaSessionService onCreate ANR error #2030

xuwang-yuewen opened this issue Jan 9, 2025 · 11 comments
Assignees
Labels

Comments

@xuwang-yuewen
Copy link

Media3 Version

1.3.1

Devices that reproduce the issue

Google Play and Firebase report these devices:

  • Oppo
  • Oppo OP4F2F
  • Infinix Infinix-X6516
  • Transsion
  • Transsion-Unisoc
  • TECNO TECNO-BF6
  • OPPO OP4C7D

including Android 11, 12, 13, 14

Reproduction steps

(online production environment)

Expected result

not ANR crash

Actual result

many ANR errors

Description

When using media3 exoplayer to implement an audio player, a large number of ANRs are encountered. The basic implementation is as follows.

class AudioService : MediaSessionService() {

	override fun onCreate() {
		super.onCreate()
		exoPlayer = ExoPlayer.Builder(this, RenderersFactory(this)).build()
		exoPlayer?.setAudioAttributes(uAmpAudioAttributes, true)
		exoPlayer?.setHandleAudioBecomingNoisy(true)
		exoPlayer?.addListener(playerListener)

		val sessionBuilder = MediaSession.Builder(this, exoPlayer!!)
		sessionBuilder.setCallback(ExtendMediaSessionCallback())
		sessionBuilder.setId(packageName)
		packageManager?.getLaunchIntentForPackage(packageName)?.let { sessionIntent ->
        sessionBuilder.setSessionActivity(
	          PendingIntent.getActivity(
	            this@AudioService,
	            0,
	            sessionIntent,
	            if (Build.VERSION.SDK_INT >= 23) FLAG_IMMUTABLE else FLAG_UPDATE_CURRENT
	          )
	        )
	      }
      	mediaSession = sessionBuilder.build()
	}
}
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.*
import kotlinx.coroutines.guava.await

class AudioConnection(context: Context, serviceComponent: ComponentName) { // serviceComponent is AudioService

	private val coroutineContext: CoroutineContext = Dispatchers.Main
	private val scope = CoroutineScope(coroutineContext + SupervisorJob())

	val player: Player? get() = controller
	var controller: MediaController? = null

	init {
		scope.launch {
	      val newController = MediaController.Builder(context, SessionToken(context, serviceComponent))
	        .setListener(ControllerListener())
	        .buildAsync()
	        .await()
	      newController.addListener(playerListener)
	      controller = newController
	    }
	}
}

AudioConnection is a singleton. Initialize AudioConnection and use it to play audio after initialization is complete.
AudioConnection.getInstance(context, ComponentName(context, AudioService::class.java))

Two examples of stack trace are as follows. There are some other stack traces with slightly different contents.
1
2

@xuwang-yuewen
Copy link
Author

截屏2025-01-09 11 39 34

@xuwang-yuewen
Copy link
Author

one more stack trace:
截屏2025-01-09 16 31 02

@icbaker icbaker self-assigned this Jan 10, 2025
@icbaker
Copy link
Collaborator

icbaker commented Jan 10, 2025

From the stack traces it looks like calls into the framework are taking too long and causing the ANR?

  • In the first one: android.media.AudioAttributes.Builder.setLegacyStreamType
  • In the second one: android.media.AudioManager.getAudioDevicesForAttributes

@Tolriq
Copy link
Contributor

Tolriq commented Jan 12, 2025

There's already a couple of other reported issues, audioManager use IPC binding to access and can ANR, there's a few other places where it's problematic.

@xuwang-yuewen
Copy link
Author

@icbaker Yes. The service is started externally in a normal way, with nothing special and no other tasks. Online users report a large number of ANRs.

@xuwang-yuewen
Copy link
Author

@Tolriq Thanks. Is there any way to solve this or reduce the number of ANR?

@Tolriq
Copy link
Contributor

Tolriq commented Jan 15, 2025

Do not instantiate ExoPlayer on mainthread until all those are fixed. I would recommend using a dedicated thread for the whole player access and callbacks, but just instantiate on background and pass the mainthread looper will remove a couple of ANRs due to IPC.

I use the dedicated thread approach + wrote a custom component to workaround #1550 that happens even in that case. No more ANRs due to Media3.

@tonihei
Copy link
Collaborator

tonihei commented Jan 15, 2025

We are currently working on changes that move all potentially blocking system calls away from the main thread, so that hopefully none of these workarounds should be needed in the future.

@xuwang-yuewen
Copy link
Author

@Tolriq Thanks for reply. Some of it can be done in a non-main thread, which should alleviate ANR.

@xuwang-yuewen
Copy link
Author

@tonihei Thanks. Looking forward to this solution.

@tonihei tonihei assigned tonihei and unassigned icbaker Jan 15, 2025
@xuwang-yuewen
Copy link
Author

@tonihei Hi, by the way, which version is expected to add these changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants