Skip to content

Correção de ANR ao tocar som na thread principal#8

Open
JCPistori wants to merge 2 commits into
masterfrom
fix/anr-ao-tocar-som
Open

Correção de ANR ao tocar som na thread principal#8
JCPistori wants to merge 2 commits into
masterfrom
fix/anr-ao-tocar-som

Conversation

@JCPistori

Copy link
Copy Markdown
Collaborator

@JCPistori JCPistori self-assigned this Jul 20, 2026
@gaudinho

gaudinho Bot commented Jul 20, 2026

Copy link
Copy Markdown

✅ Review postada — #8 (review)

job_id: flutter_foreground_task-8-1784584160401-40fd

@gaudinho gaudinho Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix correto e mínimo pra ANR: move RingtoneManager.getRingtone().play() da thread principal pra um SingleThreadExecutor dedicado, e chama shutdown() no onDestroy. Adição do safe-call r?.play() também é boa defensiva contra retorno null do getRingtone.


override fun onDestroy() {
super.onDestroy()
soundExecutor.shutdown()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sugestão (não bloqueia)soundExecutor.shutdown() só sinaliza que não aceita mais tarefas, mas se houver uma reprodução em andamento no momento do onDestroy, ela continua até terminar. Se o objetivo é encerrar sons pendentes quando o serviço morre, considere soundExecutor.shutdownNow() (interrompe a task em andamento). Como Ringtone.play() é curto e não bloqueante prolongado, o shutdown() atual é aceitável — só vale citar.

soundExecutor.execute {
try {
val r = RingtoneManager.getRingtone(applicationContext, uri)
r?.play()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit — o Ringtone obtido via RingtoneManager.getRingtone() não é liberado explicitamente. Em execuções repetidas (várias chamadas API_UPDATE seguidas), pode acumular instâncias vivas até GC. Não é blocker (o código anterior já tinha o mesmo comportamento), mas pra futura evolução vale considerar MediaPlayer com release() no setOnCompletionListener, ou manter referência única do Ringtone e chamar stop() antes de reproduzir de novo.

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.

3 participants