-1

I would like to play a sound (which I generate in a standalone app) when an event/notification occurs, like an incoming message or a notification from a game. For example: I receive a message in Telegram and the OS proceeds to "play the ringtone" (but it instead runs my sound-generating app).

I thought this behavior would require some interaction with the OS in order to "redirect" the default action of playing a ringtone's file to my app, so I could generate my sound and play it. But it's just a theory. xD :P

I searched in Google, but nothing showed up. :( I have made apps for Android, but in this issue I really have no clue of how to accomplish this. Thanks in advance for any help provided! :D

David MS
  • 19
  • 1
  • 3

1 Answers1

0

From: How to play an android notification sound

try {
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
    r.play();
} catch (Exception e) {
    e.printStackTrace();
}

You can change TYPE_NOTIFICATION to TYPE_ALARM, but you'll want to keep track of your Ringtone r in order to stop playing it... say, when the user clicks a button or something.

Also here's a link that maybe can help you solve your problem

NoobProgrammer
  • 488
  • 1
  • 8
  • 21
  • Thanks for your help! :D But I'm afraid it won't do the trick. :( I added more info to the question description to clarify what I would like to do. – David MS Jan 16 '18 at 02:00