Play notification audio by URI

后端 未结 3 669
长情又很酷
长情又很酷 2021-01-05 18:50

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

相关标签:
3条回答
  • 2021-01-05 18:53

    Inspired by Dmitry's answer.

    One line can solved the problem.

    0 讨论(0)
  • 2021-01-05 18:58

    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.

    0 讨论(0)
  • 2021-01-05 19:11

    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.

    0 讨论(0)
提交回复
热议问题