I would like to make a button play a beep sound to indicate it has been pressed. I want to know how to use the default android beep sound (like when you adjust the ringer volume), instead of importing my own mp3 music file or using ToneGenerator?
4 Answers
... use the default android beep sound (like when you adjust the ringer volume) ...
On my Cyanogen 7 Nexus One and my old stock T-Mobile Pulse Mini (the latter from memory), as far as I can hear, this is is exactly the default beep sound on volume change:
final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
tg.startTone(ToneGenerator.TONE_PROP_BEEP);
You seem to be asking for an alternative to ToneGenerator
, but I think it gives you exactly what you want in two lines.
Here are some other likely ToneGenerator
sounds I tried that were not a match (the first two might be useful as alternates to the volume beep):
// Double beeps: tg.startTone(ToneGenerator.TONE_PROP_ACK);
// Double beeps: tg.startTone(ToneGenerator.TONE_PROP_BEEP2);
// Sounds all wrong: tg.startTone(ToneGenerator.TONE_CDMA_KEYPAD_VOLUME_KEY_LITE);

- 9,349
- 5
- 33
- 38
-
Best would be to use a continuous tone like the ones from ToneGenerator.TONE_DTMF_0 to ToneGenerator.TONE_DTMF_S or else the beep sound will interrupt and start again based on selected tone description. This will happen if your durationMs (passed as param in startTone (int toneType, int durationMs) is bigger than total tone's duration. – ungalcrys Feb 10 '15 at 15:13
-
Hi @ungalcrys, I am not in a position to recheck this easily at the moment but the questioner asked for, "default android beep sound (like when you adjust the ringer volume)". When I tested, that was exactly the one I give above: `ToneGenerator.TONE_PROP_BEEP`. – ahcox Feb 10 '15 at 16:17
-
Anyone had any issues with the S6 Edge? The ToneGenerator works fine in the S6, but the S6 Edge gives me nothing. – Brian S Jun 12 '15 at 17:53
-
@GREnvoy Hi, that is interesting. I think it should be its own question though. Or even better, a support request to Samsung. – ahcox Jun 12 '15 at 18:37
-
Super easy... in 2022... Thank you!! – Mohamad Ghaith Alzin Sep 07 '22 at 06:34
public void playSound(Context context) throws IllegalArgumentException,
SecurityException,
IllegalStateException,
IOException {
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(context, soundUri);
final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
// Uncomment the following line if you aim to play it repeatedly
// mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
}
I found another answer:
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
credit goes to https://stackoverflow.com/a/9622040/737925

- 4,765
- 5
- 32
- 61

- 12,304
- 8
- 54
- 77
-
8I used that code and got annoyed that the sound repeated itself. Now I see the problem: `mMediaPlayer.setLooping(true);` Why would you want that sound to loop?? – Simon Forsberg Apr 25 '12 at 15:25
-
4
-
2Does it matter that mMediaPlayer goes out of scope and is available to be garbage collected before the sound has finished playing, or is the sound already passed on to lower-level components that will ensure it finishes playing by the `mMediaPlayer.start()` call? – ahcox May 02 '12 at 13:25
-
2Note, this code is checking the sound level for and playing on the alarm stream. For the poster's case, you'd want to swap `AudioManager.STREAM_AlARM` instances for `AudioManager.STREAM_NOTIFICATION`. See `AudioManager` docs for other options, but a notification seems the right choice here. – ahcox May 02 '12 at 13:49
-
4For me, `RingtoneManager.TYPE_NOTIFICATION` is playing the long polyphonic "new text message arrived" tone. I don't see an option that will get the OP their standard volume changed beep. – ahcox May 02 '12 at 14:45
You can access Android's default beeb sound via ToneGenerator class.
import android.media.AudioManager;
import android.media.ToneGenerator;
ToneGenerator toneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, 200);
toneGenerator.startTone(ToneGenerator.TONE_CDMA_EMERGENCY_RINGBACK);
More info on how they sound: https://developer.android.com/reference/android/media/ToneGenerator and https://www.youtube.com/watch?v=HVu7K9W1_BM

- 1,377
- 1
- 8
- 13
-
1Brilliant, thanks. For a moment I thought that I was going to have to write 3498734 lines of code to make the app go beep. – pookie Jul 27 '20 at 22:47
the easy way is to use instance of ToneGenerator classe:
//declaration
ToneGenerator toneG;
//using any where`
if(val>=taux_max)
{
taux_text.setTextColor(warnning_col);
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200); //200 is duration in ms
}

- 176
- 1
- 7