How to access default Android sounds by default?

I would like the button to play a beep to indicate that it was pressed. I want to know how to use the default audio alert for Android (for example, when adjusting the ringer volume) instead of importing my own mp3 file or using ToneGenerator?

+48
java android
Jun 24 2018-11-11T00:
source share
3 answers
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); 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 is being sent to https://stackoverflow.com/a/166168/

+67
Jul 14 2018-11-11T00:
source share

... use the default beep for Android (for example, when adjusting the ringer volume) ...

On my Cyanogen 7 Nexus One and my old stock of T-Mobile Pulse Mini (the last from memory), as far as I can hear, this is exactly the default beep when you change the volume:

  final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100); tg.startTone(ToneGenerator.TONE_PROP_BEEP); 

It seems you are asking for an alternative to ToneGenerator , but I think this gives you exactly what you want in two lines.

Here are a few other possible ToneGenerator sounds that I tried that did not match (the first two can be useful as an alternative to loud sound):

  // 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); 
+70
May 2 '12 at 14:42
source share

an easy way is to use an instance of the ToneGenerator class:

  //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 } 
0
Feb 22 '13 at 0:33
source share



All Articles