Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play notification audio by URI

Tags:

android

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.

like image 490
Dmitry Frank Avatar asked Aug 21 '12 06:08

Dmitry Frank


1 Answers

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.

like image 168
Dmitry Frank Avatar answered Oct 29 '22 12:10

Dmitry Frank