问题
I want to connect Android OS default tick sound (for example, the sound you hear when you long click Home button and select previous app to start) with my button click. I know how to play sounds via MediaPlayer, but I do not know where to search for this default tick sound. It had to be in some default resources, but I could not find it.
MediaPlayer mp = MediaPlayer.create(getBaseContext(), sound);
mp.setLooping(false);
mp.start();
Anyone can help?
PS. this sound will be activated inside of onClick method.
PPS. I know I can user /raw dir, but I do not think there's a need for it. Not to say, it's cooler to play this tick sound prepared for user's phone.
回答1:
NOTE: This is a pretty old answer. Check Roberto Tyley's answer below.
I think the sound that you are looking for is and is in - /system/media/audio/ui/KeypressStandard.ogg
I think you can give that path to the SetDataSource
API of the mediaplayer. But I am not really sure if it will have the same name in all android phones.
There might be a better way to query for default click sound..
回答2:
You can play the default Android 'tick' sound using the view.playSoundEffect() method on any View - surprisingly enough, all views can play a selection of 'system' sounds:
view.playSoundEffect(SoundEffectConstants.CLICK);
This is probably the simplest answer to your problem :)
回答3:
If you want control over the volume of the sound, use the AudioManager
system service. Plus this can be used from a Service if you don't have a view handy.
// get the AudioManager once onCreate or similar
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
...
// Play a System Sound
audioManager.playSoundEffect(SoundEffectConstants.CLICK);
// OR at 50% Volume
audioManager.playSoundEffect(SoundEffectConstants.CLICK, 0.5F);
If you are on Google GLASS you can use the com.google.android.glass.media.Sounds
constants
audioManager.playSoundEffect(Sounds.TAP);
The following are included in Sounds
DISALLOWED, ERROR, SUCCESS, TAP, SELECTED, DISMISSED
回答4:
Roberto Tyley's answer is correct.
You can play a sound from every view just by calling it this way:
Button01.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
v.playSoundEffect(SoundEffectConstants.CLICK);
}
});
Just note that the sound will not play if touch sounds are off by default. This is set in the general device sound preferences (Settings-->Sound-->Audible or on newer OS: Options > Sound > Touch)
Also, if this setting is set, most click events will trigger the click sound anyway!
回答5:
You can use tick, beep or any kind of inbuilt sound by Tone Generator
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200);
来源:https://stackoverflow.com/questions/7914518/how-to-play-default-tick-sound