5

I have URI of the notification sound, like content://media/internal/audio/media/122, but SoundPool doesn't work with URIs, it works with only apk resources of file paths.

Is there a way to get media file path from URI? I tried Uri.getPath(), which returns /internal/audio/media/122, but this isn't valid file path, and of course SoundPool doesn't work with such patch.

Actually the real path to the notification is /system/media/audio/notifications/allegro.ogg , and I see no way to get it from URI like content://media/internal/audio/media/122

Or should I use MediaPlayer for that? I tested, it works:

           MediaPlayer mp = MediaPlayer.create(this, "content://media/internal/audio/media/122");
           mp.start();

I'm confused because MediaPlayer seems to be too heavy to just play short notification sound, so I'd prefer to use SoundPool.

Please tell me if I'm wrong about MediaPlayer weight, and suggest correct way to play notification sound by its URI.

Dmitry Frank
  • 10,417
  • 10
  • 64
  • 114

3 Answers3

22

Well, I got the easiest way to play audio by URI:

RingtoneManager.getRingtone(this, Uri.parse("content://media/internal/audio/media/122")).play();

I think this is the best what i could achieve.

Dmitry Frank
  • 10,417
  • 10
  • 64
  • 114
  • The downside is that Ringtone uses MediaPlayer internally, yet does not provide an easy way to release it afterwards. – jk7 Apr 10 '18 at 00:35
0

Try this to get a full path:

String path = Environment.getRootDirectory().getAbsolutePath(); //Will return "/system"
path = path + Uri.getPath().subString(9); //To cut "content:/" from the Uri path.
//Then pass this "path" to your SoundPool

You are right. Unlike MediaPlayer which is used for music and videos, SoundPool is meant to be used for short app audio resources like notifications. It is much faster and lighter than MediaPlayer.

iTurki
  • 16,292
  • 20
  • 87
  • 132
  • Thanks, and suggest please, should I use MediaPlayer instead, if "SoundPool is meant to be used for short app audio resources"? – Dmitry Frank Aug 21 '12 at 07:12
  • @DmitryFrank Updated. If your sound is short then stick to the SoundPool. If it is long and the SoundPool didn't work well, then you need to switch to the MediaPlayer. That's what I would do. – iTurki Aug 21 '12 at 07:14
  • Unfortunately your way does not work: the real path to the notification is `/system/media/audio/notifications/allegro.ogg` , and I see no way to get it from URI like `content://media/internal/audio/media/122`. – Dmitry Frank Aug 21 '12 at 07:21
  • Why are you using Uri? You could provide the `notification path` as a String using [`sp.load()`](http://developer.android.com/reference/android/media/SoundPool.html#load%28java.lang.String,%20int%29) – iTurki Aug 21 '12 at 07:34
  • I use URI because I get URI from media select dialog. See this answer to get what dialog I meant: http://stackoverflow.com/questions/12038946/android-open-dialog-for-select-audio/12048448#12048448 – Dmitry Frank Aug 21 '12 at 07:38
  • it seems like you didn't read carefully my comments. Uri.getPath() returns `/internal/audio/media/122`, but the real path to the file is `/system/media/audio/notifications/allegro.ogg`. Could you please feel the difference? They are completely different, there's not just `/system` at the beginning. – Dmitry Frank Aug 21 '12 at 07:49
  • Yes it can, but it needs for file path, i.e. `/system/media/audio/notifications/allegro.ogg`, not `content://media/internal/audio/media/122`. – Dmitry Frank Aug 21 '12 at 07:58
  • 1
    Sorry. I thought `/system/media/internal/audio/media/122` will work. You may want to try the first method in [this answer](http://stackoverflow.com/a/11603837/543711). Sounds promising. – iTurki Aug 21 '12 at 08:03
  • 1
    Anyway, problem seems to be solved now (check out my own answer to this question). Thanks for trying to help. – Dmitry Frank Aug 21 '12 at 08:03
0

Inspired by Dmitry's answer.

One line can solved the problem.

RingtoneManager.getRingtone(this, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).play();
Johnny
  • 1,824
  • 23
  • 16