Play notification audio by URI

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

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.

回答1:

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.



回答2:

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!