I want to add custom audible feedback to a button press (various click sounds encoded as *.ogg) I've done this by using the RingtoneManager to create Ringtones for each of the clicks and then .play() them in the onClick() method. This works but seems a little sluggish. This leaves me wondering if there is a better way to attach a custom sound effect to a button press. I've scanned the Button reference page and all I found was playSoundEffect() which seems to handle only system defined sounds.
thanks,
hank
Use a SoundPool- they work it preloads the data into memory (so no file reads which cause sluggishness). Ringtones are definitely not what you want to be using here- those are typically much longer and not time critical (delaying a ringtone by a second or so isn't a problem, whereas delaying a button sound effect by that long is).
Related
When the user clicks on a button (assuming the device is not in silent/vibration mode) a confirmation sound is played, along with vibration.
From what I can tell we can have this happen on a custom View by setting the onClickListener and invoking performClick(); I have noticed that no feedback is given if the listener is not set.
Is there a way to just get the audio/vibration feedback without invoking the onClick() method?
I will need to implement onClick() for other reasons, so leaving the body empty does not work for me. Notice that the contrary is possible: with callOnClick() we can invoke the method without getting the feedback.
In addition, long clicks have no default feedback (at least on my devices). Any way to add it, again without resorting to performClick()?
EDIT:
A workaround could be using a dummy View to perform an empty click on. But I would like to know if there is a more elegant solution without this overhead.
For the record, I am not asking how to play a sound programmatically with the MediaPlayer. I know how to do that quite well.
I first searched how to replace the default click sound of a Button in the XML. But I have not found out if that is possible. Most answers suggest to use the MediaPlayer to play a sound effect somewhere in/from the onClick() event of the Button, so I assume that's the best way to go?
If I use the MediaPlayer to play a sound when a Button is clicked, do I have to disable the default click sound as well, or will both play, or will Android just know to ignore the default click sound? Should I call setSoundEffectsEnabled(false) on the Button before I play my own sound?
It seems very strange to me that I can't just replace the default click sound of a Button (is it possible to do that?)
Just set android:soundEffectsEnabled=false in your theme defined into your res/styles.xml
or programatically:
myButton.setSoundEffectsEnabled(false);
you may wish to check this link as well: Playing sound effect (CLICK/NAVIGATION_RIGHT) for button clicks - Android
seems that sound effects are kept as android.view.SoundEffectConstants.<value>
correspondingly, there might be a way of adding your own custom sound
it is all about passing the value of sound effect you'd like to hear, into <view variable>.playSoundEffect(<sound effect value>); so you may try to add your sound into res and call that method with its R value
I have an AppWidget (part of my app). I want there to be no sound when the user clicks a button in the widget.
How do I do this?
CommonsWare answered it:
This is not a method on RemoteViews, and setSoundEffectsEnabled() is not a RemotableViewMethod, so the literal answer is incorrect. However, android:soundEffectsEnabled="false" in the layout file may work.
setting android:soundEffectsEnabled="false" in the xml layout file does indeed work!
As far as I know, you have to turn off notification sounds (pressing volume down, then selecting settings on it). The click sound is usually created by the OS (Samsung Android will have a click but google Nexus will not). The only other way I can think of is, if the widget is yours, create a custom button that overrides that particular functionality.
Add the following line of code to disable the click sound,
yourbutton.setSoundEffectsEnabled(false);
Usually on the Android OS when something is touched like a button or list item it makes a touch tone or sound. I have a custom made AdapterView (DevsmartLib) that basically creates a horizontal list view for me, however, none of the items make sounds when touched. I'm wondering if anyone knows what settings or options control whether a touch ends up triggering a sound or not. Any help or pointers are highly appreciated.
Thanks,
Harry
I think this SO-thread answers your questions.
You can use the xml attribute soundEffectsEnabled, or its corresponding java method, and after that you can play sound effects using Sound Effect Constants in the playSoundEffect method. This could also be done with MediaPlayer, but I don't think that's the kind of sound you want.
This developer page on multimedia might help if you want other kinds of sound.
I have a button that plays a sound clip when it is pressed. I would like the button to appear pressed during the duration of the sound clip. By that, I mean that I would like the button to take on the default pressed-button appearance while the sound is playing. How can I implement this? I have tried using a number of things in the onClickListener (such as setSelected, requestFocus, etc), but none of those do the trick. I have also tried changing the onClickListener to an onTouchListener, again with no dice. Am I wrong in assuming that there must be a way to simply set the button image to appear pressed? (BTW, the button object is of type Button, not ImageButton).
Thanks for any advice!
Please see this question. It details a couple of different ways this can be done.
Lookup 'selector' drawables and let android take care of this for you.