androidx / androidx/media

MediaSessionService onCreate ANR error

Open
#2,030 12 comments 0 reactions 1 assignee View on GitHub

Nobody has claimed this yet.

question
Dominant language
Java
Stars
3k
Forks
955
Avg merge
12d 14h
Merged PRs (30d)
2

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.