Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off physical button's sound click (sound effects) in my Android app?

Tags:

android

button

I'm developing an Android app where the user is allowed to rest his hand on the device, and I want to disable physical buttons so the user don't get annoyed by unwanted clicks.

I've disabled the back, menu and search buttons with the following:

@Override
public void onBackPressed() {}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {return false;}

@Override
public boolean onSearchRequested() {return false;}

And I've learned that disabling the home key is unsupported and/or not adviced (although I'd like it to).

Ok, now I couldn't figure out how to disable the clicking sound for the keys I've disabled, they do nothing but keep ticking, is there a way to turn it off?

I'd like to mute it for my app only. If not possible, I'd like to know about alternatives that change system settings programmatically, to restore it latter on closing or on focus changing.

like image 486
pepper_chico Avatar asked Nov 30 '22 06:11

pepper_chico


1 Answers

You can mute the sound when your app starts and unmute when it finishes

@override
public void onResume(){
    super.onResume();
    AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    mgr.setStreamMute(AudioManager.STREAM_SYSTEM, true);
}


@override
public void onPause(){
    super.onPause();
    AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    mgr.setStreamMute(AudioManager.STREAM_SYSTEM, false);
}
like image 172
Kyle Avatar answered Dec 05 '22 00:12

Kyle