Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't play sound on button click using playSoundEffect(SoundEffectConstants.CLICK)

Tags:

android

I have the following 2 lines of code:

myButton.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
myButton.playSoundEffect(SoundEffectConstants.CLICK);

When I press the button, haptic feedback works fine but no audio is heard.

Anybody had this problem when they started with Android?

like image 901
Wael Avatar asked Dec 06 '10 02:12

Wael


3 Answers

I had this problem and it turned out to be something quite stupid on my part. I had disabled "Audible Selection" for the entire phone. On the home page, it's on Settings-->Sound-->Audible selection. There is probably a programmatic way to do the same thing but I have not found it yet.

like image 163
user199395 Avatar answered Oct 18 '22 07:10

user199395


I also had the same problem. On my Samsung Galaxy S4, the path is Settings --> My Device --> Sound --> Touch Sounds (under the System heading). Once I enabled this, then the default system sounds started working on my app.

like image 37
user2768751 Avatar answered Oct 18 '22 05:10

user2768751


Well, I came across the same situation, The trick was, to use Audiomanager.

define an AudioManager in the Activity class.

AudioManager audioManager;

Initialise in onCreate.

audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

In the click method, use the audioManager to play the sound.

public void play(View view) {
    audioManager.playSoundEffect(SoundEffectConstants.CLICK,1.0f);
}

Note that the volume is given as 1.0f if you don't specify the volume, you won't hear any sound.

like image 44
Prashant naik Avatar answered Oct 18 '22 06:10

Prashant naik