16

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?

kar
  • 741
  • 6
  • 16
Rafael
  • 6,339
  • 5
  • 22
  • 22
  • You could do it with a mediaplayer if you just find the sound file. You should be able to find the default sound somewhere within the android sources. Check out the Notification-class and [this](http://stackoverflow.com/questions/4441334/how-to-play-an-android-notification-sound) question. – keyser Apr 26 '12 at 14:05

3 Answers3

31
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();
}
dymmeh
  • 22,247
  • 5
  • 53
  • 60
9
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(context.getApplicationContext(), notification);
r.play();
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
0

you can try this

private void sendNotification(String title, String messageBody,String tag) {

    Intent intent;
    intent = new Intent(this, MainActivity.class);
    intent.setAction(tag);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);
    String channel_id=this.getResources().getString(R.string.app_name);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(title)
            .setContentText(messageBody)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(messageBody))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setChannelId(channel_id)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel mChannel = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        mChannel = new NotificationChannel(channel_id, channel_id, NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(mChannel);
    }
    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
} 
David Buck
  • 3,752
  • 35
  • 31
  • 35
arya
  • 1