I would like to disable sound effects when browsing over RecycleView items and also clicking sounds in an Android TV app. But, I do not want to disable all other sounds (e.g., There is Exoplayer in the app that its output sounds should not be muted).
I noticed there are some other questions similar to this on Stackoverflow and the suggested solutions are:
Disable Sound effect in the Layout Files by setting
android:soundEffectsEnabled="false"(I put this in every Layout). However, this does not have any effect and there is still clicking and item browsing sound effects.Disable sound effects using
AudioManager. I tried the following:audioManager.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, AudioManager.ADJUST_MUTE, 0);andaudioManager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_MUTE, 0);These mute all app sounds including Media sounds.
I would be grateful if someone can help with this issue. Thanks
Finally I found a solution for this problem.
Issue 1: Disabling sound effect on pressing DPAD_CENTER key. I could resolve this issue by programmatically disabling sound effect in CardPresenter (for Leanback ListRowPresenter) and CardAdapter (for RecyclerView).
Issue 2: Disabling sound effect on pressing DPAD navigation keys (DPAD_RIGHT, DPAD_LEFT, ...). Digging into the
ViewRootImpl.javaclass, it turns out that navigation sound is always played without checking thesoundEffectflag. Here is parts of the code inViewRootImpl.javaSo a workaround that I came up with is to override the
requestFocusmethod in my views and always returnfalseto prevent playing sound effect.Code for Leanback ListRowPresenter:
CardPresenter.javaCustomImageCardView.javaCode for RecyclerView:
CardAdapter.javaCustomLinearLayout.java(Root View for Recycler View)