Reputation: 4559
I hava a set of buttons. When I click on a button it should produce sound.
Example:
Button b=new Button(this);
b.setText("Press");
b.setOnClickListener(new OnClickListener)[
public void click(View v)
{
b.setSoundEffectsEnabled(true);
});}
This doesn't work though, can anyone help me please.
Upvotes: 3
Views: 2058
Reputation: 1
If you want to play the default click sound when you click on the button, then setting b.setSoundEffectsEnabled(true)
should work (although it doesn't need to be on the listener), but it is dependent on the device option to play audible selection. Try checking the device's sound settings if it is on.
Upvotes: 0
Reputation: 572
You can also use the build in sound notifications
ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, ToneGenerator.MAX_VOLUME);
tg.startTone(ToneGenerator.TONE_PROP_BEEP);
Upvotes: 2
Reputation: 1615
What do you mean on default sound? If you want to play your own sound, you must create a MediaPlayer like this.
MediaPlayer mediaPlayer = MediaPlayer.create(this, [here is your sound in the raw file]);
and in the click method you need to implement this:
mediaPlayer.start();
or you can use soundpool too.
Hope it helps.
Upvotes: 2