Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play notification default sound only (Android)

How can I play a notification sound only (without firing a status bar notification)? I want the notification default sound and play it exactly as a notification sound. Is it possible to achieve it using MediaPlayer?

like image 682
Rafael Avatar asked Apr 26 '12 14:04

Rafael


People also ask

Can you have separate notification sounds on Android?

You can change and set up custom notification sounds via the default phone settings. Here's how to do this: On your device, navigate to Settings > Apps > Your apps. The menu names may be slightly different on some devices—for this guide, we'll be using a Samsung device.


2 Answers

Uri defaultRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

MediaPlayer mediaPlayer = new MediaPlayer();

try {
      mediaPlayer.setDataSource(context, defaultRingtoneUri);
      mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
      mediaPlayer.prepare();
      mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

         @Override
         public void onCompletion(MediaPlayer mp)
         {
            mp.release();
         }
      });
  mediaPlayer.start();
} catch (IllegalArgumentException e) {
 e.printStackTrace();
} catch (SecurityException e) {
 e.printStackTrace();
} catch (IllegalStateException e) {
 e.printStackTrace();
} catch (IOException e) {
 e.printStackTrace();
}
like image 78
dymmeh Avatar answered Oct 04 '22 14:10

dymmeh


Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(context.getApplicationContext(), notification);
r.play();
like image 37
18446744073709551615 Avatar answered Oct 04 '22 15:10

18446744073709551615